我试图建立一个通讯应用程序,并希望发送50封电子邮件与一个连接。send_mass_mail()看起来很完美,但我不知道如何将它与EmailMultiAlternatives结合起来。
这是我的代码,只发送一封只有一个连接的电子邮件:
html_content = render_to_string('newsletter.html', {'newsletter': n,})
text_content = "..."
msg = EmailMultiAlternatives("subject", text_content, "from@bla", ["to@bla"])
msg.attach_alternative(html_content, "text/html")
msg.send() 使用上述代码和send_mass_mail的工作示例将是很好的,谢谢!
发布于 2011-09-28 13:40:22
不可能直接将send_mass_mail()与EmailMultiAlternatives一起使用。但是,根据Django文件,send_mass_mail()只是一个利用EmailMessage类的包装器。EmailMultiAlternatives是EmailMessage的子类。EmailMessage有一个“connection”参数,允许您指定向所有收件人发送消息时使用的单个连接,即与send_mass_mail()提供的相同功能。您可以使用连接()函数获得由SMTP设置在settings.py中定义的SMTP连接。
我认为下列(未经测试的)代码应该有效:
from django.core.mail import get_connection, EmailMultiAlternatives
connection = get_connection() # uses SMTP server specified in settings.py
connection.open() # If you don't open the connection manually, Django will automatically open, then tear down the connection in msg.send()
html_content = render_to_string('newsletter.html', {'newsletter': n,})
text_content = "..."
msg = EmailMultiAlternatives("subject", text_content, "from@bla", ["to@bla", "to2@bla", "to3@bla"], connection=connection)
msg.attach_alternative(html_content, "text/html")
msg.send()
connection.close() # Cleanup发布于 2012-04-18 17:52:59
即send_mass_mail()重写为使用EmailMultiAlternatives
from django.core.mail import get_connection, EmailMultiAlternatives
def send_mass_html_mail(datatuple, fail_silently=False, user=None, password=None,
connection=None):
"""
Given a datatuple of (subject, text_content, html_content, from_email,
recipient_list), sends each message to each recipient list. Returns the
number of emails sent.
If from_email is None, the DEFAULT_FROM_EMAIL setting is used.
If auth_user and auth_password are set, they're used to log in.
If auth_user is None, the EMAIL_HOST_USER setting is used.
If auth_password is None, the EMAIL_HOST_PASSWORD setting is used.
"""
connection = connection or get_connection(
username=user, password=password, fail_silently=fail_silently)
messages = []
for subject, text, html, from_email, recipient in datatuple:
message = EmailMultiAlternatives(subject, text, from_email, recipient)
message.attach_alternative(html, 'text/html')
messages.append(message)
return connection.send_messages(messages)发布于 2014-08-14 10:36:20
我采用了一种混合的方法,向用户发送纯文本和html消息,并为每个用户个性化消息。
我从发送多封电子邮件 od Django文档一节开始。
from django.conf import settings
from django.contrib.auth.models import User
from django.core import mail
from django.core.mail.message import EmailMultiAlternatives
from django.template.loader import get_template
from django.template import Context
...
connection = mail.get_connection()
connection.open()
messages = list()
for u in users:
c = Context({ 'first_name': u.first_name, 'reseller': self,})
subject, from_email, to = 'new reseller', settings.SERVER_EMAIL, u.email
text_content = plaintext.render(c)
html_content = htmly.render(c)
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
msg.attach_alternative(html_content, "text/html")
messages.append(msg)
connection.send_messages(messages)
connection.close()https://stackoverflow.com/questions/7583801
复制相似问题