使用EmailMultiAlternatives发送send_mass_emails [英] send_mass_emails with EmailMultiAlternatives

查看:602
本文介绍了使用EmailMultiAlternatives发送send_mass_emails的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试构建一个通讯应用程序,并希望通过一个连接发送50封电子邮件。 send_mass_mail()看起来很完美,但我无法弄清楚我如何将它与EmailMultiAlternatives结合使用。

I try to build a newsletter application and want to send 50 emails with one connection. send_mass_mail() looks perfect but I can't figure out how I call this in combination with EmailMultiAlternatives.

这是我的只发送的代码一个电子邮件,一个连接:

This is my code that sends only one email with one connection:

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会很棒,谢谢!

a working example with the code above and send_mass_mail would be great, thanks!

推荐答案

不可以直接使用send_mass_mail()与EmailMultiAlternatives。但是,根据 Django文档,send_mass_mail()只是一个包装器使用 EmailMessage 课程。 EmailMultiAlternatives是EmailMessage的子类。 EmailMessage有一个'connection'参数,允许您指定在向所有收件人发送消息时使用的单个连接 - 即与send_mass_mail()提供的功能相同的功能。您可以使用 get_connection()功能获取SMTP连接由settings.py中的 SMTP设置定义。

It's not possible to directly use send_mass_mail() with EmailMultiAlternatives. However, according to the Django documentation, send_mass_mail() is simply a wrapper that makes use of the EmailMessage class. EmailMultiAlternatives is a subclass of EmailMessage. EmailMessage has a 'connection' parameter allowing you to specify a single connection to use when sending the message to all recipients - i.e. the same functionality as provided by send_mass_mail(). You can use the get_connection() function to obtain an SMTP connection as defined by the SMTP settings in settings.py.

我相信以下(未经测试)的代码应该可以工作:

I believe that the following (untested) code should work:

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

这篇关于使用EmailMultiAlternatives发送send_mass_emails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