【问题标题】:Send HTML Email with Inline Images in Django在 Django 中发送带有内联图像的 HTML 电子邮件
【发布时间】:2014-12-24 05:15:08
【问题描述】:

我正在尝试发送带有内嵌图像的 HTML 电子邮件,如文章所示:https://www.vlent.nl/weblog/2014/01/15/sending-emails-with-embedded-images-in-django/。我已经让它工作了。现在,我想将它与 Django 邮件程序集成:https://github.com/pinax/django-mailer

I-e 我可以将电子邮件排队发送并一次性发送一批电子邮件。

我的代码是:

msg = EmailMultiAlternatives(subject, text_content, from_email, to_email)
msg.attach_alternative(html_content, "text/html")
msg.mixed_subtype = 'related'

fp = open(STATIC_ROOT+ filename, 'rb')
msg_img = MIMEImage(fp.read())
fp.close()
msg_img.add_header('Content-ID', '<{}>'.format(filename))
msg.attach(msg_img)

要发送电子邮件,我只需要:

msg.send()

要使用 Django 邮件程序发送 html 电子邮件,我必须使用模块:

send_html_mail(subject, message_plaintext, message_html, settings.DEFAULT_FROM_EMAIL, recipients)

msg.send_html_mail 显然不起作用。我错过了什么还是有其他选择?

【问题讨论】:

    标签: django html-email django-mailer


    【解决方案1】:

    您可以自己实例化连接(请注意,Django 1.8 将为此包含一个上下文管理器,但尚未推出)并发送邮件。这应该可以解决问题:

    from django.core import mail
    
    connection = mail.get_connection()
    connection.open()
    connection.send_messages(your_messages)
    connection.close()
    

    或者:

    from django.core import mail
    
    connection = mail.get_connection()
    connection.open()
    for to_email in recipients:
        # Generate your mail here
        msg = EmailMultiAlternatives(..., connection=connection)
        msg.send()
    connection.close()
    

    【讨论】:

      猜你喜欢
      • 2013-11-11
      • 2017-02-23
      • 2011-06-08
      • 2011-11-22
      • 2012-04-07
      • 2011-11-09
      • 2014-12-05
      相关资源
      最近更新 更多