如何在使用 Python 的 smtplib 发送的电子邮件中获得换行符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14139165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get line breaks in e-mail sent using Python's smtplib?
提问by f.ashouri
I have written a script that writes a message to a text file and also sends it as an email. Everything goes well, except the email finally appears to be all in one line.
我编写了一个脚本,将消息写入文本文件并将其作为电子邮件发送。一切都很顺利,除了电子邮件最终似乎都在一行中。
I add line breaks by \nand it works for the text file but not for the email.
Do you know what could be the possible reason?
我添加了换行符\n,它适用于文本文件,但不适用于电子邮件。你知道可能的原因是什么吗?
Here's my code:
这是我的代码:
import smtplib, sys
import traceback
def send_error(sender, recipient, headers, body):
SMTP_SERVER = 'smtp.gmail.com'
SMTP_PORT = 587
session = smtplib.SMTP('smtp.gmail.com', 587)
session.ehlo()
session.starttls()
session.ehlo
session.login(sender, 'my password')
send_it = session.sendmail(sender, recipient, headers + "\r\n\r\n" + body)
session.quit()
return send_it
SMTP_SERVER = 'smtp.gmail.com'
SMTP_PORT = 587
sender = '[email protected]'
recipient = '[email protected]'
subject = 'report'
body = "Dear Student, \n Please send your report\n Thank you for your attention"
open('student.txt', 'w').write(body)
headers = ["From: " + sender,
"Subject: " + subject,
"To: " + recipient,
"MIME-Version: 1.0",
"Content-Type: text/html"]
headers = "\r\n".join(headers)
send_error(sender, recipient, headers, body)
采纳答案by moooeeeep
You have your message body declared to have HTML content ("Content-Type: text/html"). The HTML code for line break is <br>. You should either change your content type to text/plainor use the HTML markup for line breaks instead of plain \nas the latter gets ignored when rendering a HTML document.
您已将消息正文声明为包含 HTML 内容 ( "Content-Type: text/html")。换行符的 HTML 代码是<br>. 您应该将内容类型更改为text/plain或使用 HTML 标记作为换行符而不是普通标记,\n因为后者在呈现 HTML 文档时会被忽略。
As a side note, also have a look at the email package. There are some classes that can simplify the definition of E-Mail messages for you (with examples).
作为旁注,还可以查看电子邮件包。有一些类可以为您简化电子邮件消息的定义(带有示例)。
For example you could try (untested):
例如,您可以尝试(未经测试):
import smtplib
from email.mime.text import MIMEText
# define content
recipients = ["[email protected]"]
sender = "[email protected]"
subject = "report reminder"
body = """
Dear Student,
Please send your report
Thank you for your attention
"""
# make up message
msg = MIMEText(body)
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = ", ".join(recipients)
# sending
session = smtplib.SMTP('smtp.gmail.com', 587)
session.starttls()
session.login(sender, 'my password')
send_it = session.sendmail(sender, recipients, msg.as_string())
session.quit()
回答by Inbar Rose
Unfortunately for us all, not every type of program or application uses the same standardization that python does.
对我们所有人来说不幸的是,并非每种类型的程序或应用程序都使用与 python 相同的标准化。
Looking at your question i notice your header is: "Content-Type: text/html"
看着你的问题,我注意到你的标题是: "Content-Type: text/html"
Which means you need to use HTML style tags for your new-lines, these are called line-breaks. <br>
这意味着您需要为换行符使用 HTML 样式标签,这些标签称为换行符。 <br>
Your text should be:
你的文字应该是:
"Dear Student, <br> Please send your report<br> Thank you for your attention"
If you would rather use character type new-lines, you must change the header to read: "Content-Type: text/plain"
如果您更愿意使用字符类型换行符,则必须将标题更改为: "Content-Type: text/plain"
You would still have to change the new-line character from a single \nto the double \r\nwhich is used in email.
您仍然需要将换行符从单字符更改为电子邮件中使用\n的双字符\r\n。
Your text would be:
你的文字是:
"Dear Student, \r\n Please send your report\r\n Thank you for your attention"
回答by chandradog
Setting the content-type header to Content-Type: text/plain(with \r\nat the end) allowed me to send multi-line plain-text emails.
将 content-type 标头设置为Content-Type: text/plain(\r\n在最后)允许我发送多行纯文本电子邮件。
回答by edW
Outlook will remove line feeds from plain text it believes are extras. https://support.microsoft.com/en-us/kb/287816
Outlook 将从它认为是额外内容的纯文本中删除换行符。 https://support.microsoft.com/en-us/kb/287816
You can try below update to make the lines look like bullets. That worked for me.
您可以尝试下面的更新,使线条看起来像子弹。那对我有用。
body = "Dear Student, \n- Please send your report\n- Thank you for your attention"
回答by Shepherd
In my case '\r\n'didn't work, but '\r\r\n'did. So my code was:
在我的情况下'\r\n'没有用,但'\r\r\n'确实有效。所以我的代码是:
from email.mime.text import MIMEText
body = 'Dear Student,\r\r\nPlease send your report\r\r\nThank you for your attention'
msg.attach(MIMEText(body, 'plain'))
The message is written in multiple lines and is displayed correctly in Outlook.
该消息以多行书写,并在 Outlook 中正确显示。

