2

I am new to python and trying to receive and resend an email using poplib and smtplib:

    messages = [pop_conn.retr(i)[1] for i in range(1, mail_count + 1)]
    #decode messages
    messages = [[line.decode("utf-8") for line in message] for message in messages]
    # Concat messages
    messages = ["\n".join(msg) for msg in messages]
    #...        
    for message in messages:
        smtp_conn.sendmail(args.address, args.target, message)

In the debugger all message strings look good, but in the sendmail call following error occurs:

msg = _fix_eols(msg).encode('ascii')

UnicodeEncodeError: 'ascii' codec can't encode character '\xa0' in position 17938: ordinal not in range(128)

What am i doing wrong?

3 Answers3

2

It appears as though whatever character \xa0 represents does not have a representation in ASCII. According to this link, \xa0 is the unicode character for a non-breaking space.

Since this is really just a space, you could try and replace all \xa0 characters in your string:

 messages = ["\n".join(msg.replace(u'\xa0', u' ')) for msg in messages]

To be fair, space and non-breaking spaces function differently, so depending on where this character appears in your message, the output could look slightly different after replacing the non-breaking spaces with regular spaces.

Another option is to ignore any characters that produce any error. This solution is not ideal because you could lose characters that end-up changing the formatting (or sometimes meaning) of your text. Replacing the non-breaking space with a normal space is smart to do regardless, but for all other pesky characters:

msg.encode("ascii", errors="ignore")

Alternatively, you can do msg.encode("ascii", errors="replace") but that will replace these characters with a '?' which doesn't look so nice.

Community
  • 1
  • 1
TheF1rstPancake
  • 2,318
  • 17
  • 17
  • It's necessary because the exception is raised in the smtplib anyways. I forgot this in my question –  Nov 25 '16 at 19:03
  • Well damn. There goes that solution. I'll remove it from my answer. Did you try replacing the character with a space? – TheF1rstPancake Nov 25 '16 at 19:04
  • Yes, but than another error with unknown character '\xe4' occurs –  Nov 25 '16 at 19:10
  • Got it. I've definitely gotten around this issue before but need to rediscover how. – TheF1rstPancake Nov 25 '16 at 19:10
  • I still don't unterstand why the libary encodes to ASCII because Utf-8 semms to be the standart email encoding. But anyways your solution works for mehr –  Nov 26 '16 at 08:05
1

You are trying to encode an utf-8 character that is not in the ascii standard, as ascii. A0 is a non-breaking space. If that's the only character that's not ascii encodable, you can just replace it with a normal space:

spaced_message = message.replace("\x0a", " ")

Otherwise, look into https://en.wikipedia.org/wiki/Unicode_and_email#Unicode_support_in_message_bodies

Encoding strings as utf-7 (yes, 7) usually works, but it's officially deprecated in many systems. Utf-8 requires base64 encoding on top, which is a bit tricky.

Filip Haglund
  • 13,919
  • 13
  • 64
  • 113
0

I solved this error by editing the smtplib source code on line 859.

Replace 'ascii' on line 859

msg = _fix_eols(msg).encode('ascii')

with 'utf-8'

msg = _fix_eols(msg).encode('utf-8')
Peter Park
  • 396
  • 4
  • 17