0

Please find below relevant code snippet :

def send_mail_cloned():
    COMMASPACE = ', '
    sender='xxx@xyz.com'
    send_open_mail_to='xxx@xyz.com'
    # Create the container (outer) email message.
    msg = MIMEMultipart()
    msg.set_charset("utf-8")
    msg['Subject'] = 'Test'
    msg['From'] = sender
    msg['To'] = COMMASPACE.join(send_open_mail_to)

    text="""
    <html>
    <head>
       <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
       <title>html title</title>
       <style type="text/css" media="screen">
           table{
                 background-color: #AAD373;
                 empty-cells:hide;
           }
           td.cell{
                 background-color: white;
           }
       </style>
     </head>
     <body>"""

    text+="""<p><BR>Hi, <BR>Please find below table for Cloned JIRA's:<BR></p>
         <table style="border: blue 1px solid;">

             <tr><td class="cell">JIRA Link</td><td class="cell">PRISM URL</td><td class="cell">CR Title</td></tr>"""
for key,value in jira_cr_cloned_dict.items():
             CR_title=value[1].decode('utf-8')
             text+="""<tr><td class="cell">%s</td><td class="cell">%s</td><td class="cell">%s</td></tr>""" %(key,value[0],CR_title)
text+=”””</table></body>
     </html>”””
    HTML_BODY = MIMEText(text, 'html','utf-8')
    Encoders.encode_7or8bit(HTML_BODY)
    msg.attach(HTML_BODY)
    # Send the email via our own SMTP server.
    s = smtplib.SMTP('localhost')
    s.sendmail(sender, send_open_mail_to, msg.as_string())
    s.quit()

Code segment where jira_cr_cloned_dict is populated :-

CR_title=CR_detail['Title'].encode('utf-8')
print ('CR title in PRISM: %s\n'%CR_title)
CR_list=[prism_url[1:-1],CR_title]
jira_cr_cloned_dict[jira_link]=CR_list

Upon running the function send_mail_cloned(), I am getting errors such as

File "/usr/lib/python2.6/email/mime/text.py", line 30, in __init__
    self.set_payload(_text, _charset)
  File "/usr/lib/python2.6/email/message.py", line 224, in set_payload
    self.set_charset(charset)
  File "/usr/lib/python2.6/email/message.py", line 266, in set_charset
    self._payload = charset.body_encode(self._payload)
  File "/usr/lib/python2.6/email/charset.py", line 387, in body_encode
    return email.base64mime.body_encode(s)
  File "/usr/lib/python2.6/email/base64mime.py", line 147, in encode
    enc = b2a_base64(s[i:i + max_unencoded])

UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 54: ordinal not in range(128)

I tried not to encode/decode the strings. However, in that case the print %s fails itself.

Could someone please help me figure out the error here while sending email?

user1429246
  • 323
  • 2
  • 4
  • 9

1 Answers1

0

My guess is that your basic problem is at the following line:

CR_title=value[1].decode('utf-8')

You are explicitly decoding your value into a Unicode string. By concatenating it with byte strings, you are implicitly create the whole text as a Unicode string, and I'm not sure, but I'm guessing that MIMEText isn't written to handle Unicode strings but only bytestrings, and when it, then, tries to construct the message it triggers an implicit encoding into bytes which uses ASCII by default.

Try encoding the whole text explicitly as UTF-8 before passing it to MIMEText:

HTML_BODY = MIMEText(text.encode("utf-8"), 'html','utf-8')
Dolda2000
  • 25,216
  • 4
  • 51
  • 92
  • If I don't do CR_title=value[1].decode('utf-8') , then the code breaks at this line itself text+="""%s%s%s""" %(key,value[0],CR_title) text+=””” – user1429246 Mar 19 '13 at 00:18
  • Thanks for your suggestion. Doing this change, the error goes away BUT the email contains gibberish values which don't make any sense. – user1429246 Mar 19 '13 at 00:19
  • I'd wager that gibberish is simply your data not looking quite as you expected it to look. :) – Dolda2000 Mar 19 '13 at 07:07
  • Thanks for the suggestions. I eventually figured out that any of the required characters weren't outside ascii range so i decided to do CR_title=CR_detail['Title'].encode('ascii','ignore') instead of encoding to utf-8. – user1429246 Mar 19 '13 at 17:24