How to customize email backend for EmailMultiAlternatives in Django

3.1k Views Asked by At

Background :-

My email Backend is something like this:

EMAIL_HOST ='smtp.gmail.com' 
EMAIL_PORT = 587
EMAIL_HOST_USER = '[email protected]' 
EMAIL_HOST_PASSWORD = 'XXX' 
EMAIL_USE_TLS = True

Problem:-

I am using EmailMultiAlternatives to send html mails. Now I want to define auth_user and auth_password so that i can change the "from". Basically I want to over ride the EMAIL_HOST_USER, but want the from_email to be verbose, i.e. 'Support Team'. How can it be done?

subject, from_email, to = "hello", 'Support Team','[email protected]'
text = "hello"
html = "<strong>hello</strong>"
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
msg.attach_alternative(html_content, "text/html")
msg.send(fail_silently=False,auth_user = '[email protected]',auth_password = 'XXX')
1

There are 1 best solutions below

3
On

You could do something like this:

from django.core.mail import *

#retrieve emailbackend from settings.py
connection = get_connection(username, password)
#change connection parameters
connection.host='XXX'
msg = EmailMultiAlternatives(subject, text_content, from_email, [to], connection)
msg.attach_alternative(html_content, "text/html")