【问题标题】:Send HTML Mail with Unicode使用 Unicode 发送 HTML 邮件
【发布时间】:2016-07-23 17:02:06
【问题描述】:

我修改了 python 文档中的示例,以测试电子邮件模块中的 unicode。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, unicode_literals, print_function

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"

umlauts='German Umlauts: üöä ÜÖÄ ß'

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

# Create the body of the message (a plain-text and an HTML version).
text = umlauts
html = """\
<html>
  <head></head>
  <body>
    <p>Hi!<br>
       %s
    </p>
  </body>
</html>
""" % umlauts

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

来源:https://docs.python.org/2/library/email-examples.html#id4

我得到了这个例外:

user@pc:~$ python src/sendhtmlmail.py 
Traceback (most recent call last):
  File "src/sendhtmlmail.py", line 37, in <module>
    part1 = MIMEText(text, 'plain')
  File "/usr/lib/python2.7/email/mime/text.py", line 30, in __init__
    self.set_payload(_text, _charset)
  File "/usr/lib/python2.7/email/message.py", line 226, in set_payload
    self.set_charset(charset)
  File "/usr/lib/python2.7/email/message.py", line 262, in set_charset
    self._payload = self._payload.encode(charset.output_charset)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 16-18: ordinal not in range(128)

如果要发送text+html邮件,如何处理unicode?​​p>

【问题讨论】:

    标签: python python-2.7 email unicode


    【解决方案1】:

    您需要将其显式编码为 UTF-8。

    part1 = MIMEText(text.encode('utf-8'), 'plain', 'utf-8')
    part2 = MIMEText(html.encode('utf-8'), 'html', 'utf-8')
    

    或者,避免导入 unicode_literals,你的字符串将首先是字节。

    【讨论】:

    • 我在所有文件中使用 unicode_literals 未来导入,因为 Python3 的更新迟早会发生。在真实代码中,字符来自 db,而不是来自 python 源文件:-)
    • 问题发生在使用 Flask 的 Python 3 上。有什么建议吗?
    【解决方案2】:

    您尝试使用 ASCII 发送电子邮件,但您将 Unicode 字符传递给电子邮件。

    具体检查: https://docs.python.org/2/library/email.header.html

    编码示例而言,我将公然将您链接到以下网站。

    http://www.askthepony.com/blog/2011/06/how-to-send-a-proper-unicode-encoded-email-using-python-2-7/

    【讨论】:

    • 最后一个示例链接是 404。当然它可能在过去可用,但现在明显不再可用。
    猜你喜欢
    • 2012-02-24
    • 1970-01-01
    • 2010-11-24
    • 1970-01-01
    • 2018-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多