2013-10-23 106 views
2

我使用Django的send_mail是這樣的:Python Django send_mail換行符?

from django.core.mail import send_mail 

send_mail('Test', 'asdfasdfasdf\nasdfasfasdfasdf\nasdfasdfasdf', '[email protected]', ['[email protected]'], fail_silently=False) 

的Gmail臨危這一點。

Content-type: multipart/alternative; boundary="----------=_1382512224-21042-56" 
------------=_1382512224-21042-56 
Content-Type: text/plain; charset="utf-8" 
Content-Transfer-Encoding: 7bit 

asdfasdfasdf 
asdfasfasdfasdf 
asdfasdfasdf 

------------=_1382512224-21042-56 
Content-Type: text/html; charset="utf-8" 
Content-Disposition: inline 
Content-Transfer-Encoding: 7bit 

<html><body> 
<p>asdfasdfasdf asdfasfasdfasdf asdfasdfasdf</p> 
</body></html> 

並將我的換行符顯示爲一個整段。爲什麼?我想要三行文字,而不是一行。

+0

一個解決辦法是發送純/文我想.. – heffaklump

+0

更改內容類型爲text/plain或者使用
,而不是\ n –

+0


標籤成了這個樣子?

asdfasdfasdf < br/> asdfasfasdfasdf < br/> asdfasdfasdf

heffaklump

回答

9

嘗試以HTML格式而不是純文本形式發送您的電子郵件。使用EmailMessage()

from django.core.mail import EmailMessage 

msg = EmailMessage(
         'Test', 
         'asdfasdfasdf<br>asdfasfasdfasdf<br>asdfasdfasdf', 
         '[email protected]', 
         ['[email protected]', ] 
       ) 
msg.content_subtype = "html" 
msg.send() 
1

如果你想在mutlipart電子郵件的不同組成部分的控制,你可以創建你創建了一個EmailMultiAlternatives然後.send()電子郵件。

Django的從documentation exmample。

from django.core.mail import EmailMultiAlternatives 

subject, from_email, to = 'hello', '[email protected]', '[email protected]' 
text_content = 'This is an important message.' 
html_content = '<p>This is an <strong>important</strong> message.</p>' 
msg = EmailMultiAlternatives(subject, text_content, from_email, [to]) 
msg.attach_alternative(html_content, "text/html") 
msg.send()