2013-10-23 58 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()