如何在Django中为EmailMultiAlternatives定制电子邮件后端

时间:2012-01-19 11:55:59

标签: django django-settings django-email

背景: -

我的电子邮件后端是这样的:

EMAIL_HOST ='smtp.gmail.com' 
EMAIL_PORT = 587
EMAIL_HOST_USER = 'feedback@example.com' 
EMAIL_HOST_PASSWORD = 'XXX' 
EMAIL_USE_TLS = True

问题: -

我正在使用EmailMultiAlternatives发送html邮件。 现在我想定义auth_user和auth_password,以便我可以更改“from”。基本上我想过度使用EMAIL_HOST_USER,但希望from_email是详细的,即“支持团队”。怎么办呢?

subject, from_email, to = "hello", 'Support Team','to_user@gmail.com'
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 = 'support@example.me',auth_password = 'XXX')

1 个答案:

答案 0 :(得分:3)

你可以这样做:

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")