如何在Python3中发送带风格的电子邮件?

时间:2014-06-06 08:23:24

标签: python python-3.x email mime

我正在发送来自Python3脚本的电子邮件(使用smtplib)。到目前为止,我一直在Gmail帐户中收到邮件。但问题是我无法显示CSS样式,尽管是内联的。此外,即使这个简单的消息也无法发送:

title = 'My title'
msg_content = '<h2>{title}: <font color="green">OK</font></h2>\n'.format(title=title)

但是,如果我在{title}之后删除这两个点,它就可以了。如果我删除最后的\ n它将无法再次工作。为什么?如何在Python3中发送这样的一行?

title = 'My title'
msg_content = '<h2>{title}: <span style="color: green">OK</span></h2>'.format(title=title)

修改

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

2 个答案:

答案 0 :(得分:17)

澄清后编辑

您的示例的msg_full结果如下所示:

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>

您的电子邮件格式不符合RFC 2822

  • 您必须使用CRLF('\ r \ n')作为换行符分隔符,LF(\ n')只是非法的
  • 标题和正文必须用CRLF(即一个空行)分隔。如果您执行''.join([msg_header, msg_body]),则不会插入此行。因此,您希望传输的正文文本被视为标题。

同一封电子邮件的正确版本如下所示:

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>

我强烈建议您使用Python的内置库来构建符合RFC的有效负载。

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

此外,添加邮件的文本/纯文本版本也是一种很好的形式,这样任何收件人都可以在任何地方阅读它(我禁用了HTML邮件,并且在我的客户端上看不到任何内容)。您可以使用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)

上一个答案

您的问题缺少用于发送邮件的代码。我强烈怀疑你直接将msg_content作为消息传递给SMTP.sendmail

然而,

SMTP.sendmail按原样发送此字符串,即根据RFC 5321发送邮件的有效负载。此有效内容数据由电子邮件标头和内容组成,邮件顶部有标题(请参阅RFC 2822)。

您的消息“我的标题:我的标题:”并且未在接收端显示。如果你在{title}:之后删除冒号,那么接收器显然不会将结果视为标题等。

对于HTML样式的邮件,请查看https://docs.python.org/2/library/email-examples.html处的示例 - 基本上您必须创建正确的text / html MIME编码邮件才能发送邮件。

答案 1 :(得分:0)

迟到的答案,但有 2021,这是我用 python 发送电子邮件的方法:

import smtplib
from email.mime.text import MIMEText

sender = 'sender@tld'
receivers = ['receiver1@tld', 'receiver2@tld']
body_of_email = 'The body of the email'
msg = MIMEText(body_of_email, 'plain')
# html_body_of_email = '<h1>The html body of the email</h1>'
# msg = MIMEText(html_body_of_email, 'html')
msg['Subject'] = 'The Subject'
msg['From'] = sender
msg['To'] = ','.join(receivers)

s = smtplib.SMTP_SSL(host = 'smtp.gmail.com', port = 465)
s.login(user = 'username', password = 'password')
s.sendmail(sender, receivers, msg.as_string())
s.quit()

在使用 gmail 发送电子邮件之前,请记住在您的 Google 帐户上启用 less secure apps

  1. 转到您的 (Google Account)。
  2. 在左侧导航面板上,点击 Security
  3. 在页面底部的“不太安全的应用访问”面板中, 点击Turn on access
  4. 点击保存按钮。