Python - 发送有编码问题的电子邮件(撇号)

Python - Sending email with encoding problem (apostrophe)

我一直在查看关于这个问题的已回答问题,但不幸的是找不到适合我的答案...这是我的代码...通过 Python 发送电子邮件时, 由于编码错误,撇号无法正常工作。我错过了什么

所有内容都通过电子邮件正确发送,但撇号显示如下:’s

我尝试过不同的字符集设置,但目前找不到解决方案。任何帮助将不胜感激。

#---------------------------------------------------------------------------------------------------------------------------
#HTML CONTENT
#---------------------------------------------------------------------------------------------------------------------------

text = """

"""

html = """

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
</head>
<body>

<p style="border-bottom: 2px solid #00874E; width:100%; text-align:left; padding-top:10px; color:#3f3f3f"><font size="3"><b>SIGNIFICANT INSIDER TRANSACTIONS</b></font></p>

<table style="width:100%">
  <tr>
    <td style="font-size:11px; text-align: justify">"""+header_insiders+"""</td>
  </tr>
  <tr>
    <td style="font-size:11px"><b>Dollar Value Buying (000's)</b></td>
  </tr>
  <tr>
    <td style="font-size:11px; text-align: justify">"""+dollar_buying_combine+"""</td>
  </tr>
  <tr>
    <td style="font-size:11px"><b>Dollar Value Selling (000's)</b></td>
  </tr>
  <tr>
    <td style="font-size:11px; text-align: justify">"""+dollar_selling_combine+"""</td>
  </tr>
</table>

<p style="border-bottom: 2px solid #00874E; width:100%; text-align:left; padding-top:10px; color:#3f3f3f"><font size="3"><b>PRESS HEADLINES</b></font></p>
<table style="width:100%">
  <tr>
    <td><b>Bloomberg</b></td>
  </tr>
    <tr>
    <td>""" + bloomberg + """</td>
  </tr>
    <tr>
    <td><b>The Globe & Mail</b></td>
  </tr>
    <tr>
    <td>""" + globe + """</td>
  </tr>
    <tr>
    <td><b>Reuters</b></td>
  </tr>
  <tr>
    <td>""" + reuters + """</td>
  </tr>
  <tr>
    <td><b>The Wall Street Journal</b></td>
  </tr>
  <tr>
    <td>""" + WSJ + """</td>
  </tr>
  <tr>
    <td><b>Financial Times</b></td>
  </tr>
  <tr>
    <td>""" + FT + """</td>
  </tr>
  <tr>
    <td><b>Financial Post</b></td>
  </tr>
  <tr>
    <td>""" + FP + """</td>
  </tr>
</table>

</body></html>
"""

#---------------------------------------------------------------------------------------------------------------------------
#SEND EMAIL
#---------------------------------------------------------------------------------------------------------------------------


message = MIMEMultipart(
    "alternative", None, [MIMEText(text), MIMEText(html.encode('utf-8'), 'html','utf-8')])

message['Subject'] = "Morning Note"
message['From'] = me
message['To'] = you

server = smtplib.SMTP(server)
server.ehlo()
server.starttls()
server.login(me, password)
server.sendmail(me, you, message.as_string())
server.quit()

听起来像是 html 编码(而不是 mime 编码)问题。我不熟悉 MIMEMultipart 的所有参数。一种可能的解决方法是将所有撇号替换为 &#39;,即 HTML 编码的等价物。

我有点暴力破解...我添加了第一行如下:

#---------------------------------------------------------------------------------------------------------------------------
#SEND EMAIL
#---------------------------------------------------------------------------------------------------------------------------
html = html.replace("’","'").replace("‘","'").replace("—","-")
message = MIMEText(html, "html")

message['Subject'] = "Morning Note"
message['From'] = me
message['To'] = you

server = smtplib.SMTP(server)
server.ehlo()
server.starttls()
server.login(me, password)
server.sendmail(me, you, message.as_string())
server.quit()