Defining a custom email backend

217 views
Skip to first unread message

Sujata Aghor

unread,
Sep 23, 2021, 1:18:02 AM9/23/21
to Django users
Hello Everyone,
I want to use different values of - EMAIL_HOST_USER & EMAIL_HOST_PASSWORD for each of my users. I will take those values from database and not from settings.py

In django documentation I can see only a single paragraph about defining a custom email backend, which is not giving me enough ideas.

If I Try to change value of EMAIL_HOST_USER in script, it gives me below error - 
send_mail() got an unexpected keyword argument 'EMAIL_HOST_USER'
How to change these two values in a script?
Can anyone help here please.


--

Thanks & Regards!
Sujata S. Aghor

sum abiut

unread,
Sep 23, 2021, 1:28:26 AM9/23/21
to django...@googlegroups.com
If you don't mind sharing your code here.

cheers, 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAJCP8KC9Acj36VhnY1UqnpFtVb_xGLkZgmZJgPyugdhmH2QvOQ%40mail.gmail.com.


Sujata Aghor

unread,
Sep 23, 2021, 1:40:35 AM9/23/21
to Django users
settings.py : 
DEFAULT_FROM_EMAIL = '*****************'
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_USE_TLS = True
EMAIL_PORT = ***
EMAIL_HOST_USER = '*********************"
EMAIL_HOST_PASSWORD = '***********"


script.py :

from_email = DEFAULT_FROM_EMAIL
vars_dict = dict(tasks=usr_tasks,
                     reminder=reminder,
                     reseller=reseller,
                     logo=logo,
                     host=host,
                     )

# get values: r_email and r_pass specific to this reseller
        r_obj = Reseller.objects.get(id=rid)
        r_email = r_obj.remail if r_obj.remail else None
        r_pass = r_obj.rpass if r_obj.rpass else None  

        html_message = loader.render_to_string(
            template,vars_dict)

        if subject and message and from_email:
            print("Sending mail to %s"%recipient)
            try:
                if r_email and r_pass:
                    send_mail(subject,message,from_email,recipient,fail_silently=True, EMAIL_HOST_USER=r_email,
                    EMAIL_HOST_PASSWORD=r_pass, html_message=html_message)
                else:  
                    send_mail(subject,message,from_email,recipient,fail_silently=True,html_message=html_message)
            except Exception as e:
                print(e)
            else:
                print("Mail sent successfully!")

---------------------------------------------------------------------------------------------------------------------------------
Roughly this is my code.
in line -  send_mail(subject,message,from_email,recipient,fail_silently=True, EMAIL_HOST_USER=r_email,
                    EMAIL_HOST_PASSWORD=r_pass, html_message=html_message)
its giving me error - send_mail() got an unexpected keyword argument 'EMAIL_HOST_USER'

Can you suggest anything about this ?
Thanks


sum abiut

unread,
Sep 23, 2021, 1:57:21 AM9/23/21
to django...@googlegroups.com
You can create your own connection and then pass to send_mail()

connection = get_connection(host=my_host, 
                            port=my_port, 
                            username=my_username, 
                            password=my_password, 
                            use_tls=my_use_tls) 

send_mail('subject', 'test message', 'from_email', ['to'], connection=connection)

Sujata Aghor

unread,
Sep 24, 2021, 2:00:04 AM9/24/21
to Django users
Got my answer here - https://stackoverflow.com/questions/43857594/django-set-e-mail-settings-in-view/43881974#43881974
Setting up values for connection object from database for specific reseller.
Thanks
Reply all
Reply to author
Forward
0 new messages