簡體   English   中英

在Django中發送帶有嵌入式圖像的HTML電子郵件

[英]Send HTML Email with Inline Images in Django

我正在嘗試發送帶有內嵌圖像的HTML電子郵件,如文章中所示: https : //www.vlent.nl/weblog/2014/01/15/sending-emails-with-embedded-images-in-django/ 我已經做好了。 現在,我想將其與Django mailer集成: 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 mailer發送html電子郵件,我必須使用以下模塊:

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

msg.send_html_mail顯然不起作用。 我是否缺少某些東西或有其他選擇?

您可以自己實例化連接(請注意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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM