PythonDjango发邮件换行?

2024-05-19 12:35:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我用的是django send_邮件,如下所示:

from django.core.mail import send_mail

send_mail('Test', 'asdfasdfasdf\nasdfasfasdfasdf\nasdfasdfasdf', 'sender@test.com', ['receiver@test.com'], fail_silently=False)

Gmail收到这个。在

^{pr2}$

并将换行显示为一个完整的段落。为什么?我想要三行文字,不是一行。在


Tags: djangofromcoretestimportcomsend邮件
2条回答

试着用HTML格式而不是纯文本格式发送电子邮件。使用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()

如果您想控制mutlipart电子邮件的不同组件,可以创建一个EmailMultiAlternatives,然后.send()创建的电子邮件。在

Django的exmample来自documentation。在

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()

相关问题 更多 >