我正在尝试使用内嵌图像发送HTML电子邮件,如文章https://www.vlent.nl/weblog/2014/01/15/sending-emails-with-embedded-images-in-django/所示。我有它的工作。现在,我想将它与Django邮件程序集成:https://github.com/pinax/django-mailer。
我可以将电子邮件排队,一次发送和发送一批电子邮件。
我的代码是:
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显然无效。我错过了什么或有替代方案吗?
答案 0 :(得分: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()