Error sending e-mail

I’ve got a case in which sending an e-mail with non UTF8 characters (ie “à”), causes a crash with:

sendmail.py", line 40, in sendmail
    senderrs = server.sendmail(from_addr, to_addrs, msg.as_string())
  File "/usr/lib/python3.9/smtplib.py", line 859, in sendmail
    msg = _fix_eols(msg).encode('ascii')
UnicodeEncodeError: 'ascii' codec can't encode character '\xe0' in position 213: ordinal not in range(128)

My code looks like this:

msg = MIMEText('Gràcies', 'html', _charset='utf-8')                                                  
msg['From'] = from_addr                                                                              
msg['To'] = ', '.join(to_addr)                                                                       
msg['Subject'] = Header("Hola", 'utf-8')
sendmail(from_addr, to_addr, msg)

The thing is that I can prevent the issue with this simple patch, which basically lets SMTP class to convert the e-mail message before sending:

--- a/trytond/sendmail.py
+++ b/trytond/sendmail.py
@@ -36,7 +36,7 @@ def sendmail(from_addr, to_addrs, msg, server=None, strict=False):
     if 'Date' not in msg:
         msg['Date'] = formatdate()
     try:
-        senderrs = server.sendmail(from_addr, to_addrs, msg.as_string())
+        senderrs = server.send_message(msg, from_addr, to_addrs)
     except Exception:
         if strict:
             raise

Am I doing something wrong? Would it make more sense to use send_message()?

non UTF-8 for sure is not allowed.
ASCII only is preferred for compatibility. And UTF-8 may be supported by some servers but it must be supported for all the chain of servers (or they must support fallback). So it is better to avoid UTF-8.

I guess it would be an improvement but then I would make the from_addr and to_addrs optional for sendmail and sendmail_transactional and add the options parameters. And I would update all the codes to take advantage of the usage of header instead of addr parameters.

Ups! Of course, I meant non-ASCII!

I’m not sure I get this. Do you mean that we cannot send emails with UTF8 encodings in the body? How should I do this?

No the body is not a problem. It is the email addresses.