I am trying to make a program with smtp in python in order to send emails with it , but it always deploys me this error 'ascii' codec can't encode character '\xe9' in position 10: ordinal not in range(128), and I don really know how to solve it. Here is my code , please if anyone knows please explain me.
from email.message import EmailMessage
import smtplib
remitente = "email_sender"
destinatario = "email_receiver
"
mensaje = "¡Hola, mundo!"
email = EmailMessage()
email["From"] = remitente
email["To"] = destinatario
email["Subject"] = "Correo de prueba"
email.set_content(mensaje)
smtp = smtplib.SMTP("smtp-mail.outlook.com", port=587)
smtp.starttls()
smtp.login(remitente, "password")
smtp.sendmail(remitente, destinatario, email.as_string())
smtp.quit()
I tried different programs from different websites but everyone single time, it launches me this problem. Even I tried to change or find anything that could be the reason for the problem but even so , this error keeps.
Here is the traceback :
Traceback (most recent call last):
File "c:\Users\bilel\Desktop\Correos\correo.py", line 12, in <module>
smtp.starttls()
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2032.0_x64__qbz5n2kfra8p0\Lib\smtplib.py", line 769, in starttls
self.ehlo_or_helo_if_needed()
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2032.0_x64__qbz5n2kfra8p0\Lib\smtplib.py", line 611, in ehlo_or_helo_if_needed
if not (200 <= self.ehlo()[0] <= 299):
^^^^^^^^^^^
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2032.0_x64__qbz5n2kfra8p0\Lib\smtplib.py", line 451, in ehlo
self.putcmd(self.ehlo_msg, name or self.local_hostname)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2032.0_x64__qbz5n2kfra8p0\Lib\smtplib.py", line 378, in putcmd
self.send(f'{s}{CRLF}')
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2032.0_x64__qbz5n2kfra8p0\Lib\smtplib.py", line 357, in send
s = s.encode(self.command_encoding)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' in position 10: ordinal not in range(128)
I tried deleting and installing python again and it didn't work. I tried printing just the message and it prints the message correctly , I think the main problem is in the smtp extension, I also tried with the extension policy but it didn't work.I am thinking about changing to another lenguage , Which would you recommend me?
The problem is that e-mail internet protocols started and evolved long before charsets gravitated towards stabilizing on the use of UTF-8 encoding for non ASCII characters.
Actually, the default for many parameters in e-mail infrastructure, up to today, is to allow 7bit only characters in messages.
Python does implement fully working e-mail handling classes and utilities, as you have being digging, and as such, you have to be explicit when trying to send any non-ASCII character.
In your example code, there is the "¡" character - although the error message suggests you are using an "é" (which is encoded as "\xe9" in latin-1, the charset used by Windows) elsewhere.
Nonetheless, Python 3.6 incorporated some defaults that should allow any text to be included in e-mail bodies and headers, and should encode those as utf-8 (and other layers), as described by the latest RFCs -
Try just adding an explicit policy when creating the EmailMessage object:
If that does not work, you will have to check for yourslef on the docs of Python's e-mail package what could work there: https://docs.python.org/3/library/email.policy.html#module-email.policy