1

I am getting the above error when trying to run an automated email script. If I delete most of the body of the email it works fine. However, when I add over some number of characters (all are upper/lower case or numbers) I get the error. I tried changing the code in smtplib.py but then my script stopped working.

This is my code:

smtpObj = smtplib.SMTP('smtp endpoint', 587)
smtpObj.ehlo()
smtpObj.starttls()
smtpObj.login('login', sys.argv[1])

for name, email in unmessagedmembers.items():
    body = """Subject: Research\na {} lot of text 
n more text""".format(name)

print('Sending email to {}...'.format(email))


sendmailStatus = smtpObj.sendmail('me@email.com', email, body)

if sendmailStatus != {}:
    print('There was a problem sending email to {}: {}'.format(email, sendmailStatus))

smtpObj.quit()

it is saying I get the error on the line:

sendmailStatus = smtpObj.sendmail('me@email.com', email, body)
martineau
  • 119,623
  • 25
  • 170
  • 301
  • You're giving it a string, which will be encoded to bytes using the ASCII codec, as per [documentation](https://docs.python.org/3/library/smtplib.html). If you want to send text beyond basic ASCII, you need to encode the string yourself to bytes first. – Grismar Jun 16 '22 at 21:01
  • I did this .encode('ascii', 'ignore').decode('ascii') per another stack question but then certain mailboxes block the message. –  Jun 16 '22 at 21:05
  • 3
    `\u2019` is a `RIGHT SINGLE QUOTATION MARK` (`’` as opposed to ASCII quote `'`) and is not an ASCII character. Your "lots of text" must contain one. – Mark Tolonen Jun 16 '22 at 21:13
  • Please first search for the error message before asking yet another question on it. Also, provide a [mcve], so people can reproduce the issue. In most cases, you'll find the error yourself that way. – Ulrich Eckhardt Jun 16 '22 at 21:16
  • Thanks for the help, all the suggestions worked. I guess I wasn't understanding the solution on first review. –  Jun 16 '22 at 21:24

1 Answers1

1

The documentation states: "msg may be a string containing characters in the ASCII range, or a byte string. A string is encoded to bytes using the ascii codec, and lone \r and \n characters are converted to \r\n characters. A byte string is not modified."

Since email is likely a string, Python tries to encoded it using the ASCII codec when you call smtpObj.sendmail(), and the encoding comes down to:

email.encode('ascii')

If you were to run that line, you're likely to see the same error message.

To avoid it, encode email yourself and then pass it to smtpObj.sendmail():

    email.encode()  # by default will use utf8 in modern Python
    sendmailStatus = smtpObj.sendmail('me@email.com', email, body')

Note that your example code has errors in the indentation, I assumed all the code was indented to be within the for block.

However, whether that causes problems on the receiving end is a different matter - you can try replacing any character outside the ASCII range with other characters, or look into adding headers that instruct the recipient to decode the message using the correct encoding.

Grismar
  • 27,561
  • 4
  • 31
  • 54
  • Isn't the [quopri encoding](https://docs.python.org/3/library/quopri.html) pretty standard in emails? – lenz Jun 16 '22 at 21:21