一尘不染

send_mass_emails和EmailMultiAlternatives

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

上面的代码和send_mass_mail的工作示例将非常棒,谢谢!


阅读 622

收藏
2020-04-03

共2个答案

一尘不染

不能直接将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, 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
2020-04-03
一尘不染

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)
2020-04-03