用Python发送HTML电子邮件

351

如何使用Python发送带有HTML内容的电子邮件?我能够发送简单的文本。


2
只是一个大而明显的警告。如果你正在使用 Python < 3.0 发送非 ASCII 邮件,请考虑使用 Django 中的电子邮件。它可以正确地包装 UTF-8 字符串,并且使用起来也更简单。你已经被警告了 :-) - Anders Rune Jensen
3
如果您想发送带有Unicode的HTML,请参见此处:http://stackoverflow.com/questions/36397827/send-html-mail-with-unicode - guettli
12个回答

536

Python v2.7.14文档-18.1.11.电子邮件:示例中获取:

这是一个创建包含可替换纯文本版本的HTML消息的示例:

#! /usr/bin/python

import smtplib

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

# me == my email address
# you == recipient's email address
me = "my@email.com"
you = "your@email.com"

# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = "Link"
msg['From'] = me
msg['To'] = you

# Create the body of the message (a plain-text and an HTML version).
text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org"
html = """\
<html>
  <head></head>
  <body>
    <p>Hi!<br>
       How are you?<br>
       Here is the <a href="http://www.python.org">link</a> you wanted.
    </p>
  </body>
</html>
"""

# 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.
msg.attach(part1)
msg.attach(part2)

# Send the message via local SMTP server.
s = smtplib.SMTP('localhost')
# sendmail function takes 3 arguments: sender's address, recipient's address
# and message to send - here it is sent as one string.
s.sendmail(me, you, msg.as_string())
s.quit()

2
能否附加第三和第四部分,它们都是附件(一个ASCII,一个二进制)?如何操作?谢谢。 - Hamish Grubijan
2
嗨,我注意到你在最后退出了s对象。如果我想发送多条消息怎么办?我是每次发送消息都要退出还是全部发送(在for循环中)然后一次性退出? - xpanta
5
确保将HTML最后附加,因为首选(显示)部分将是最后附加的部分。根据RFC 2046,多部分消息的最后一部分,即HTML消息,在这种情况下是最佳且首选的。我希望我2小时前就读了这个。 - dwkd
2
警告:如果文本中包含非 ASCII 字符,则此操作将失败。 - guettli
9
msg.as_string() 出现错误:list对象没有 encode 属性。 - WJA
显示剩余9条评论

82

以下是已被接受的答案的Gmail版本实现:Gmail

import smtplib

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

# me == my email address
# you == recipient's email address
me = "my@email.com"
you = "your@email.com"

# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = "Link"
msg['From'] = me
msg['To'] = you

# Create the body of the message (a plain-text and an HTML version).
text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org"
html = """\
<html>
  <head></head>
  <body>
    <p>Hi!<br>
       How are you?<br>
       Here is the <a href="http://www.python.org">link</a> you wanted.
    </p>
  </body>
</html>
"""

# 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.
msg.attach(part1)
msg.attach(part2)
# Send the message via local SMTP server.
mail = smtplib.SMTP('smtp.gmail.com', 587)

mail.ehlo()

mail.starttls()

mail.login('userName', 'password')
mail.sendmail(me, you, msg.as_string())
mail.quit()

5
很棒的代码,对我有用,如果我打开了谷歌低安全性设置 - Tovask
28
我使用谷歌的应用程序特定密码和Python的smtplib,这样就不必降低安全性也能实现目标。 - yoyo
3
对于阅读上述评论的任何人:只有在您之前在Gmail帐户中启用了两步验证时,才需要“应用程序密码”。 - Mugen
有没有一种方法可以动态地在消息的HTML部分附加一些内容? - magma
不知何故,只有最后附加的部分似乎起作用了。 - Zobayer Hasan

72

您可以尝试使用我的mailer模块。

from mailer import Mailer
from mailer import Message

message = Message(From="me@example.com",
                  To="you@example.com")
message.Subject = "An HTML Email"
message.Html = """<p>Hi!<br>
   How are you?<br>
   Here is the <a href="http://www.python.org">link</a> you wanted.</p>"""

sender = Mailer('smtp.example.com')
sender.send(message)

Mailer模块很棒,但声称可以与Gmail一起使用,实际上却不能,并且没有文档。 - MFB
2
@MFB -- 你试过Bitbucket仓库了吗?https://bitbucket.org/ginstrom/mailer/ - Ryan Ginstrom
5
在初始化“Mailer”时,如果要使用 Gmail,则应提供use_tls=Trueusr='email'pwd='password',它就能正常工作了。请注意,这里的“邮箱”和“密码”指的是您的 Gmail 邮箱地址和密码。 - ToonAlfrink
我建议在message.Html行后面添加以下代码行:message.Body = """当客户端无法显示HTML电子邮件时,显示一些文本""" - IvanD
很好,但如何将变量值添加到链接中,我的意思是创建这样的链接<a href="http://www.python.org/somevalues">link</a>,以便我可以从它所到达的路由中访问这些值。谢谢。 - TaraGurung

