Q: How to properly use SMTP headers with unicode or accented characters? I had lot of difficulties recently sending emails because my headers were not properly encoded. It was working fine for all mail services except Gmail. Here I share a solution working for all of them.

A: Here a script sending an email. Explanations are in the comments. Access to an SMTP server is needed.
# Python 3.7.2
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import base64
import smtplib


# Please fill to match your server and email addresses:
SMPT_HOST = ''
SMPT_PORT = 
SMTP_USER = ''
SMTP_PASSWORD = ''
EMAIL_FROM_NAME = 'something'  # Here you can use unicode characters
EMAIL_FROM_ADR = 'something@something'
EMAIL_SUBJECT = 'Test'
DESTINATION_NAME = 'something'  # Here you can use unicode characters
DESTINATION_ADR = 'something@something'

# I use this function to be able to use accented characters in my headers:
def encode_base64(s: str) -> str:
    encoded_text = base64.b64encode(s.encode('utf-8')).decode('ascii')
    return f'=?utf-8?b?{encoded_text}?='

    
# This function is for encoding From and To:
def format_named_address(name: str, adr: str):
    return f'{encode_base64(name)} <{adr}>'


smtp = smtplib.SMTP(SMPT_HOST, SMPT_PORT)
smtp.starttls()  # enable TLS
smtp.login(SMTP_USER, SMTP_PASSWORD)

body = 'This is test.'

# Create the container (outer) email message.
outer = MIMEMultipart()
outer['Content-Type'] = 'text/plain; charset=utf-8'
outer['Subject'] = encode_base64(EMAIL_SUBJECT)
# For From and To, the email itself should not be encoded:
outer['From'] = format_named_address(EMAIL_FROM_NAME, EMAIL_FROM_ADR)
outer['To'] = format_named_address(DESTINATION_NAME, DESTINATION_ADR)

# Now send the message
part1 = MIMEText(body, 'plain', 'UTF-8')
outer.attach(part1)
smtp.sendmail(SMTP_USER, DESTINATION_ADR, outer.as_bytes())

smtp.quit()

