boto3 ses InvalidParameterValue error due to unicode characters

3.4k views Asked by At

I am trying to send emails through SES using boto3. Below is my code.

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

import boto3

message = MIMEMultipart()
message['Subject'] = 'i am subject äöü'
message['From'] = 'sender äöü <[email protected]>'
message['To'] = 'receipient äöü <[email protected]>'
html = MIMEText('<strong>Welcome to AWS SES äöü</strong>', 'html')
message.attach(html)

ses.send_raw_email(
    RawMessage={
        'Data': message.as_string()
    }
)

But i am getting the exception below. This error is something to do with unicode characters(äöü) in From and To addresses. If i remove the unicode characters then email is sent successfully.

An error occurred (InvalidParameterValue) when calling the SendRawEmail operation: Missing final '@domain'

Please help to resolve the issue.

2

There are 2 answers

0
MA1 On

Encode the email parts as suggested in https://mg.pov.lt/blog/unicode-emails-in-python.html. This solved the issue and now i am able to send emails.

0
charlax On

According to the SES documentation:

If you want to use Unicode characters in the "friendly from" name, you must encode the "friendly from" name using MIME encoded-word syntax, as described in Sending Raw Email Using the Amazon SES API.

One way to do it:

from email.header import Header

msg['Subject'] = Header('helloé éé', 'utf-8').encode()
# '=?utf-8?b?aGVsbG/DqSDDqcOp?='