65

以下是一种简单的方法,可以通过指定Content-Type头为“text/html”来发送HTML电子邮件:

import email.message
import smtplib

msg = email.message.Message()
msg['Subject'] = 'foo'
msg['From'] = 'sender@test.com'
msg['To'] = 'recipient@test.com'
msg.add_header('Content-Type','text/html')
msg.set_payload('Body of <b>message</b>')

# Send the message via local SMTP server.
s = smtplib.SMTP('localhost')
s.starttls()
s.login(email_login,
        email_passwd)
s.sendmail(msg['From'], [msg['To']], msg.as_string())
s.quit()

3
这是一个不错的简单答案,适用于快速且简单的脚本,谢谢。顺便说一下,人们可以参考被接受的答案来获取一个简单的smtplib.SMTP()示例,它不使用tls。我在工作中使用了这个内部脚本,我们使用ssmtp和本地邮件集线器。此外,这个示例缺少s.quit() - Mike S
2
"mailmerge_conf.smtp_server" 未定义... 至少 Python 3.6 是这样说的... - ZEE
太棒了!对我来说,添加.add_header并使用.set_payload而不是.set_content非常有效!现在的电子邮件看起来很漂亮。再次感谢! - MegaBytten

57

针对Python3,改进@taltman的答案:

  • 使用email.message.EmailMessage构造邮件,而不是email.message.Message
  • 使用email.set_content方法,分配subtype='html'参数,而不是使用底层方法set_payload并手动添加头文件。
  • 使用SMTP.send_message方法发送邮件,而不是SMTP.sendmail函数。
  • 使用with块自动关闭连接。
from email.message import EmailMessage
from smtplib import SMTP

# construct email
email = EmailMessage()
email['Subject'] = 'foo'
email['From'] = 'sender@test.com'
email['To'] = 'recipient@test.com'
email.set_content('<font color="red">red color text</font>', subtype='html')

# Send the message via local SMTP server.
with smtplib.SMTP('localhost') as s:
    s.login('foo_user', 'bar_password')
    s.send_message(email)

4
如果您想附加发送一个附件,可以使用email.add_alternative()(与使用email.set_content()添加HTML的方法相同),然后使用email.add_attachment()添加附件来改进此功能(这让我花费了很长时间才弄清楚)。 - Zilbert97
3
EmailMessage API只在Python 3.6及以上版本中正式提供,尽管在3.3版本中已经作为替代方法出现。新代码应该绝对使用这个API而不是旧的email.message.Message遗留API,大部分答案中都还在建议使用旧API。任何涉及到MIMETextMIMEMultipart的部分都是旧API,除非你有遗留原因,否则应该避免使用。 - tripleee
有没有办法在email.set context中使用文件而不是手动输入HTML? - Michael
我找到了添加HTML文件的方法。report_file = open('resetpasswordemail.html') html = report_file.read() em.set_content(html, subtype='html') - Michael

12

这是一段示例代码,灵感来源于Python Cookbook网站上的代码(找不到确切链接)。

def createhtmlmail (html, text, subject, fromEmail):
    """Create a mime-message that will render HTML in popular
    MUAs, text in better ones"""
    import MimeWriter
    import mimetools
    import cStringIO

    out = cStringIO.StringIO() # output buffer for our message 
    htmlin = cStringIO.StringIO(html)
    txtin = cStringIO.StringIO(text)

    writer = MimeWriter.MimeWriter(out)
    #
    # set up some basic headers... we put subject here
    # because smtplib.sendmail expects it to be in the
    # message body
    #
    writer.addheader("From", fromEmail)
    writer.addheader("Subject", subject)
    writer.addheader("MIME-Version", "1.0")
    #
    # start the multipart section of the message
    # multipart/alternative seems to work better
    # on some MUAs than multipart/mixed
    #
    writer.startmultipartbody("alternative")
    writer.flushheaders()
    #
    # the plain text section
    #
    subpart = writer.nextpart()
    subpart.addheader("Content-Transfer-Encoding", "quoted-printable")
    pout = subpart.startbody("text/plain", [("charset", 'us-ascii')])
    mimetools.encode(txtin, pout, 'quoted-printable')
    txtin.close()
    #
    # start the html subpart of the message
    #
    subpart = writer.nextpart()
    subpart.addheader("Content-Transfer-Encoding", "quoted-printable")
    #
    # returns us a file-ish object we can write to
    #
    pout = subpart.startbody("text/html", [("charset", 'us-ascii')])
    mimetools.encode(htmlin, pout, 'quoted-printable')
    htmlin.close()
    #
    # Now that we're done, close our writer and
    # return the message body
    #
    writer.lastpart()
    msg = out.getvalue()
    out.close()
    print msg
    return msg

