不能直接将send_mass_mail()与EmailMultiAlternatives一起使用。但是,根据Django文档,send_mass_mail()只是使用EmailMessage类的包装器。EmailMultiAlternatives是EmailMessage的子类。EmailMessage具有一个“连接”参数,允许你指定将消息发送给所有收件人时要使用的单个连接,即与send_mass_mail()提供的功能相同。你可以使用get_connection()函数来获取settings.py中SMTP设置所定义的SMTP连接。
我相信以下(未经测试)的代码应该可以工作:
from django.core.mail import get_connection, EmailMultiAlternativesconnection = get_connection() # uses SMTP server specified in settings.pyconnection.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
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)