2015-10-26 16 views
1

我試圖用EmailMultiAlternatives發送郵件html和 的一段文字。我也想在這封電子郵件中包含一個文件。emailmultialternatives在django中附加文件和html內容

但後來似乎抹去我的html內容。

這裏是我的代碼:

msg = EmailMultiAlternatives(subject, html2text(html_content), 
list(email_from), list(email_to), 
attachments=((request.session['customer']+".txt.blowfish", 
request.session["customer"].content),)) 

msg.attach_alternative(html_content, "text/html") 

msg.send() 

我用的是最新的SVN版本 我使用msg.attact()代替附件,同樣的結果也試過!

發送替代文本內容,但html不是。它只有 顯示該文件。

任何線索將不勝感激,

回答

3

我有同樣的問題!

我用一個文件附加到HTML電子郵件的代碼是:

email_dict = {} 
email_dict['user'] = user      

t = loader.get_template('emails/myemail.html') 
html = t.render(Context(email_dict)) 

msg = EmailMultiAlternatives(subject='My Subject', 
          body=html, 
          from_email=settings.DEFAULT_FROM_EMAIL, 
          to=['[email protected]'],) 

attachment = open('filepath', 'rb') 
msg.attach('Name.txt', attachment.read(), 'text/csv') 

msg.content_subtype = 'html' 
msg.send() 

因此,也許這樣的事情可能會爲你工作:

msg = EmailMultiAlternatives(subject=subject, 
          body=html2text(html_content), 
          from_email=list(email_from), 
          to=list(email_to)) 


attachment = open(request.session['customer']+".txt.blowfish", 'rb') 
msg.attach('Name.txt.blowfish', attachment.read(), 'text/plain') 

msg.content_subtype = 'html' 
msg.send() 

我希望這有助於!

+0

就這麼完美的答案..! – Jay