使用EmailMultiAlternatives发送send_mass_emails

gus*_*ans 20 django

我尝试建立一个简报应用程序,并希望通过一个连接发送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()  
Run Code Online (Sandbox Code Playgroud)

上面的代码和send_mass_mail的工作示例将是伟大的,谢谢!

sem*_*nte 33

这被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)
Run Code Online (Sandbox Code Playgroud)

  • 应该合并到 Django (2认同)

msa*_*ers 15

使用EmailMultiAlternatives直接使用send_mass_mail()是不可能的.但是,根据Django文档,send_mass_mail()只是一个使用EmailMessage类的包装器.EmailMultiAlternatives是EmailMessage的子类.EmailMessage具有"连接"参数,允许您指定在向所有收件人发送邮件时使用的单个连接 - 即与send_mass_mail()提供的功能相同.您可以使用get_connection()函数获取由settings.py中的SMTP设置定义的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
Run Code Online (Sandbox Code Playgroud)

  • 使用'bcc'例如:msg = EmailMultiAlternatives("subject",text_content,"from @ bla",["to @ bla"],bcc = ["to2 @ bla","to3 @ bla"],connection = connection) (2认同)
  • 当你没有迭代或创建多个EmailMultiAlternatives实例时,我不明白为什么你需要手动打开和关闭smtp连接. (2认同)