Here is a part of my code which sends an email:
servidor = smtplib.SMTP()
servidor.connect(HOST, PORT)
servidor.login(user, usenha)
assunto = str(self.lineEdit.text())
para = str(globe_email)
texto = self.textEdit.toPlainText()
textos = str(texto)
corpo = MIMEText(textos.encode('utf-8'), _charset='utf-8')
corpo['From'] = user
corpo['To'] = para
corpo['Subject'] = assunto
servidor.sendmail(user, [para], corpo.as_string())
Everything is ok except the part of the Subject. When I try to send a string with special characters (e.g. "ação") it raises this error:
UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-2: ordinal not in range(128)
How can I send emails with special characters in the Subject of MIMEText?
It seems that, in python3, a
Header
object is needed to encode aSubject
as utf-8:Output:
So the original script would become: