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

时间:2018-09-04 02:31:56

标签: python html email apostrophe

我一直在浏览有关此问题的解答,但很遗憾,找不到适合我的答案...这是我的代码...通过Python发送电子邮件时,撇号无效正确地基于编码错误。我想念什么

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

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

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

我有点蛮力...我在第一行中添加了以下内容:

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