if __name__=="__main__":
    import smtplib
    html = 'html version'
    text = 'TEST VERSION'
    subject = "BACKUP REPORT"
    message = createhtmlmail(html, text, subject, 'From Host <sender@host.com>')
    server = smtplib.SMTP("smtp_server_address","smtp_port")
    server.login('username', 'password')
    server.sendmail('sender@host.com', 'target@otherhost.com', message)
    server.quit()

FYI;这段内容来自http://code.activestate.com/recipes/67083-send-html-mail-from-python/history/1/ - Dale E. Moore
2
这比email.message.Message API更古老、更杂乱。MimeWriter库在2003年Python 2.3中已被弃用。 - tripleee

7

实际上,yagmail 采取了一种略微不同的方法。

它将默认发送HTML格式的邮件,并自动为不支持HTML格式的邮件客户端提供回退方案。现在已经不是17世纪了。

当然,这个设置可以被覆盖,但以下是默认设置:

import yagmail
yag = yagmail.SMTP("me@example.com", "mypassword")

html_msg = """<p>Hi!<br>
              How are you?<br>
              Here is the <a href="http://www.python.org">link</a> you wanted.</p>"""

yag.send("to@example.com", "the subject", html_msg)

如需安装说明和更多功能,请查看github


仅适用于Gmail用户,有帮助。 - tedioustortoise

5
以下是使用smtplib在Python中发送纯文本和HTML电子邮件的工作示例,还包括CC和BCC选项。
参考链接:https://varunver.wordpress.com/2017/01/26/python-smtplib-send-plaintext-and-html-emails/
#!/usr/bin/env python
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

def send_mail(params, type_):
      email_subject = params['email_subject']
      email_from = "from_email@domain.com"
      email_to = params['email_to']
      email_cc = params.get('email_cc')
      email_bcc = params.get('email_bcc')
      email_body = params['email_body']

      msg = MIMEMultipart('alternative')
      msg['To'] = email_to
      msg['CC'] = email_cc
      msg['Subject'] = email_subject
      mt_html = MIMEText(email_body, type_)
      msg.attach(mt_html)

      server = smtplib.SMTP('YOUR_MAIL_SERVER.DOMAIN.COM')
      server.set_debuglevel(1)
      toaddrs = [email_to] + [email_cc] + [email_bcc]
      server.sendmail(email_from, toaddrs, msg.as_string())
      server.quit()

# Calling the mailer functions
params = {
    'email_to': 'to_email@domain.com',
    'email_cc': 'cc_email@domain.com',
    'email_bcc': 'bcc_email@domain.com',
    'email_subject': 'Test message from python library',
    'email_body': '<h1>Hello World</h1>'
}
for t in ['plain', 'html']:
    send_mail(params, t)

我认为这个答案涵盖了所有内容。非常好的链接。 - stingMantis

3
我可能回答晚了,但问题是如何发送HTML电子邮件。使用像“email”这样的专用模块是可以的,但我们可以在不使用任何新模块的情况下实现相同的结果。这完全取决于Gmail协议。
以下是我使用“smtplib”而没有其他内容发送HTML邮件的简单示例代码。
```
import smtplib

FROM = "....@gmail.com"
TO = "another....@gmail.com"
SUBJECT= "Subject"
PWD = "thesecretkey"

TEXT="""
<h1>Hello</h1>
""" #Your Message (Even Supports HTML Directly)

message = f"Subject: {SUBJECT}\nFrom: {FROM}\nTo: {TO}\nContent-Type: text/html\n\n{TEXT}" #This is where the stuff happens

try:
    server=smtplib.SMTP("smtp.gmail.com",587)
    server.ehlo()
    server.starttls()
    server.login(FROM,PWD)
    server.sendmail(FROM,TO,message)
    server.close()
    print("Successfully sent the mail.")
except Exception as e:
    print("Failed to send the mail..", e)
```

email 库不是一个“新模块”,它与 smtplib 一样是标准库的一部分。您绝对不应该像这样组合字符串来创建消息;虽然对于非常简单的消息似乎可以工作,但一旦您离开了仅包含7位US-ASCII内容的单个 text/plain 负载的舒适领域,就会以灾难性的方式失败(即使在这种情况下,也有可能会遇到一些棘手的问题,特别是如果您不知道自己在做什么)。 - tripleee

2

在Office 365中使用组织账户发送电子邮件的最简解决方案:

from O365 import Message

html_template =     """ 
            <html>
            <head>
                <title></title>
            </head>
            <body>
                    {}
            </body>
            </html>
        """

final_html_data = html_template.format(df.to_html(index=False))

o365_auth = ('sender_username@company_email.com','Password')
m = Message(auth=o365_auth)
m.setRecipients('receiver_username@company_email.com')
m.setSubject('Weekly report')
m.setBodyHTML(final_html_data)
m.sendMessage()

这里的 df 是一个转换为 HTML 表格的数据帧,正在被注入到 html_template 中。


3
这个问题没有提到使用Office或组织账户的事情。很好的贡献,但对提问者没有太大帮助。 - Mwikala Kangwa

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接