2012-07-18 45 views
2

在Django中(與Python)的Python/Django的MIME多的電子郵件,我怎麼可以構建如下所示的電子郵件:利用邊界

我需要的是開頭的電子郵件:

Content-Type: multipart/alternative; 
boundary="=_5f8686714d769f9476ce778798b84ff4" 

邊界是把純文本的地方:

--=_5f8686714d769f9476ce778798b84ff4 
Content-Type: text/plain; charset="ISO-8859-1" 
Content-Transfer-Encoding: 7bit 

This e-mail is in HTML-format. Click the following link: 

http://www.company.nl/enews/7 
--=_5f8686714d769f9476ce778798b84ff4 

之後,我需要的相關內容塔伊:

Content-Type: multipart/related; 
boundary="=_e49477d06c604958b9b2bc038628a26f" 

邊界是將HTML

--=_e49477d06c604958b9b2bc038628a26f 
Content-Type: text/html; charset="ISO-8859-1" 
Content-Transfer-Encoding: quoted-printable 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.= w3.org/TR/html4/loose.dtd"> <html style=3D"height: 100%"> <head>/head> <body></body> </html> 
--=_e49477d06c604958b9b2bc038628a26f 

最終,它看起來像這樣的地方:

Received: (qmail 6116 invoked by uid 48); 16 Jul 2012 18:00:49 +0200 
Date: 16 Jul 2012 18:00:49 +0200 
To: [email protected] 
Subject: Something 
MIME-Version: 1.0 
From: Company <[email protected]> 

Content-Type: multipart/alternative; 
    boundary="=_5f8686714d769f9476ce778798b84ff4" 
Message-ID: <[email protected]> 

--=_5f8686714d769f9476ce778798b84ff4 
Content-Type: text/plain; charset="ISO-8859-1" 
Content-Transfer-Encoding: 7bit 

This e-mail is in HTML-format. Click the following link: 

http://www.company.nl/enews/7 
--=_5f8686714d769f9476ce778798b84ff4 

Content-Type: multipart/related; 
    boundary="=_e49477d06c604958b9b2bc038628a26f" 

--=_e49477d06c604958b9b2bc038628a26f 
Content-Type: text/html; charset="ISO-8859-1" 
Content-Transfer-Encoding: quoted-printable 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.= 
w3.org/TR/html4/loose.dtd"> 
<html style=3D"height: 100%"> 
<head>/head> 
<body></body> 
</html> 
--=_e49477d06c604958b9b2bc038628a26f 

我試圖與Django的EmailMessage(替代),蟒蛇MimeMultipart的,但我可以」沒有按照確切的順序。

回答

0

直接使用emailsmtplib模塊。

Django內置的電子郵件處理方法非常有限。

1

如果您嘗試發送電子郵件的純文本和HTML版本(其中純文本版本只是指URL),EmailMultiAlternatives類似乎對我來說工作正常。從文檔的示例的

略加修改:

from django.core.mail import EmailMultiAlternatives 

subject, from_email, to = 'hello', '[email protected]', '[email protected]' 
text_content = 'This e-mail is in HTML-format. Click the following link:\n\nhttp://www.company.nl/enews/7' 
html_content = '<p>This is an <strong>HTML-format</strong> version.</p>' 
msg = EmailMultiAlternatives(subject, text_content, from_email, [to]) 
msg.attach_alternative(html_content, "text/html") 
msg.send() 

結果在此電子郵件(使用console backend):

Content-Type: multipart/alternative; 
boundary="===============2514609180855632526==" 
MIME-Version: 1.0 
Subject: hello 
From: [email protected] 
To: [email protected] 
Date: Wed, 18 Jul 2012 10:31:22 -0000 
Message-ID: <[email protected]localdomain6> 

--===============2514609180855632526== 
Content-Type: text/plain; charset="utf-8" 
MIME-Version: 1.0 
Content-Transfer-Encoding: 7bit 

This e-mail is in HTML-format. Click the following link: 

http://www.company.nl/enews/7 
--===============2514609180855632526== 
Content-Type: text/html; charset="utf-8" 
MIME-Version: 1.0 
Content-Transfer-Encoding: 7bit 

<p>This is an <strong>HTML-format</strong> version.</p> 
--===============2514609180855632526==-- 

雖然它不具有multipart/related內容類型,它對純文本(顯示鏈接)和HTML客戶端(顯示HTML版本)都起作用。