如何在Python3中发送带风格的电子邮件? [英] How to send an email with style in Python3?
问题描述
smtplib
)。现在,我总是在Gmail帐户中收到邮件。但问题是我无法显示CSS样式,尽管是内联的。此外,即使这个简单的消息也不能发送: title ='我的标题'
msg_content ='&h2& ; {title}:< font color =green> OK< / font>< / h2> \\\
'.format(title = title)
但是,如果我删除{title}之后的两点,它可以工作。如果我删除\\\
最后它不会再工作。为什么?如何在Python3中发送这样的行?
title ='我的标题'
p $ p>
msg_content ='< h2> {title}:< span style =color:green> OK< / span>< / h2>'format(title = title)
编辑
import smtplib
msg_header ='From:发件人名称<发件人@服务器> \\\
'\
'收件人:接收者名称< receiver @ server> \\\
'\
'Cc:Receiver2 Name< receiver2 @ server> \\\
'\
'MIME-Version:1.0\\\
'\
'Content-type:text / html\\\
' \
'主题:任何主题\''
title ='我的标题'
msg_content ='< h2> {title}> < font color =green> OK< / font>< / h2> \\\
'.format(
title = title)
msg_full =(''.join([msg_header, msg_content]))。encode()
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(' sender@server.com','senderpassword')
server.sendmail(sender@server.com',
['receiver@server.com','receiver@server.com'],
msg_full)
server.quit()
解决方案
澄清后编辑
您的示例的msg_full结果如下所示:
code> From:发件人名称< sender @ server>
至:接收者名称< receiver @ server>
Cc:Receiver2 Name< receiver2 @ server>
MIME版本:1.0
内容类型:text / html
主题:任何主题
< h2>我的标题> < font color =green> OK< / font>< / h2>
您的电子邮件格式不符合 RFC 2822 :
- 您必须使用CRLF('\r\ n')作为换行符分隔符,LF(\\\
')只是非法的。 - 标题和正文必须由CRLF(即一行空行)分隔开。如果你做了一个
''。join([msg_header,msg_body])
,这不会插入这一行。因此,您希望以身体文本的形式发送,因此被视为标题。
同一封电子邮件的正确版本看起来像这个:
内容类型:text / html; charset =us-ascii
MIME版本:1.0
Content-Transfer-Encoding:7bit
From:发件人名称< sender @ server>
至:接收者名称< receiver @ server>
Cc:Receiver2 Name< receiver2 @ server>
主题:任何主题
< h2>我的标题> < font color =green> OK< / font>< / h2>
我强烈建议您使用Python的内建库来构建符合RFC的有效载荷。
import smtplib
from email.mime.text import MIMEText
title ='我的标题'
msg_content ='< h2> {title}> < font color =green> OK< / font>< / h2> \\\
'.format(title = title)
message = MIMEText(msg_content,'html')
message ['From'] ='发件人名称< sender @ server>'
message ['To'] ='接收者名称< receiver @ server>'
message ['Cc' ] ='Receiver2 Name< receiver2 @ server>'
message ['Subject'] ='任何主题'
msg_full = message.as_string()
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(sender@server.com','senderpassword')
服务器.sendmail(sender@server.com',
['receiver@server.com','receiver@server.com'],
msg_full)
server.quit()
此外,添加消息的文本/纯文本也是一种很好的形式,收件人可以在任何地方阅读(我的HTML邮箱被禁用,在我的客户端看不到任何)。您可以通过email.mime.text轻松实现:
from email.mime.multipart import MIMEMultipart
message = MIMEMultipart('alternative')
message ['From'] ='发件人名称< sender @ server>'
message ['To'] ='Receiver Name< receiver @ server> ;'
message ['Cc'] ='Receiver2 Name< receiver2 @ server>'
message ['Subject'] ='任何主题'
#记录MIME两个部分的类型 - text / plain和text / html。
part1 = MIMEText(text,'plain')
part2 = MIMEText(html,'html')
#将零件附加到消息容器中。
#根据RFC 2046,多部分消息的最后部分(在这种情况下为
#的HTML消息)是最好的和首选的。
message.attach(part1)
message.attach(part2)
以前的答案
您的问题缺少用于发送邮件的代码。我强烈怀疑您通过msg_content直接发送到 SMTP.sendmail 的消息。
SMTP.sendmail,但是按照原样发送此字符串,即根据 RFC 5321 。此有效载荷数据由邮件头和内容组成,标题位于邮件顶部(请参阅 RFC 2822 )
您的消息我的标题:我的标题:,而不是在接收端显示。如果您在 {title}:
之后删除冒号,则接收者显然不会将结果视为标题等。
对于HTML风格的邮件,请查看 https://docs.python.org/上的示例2 / library / email-examples.html - 基本上您必须创建一个适当的text / html MIME编码消息才能发送您的消息。
I am sending emails from a Python3 script (using smtplib
). By now, I am always receiving the messages in Gmail accounts. But the problem is I am not able to show CSS styles, in spite of being inline. Besides, even this simple message cannot be sent:
title = 'My title'
msg_content = '<h2>{title}: <font color="green">OK</font></h2>\n'.format(title=title)
However, if I remove the two points just after {title}, it works. And if I remove the \n at the end it will not work again. Why? How can I send a line like this in Python3?
title = 'My title'
msg_content = '<h2>{title}: <span style="color: green">OK</span></h2>'.format(title=title)
EDIT
import smtplib
msg_header = 'From: Sender Name <sender@server>\n' \
'To: Receiver Name <receiver@server>\n' \
'Cc: Receiver2 Name <receiver2@server>\n' \
'MIME-Version: 1.0\n' \
'Content-type: text/html\n' \
'Subject: Any subject\n'
title = 'My title'
msg_content = '<h2>{title} > <font color="green">OK</font></h2>\n'.format(
title=title)
msg_full = (''.join([msg_header, msg_content])).encode()
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login('sender@server.com', 'senderpassword')
server.sendmail('sender@server.com',
['receiver@server.com', 'receiver@server.com'],
msg_full)
server.quit()
Edit after clarification
The msg_full result of your example looks like this:
From: Sender Name <sender@server>
To: Receiver Name <receiver@server>
Cc: Receiver2 Name <receiver2@server>
MIME-Version: 1.0
Content-type: text/html
Subject: Any subject
<h2>My title > <font color="green">OK</font></h2>
Your E-Mail format is not conforming to RFC 2822:
- You must use CRLF ('\r\n') as newline separators, LF (\n') only is illegal
- Headers and body must be separated by a CRLF (i.e. one empty line). If you do a
''.join([msg_header, msg_body])
, this does not insert this line. What you want to be transmitted as the body text is therefore treated as a header.
A correct version of the same email would look like this:
Content-Type: text/html; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
From: Sender Name <sender@server>
To: Receiver Name <receiver@server>
Cc: Receiver2 Name <receiver2@server>
Subject: Any subject
<h2>My title > <font color="green">OK</font></h2>
I strongly encourage you to use Python's built in libraries for building RFC conformant payloads.
import smtplib
from email.mime.text import MIMEText
title = 'My title'
msg_content = '<h2>{title} > <font color="green">OK</font></h2>\n'.format(title=title)
message = MIMEText(msg_content, 'html')
message['From'] = 'Sender Name <sender@server>'
message['To'] = 'Receiver Name <receiver@server>'
message['Cc'] = 'Receiver2 Name <receiver2@server>'
message['Subject'] = 'Any subject'
msg_full = message.as_string()
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login('sender@server.com', 'senderpassword')
server.sendmail('sender@server.com',
['receiver@server.com', 'receiver@server.com'],
msg_full)
server.quit()
In addition, it is good form to add a text/plain version of your message as well, so that any receipient can read it anywhere (I have HTML mail disabled and don't see any of that on my client). You can do that easily with email.mime.text:
from email.mime.multipart import MIMEMultipart
message = MIMEMultipart('alternative')
message['From'] = 'Sender Name <sender@server>'
message['To'] = 'Receiver Name <receiver@server>'
message['Cc'] = 'Receiver2 Name <receiver2@server>'
message['Subject'] = 'Any subject'
# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
message.attach(part1)
message.attach(part2)
Previous Answer
Your question lacks the code you are using to send the mail. I am strongly suspecting you pass msg_content directly as the message to SMTP.sendmail.
SMTP.sendmail, however transmits this string as-is, i.e. as the payload of the mail according to RFC 5321. This payload-data consists of email headers and content, with headers at the top of the message (see RFC 2822).
Your message "My title: My title:" and not shown at the receiving end. If you remove the colon after {title}:
, then the receiver obviously does not treat the result as a header, etc.
For HTML styled mail, look at the examples at https://docs.python.org/2/library/email-examples.html - basically you must create a proper text/html MIME encoded message in order to send your message.
这篇关于如何在Python3中发送带风格的电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!