[英]Python Django send_mail newlines?
I'm using django send_mail like this:我正在像这样使用 django send_mail:
from django.core.mail import send_mail
send_mail('Test', 'asdfasdfasdf\nasdfasfasdfasdf\nasdfasdfasdf', 'sender@test.com', ['receiver@test.com'], fail_silently=False)
Gmail recieves this. 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>
And shows my newlines as one whole paragraph.并将我的换行符显示为一个完整的段落。 Why?为什么? I would like three lines of text, not one.我想要三行文字,而不是一行。
Try sending your email as HTML instead of plain text.尝试以 HTML 格式而不是纯文本格式发送电子邮件。 Use EmailMessage() .使用EmailMessage() 。
from django.core.mail import EmailMessage
msg = EmailMessage(
'Test',
'asdfasdfasdf<br>asdfasfasdfasdf<br>asdfasdfasdf',
'sender@example.com',
['receiver@example.com', ]
)
msg.content_subtype = "html"
msg.send()
If you want control over the different components of the mutlipart e-mail, you can create a EmailMultiAlternatives
and then .send()
the email message you created.如果您想控制多部分电子邮件的不同组件,您可以创建一个EmailMultiAlternatives
,然后.send()
您创建的电子邮件。
Django's exmample from the documentation . 文档中的Django 示例。
from django.core.mail import EmailMultiAlternatives
subject, from_email, to = 'hello', 'from@example.com', 'to@example.com'
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.