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

发布于 2024-12-28 08:12:37 字数 1089 浏览 1 评论 0原文

背景:-

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

EMAIL_HOST ='smtp.gmail.com' 
EMAIL_PORT = 587
EMAIL_HOST_USER = '[email protected]' 
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','[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')

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生生不灭 2025-01-04 08:12:37

你可以这样做:

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

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")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文