Search code examples
pythonhtmlemailapostrophe

Python - Sending email with encoding problem (apostrophe)


I've been looking through the answered questions on this one, but can't find an answer that works for me unfortunately... Here is my code... When sending an email through Python, the apostrophe does not work properly based on an encoding error. What am I missing

All of the content is sent by email properly, but the apostrophe show up as followed: ’s

I've tried with different charset settings but I can't find a solution for now. Any help would be appreciated.

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

Solution

  • I kind of brute-forced it... I added the first line as follows:

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