Jens
Jens

Reputation: 131

How to specify Reply-To using send_raw_email in boto3?

Using boto3's send_email I can easily set up custom reply-to addresses with ReplyToAddresses. But how do I achieve the same behaviour with send_raw_email?

Simple send_email example:

import boto3

client = boto3.client('ses')

client.send_email(**{
    'Source': "[email protected]",
    'Destination': {
        'ToAddresses': ["[email protected]"],
    },
    'Message': {
        'Subject': {'Data': "Hello!"},
        'Body': {
            'Text': {'Data': "How are you?"},
        },
    },
    'ReplyToAddresses': ['[email protected]']
})

What would this look like with send_raw_email (in particular ReplyToAddresses)?

Upvotes: 3

Views: 1563

Answers (2)

tripleee
tripleee

Reputation: 189297

The send_raw_email API requires you to pass in a complete RFC5322 message as a bytes object. Ad-libbing from your example, something like

from email.message import EmailMessage
import boto3

client = boto3.client('ses')

msg = EmailMessage()
msg['from'] = "[email protected]"
msg['to'] = "[email protected]"
msg['subject'] = "Hello!"
msg['reply-to'] = "[email protected]"
msg.set_content("How are you?")

response = client.send_raw_email(
    RawMessage={
        'Data': msg.as_string().encode('us-ascii')
    },
    FromArn='string',
    # ... etc etc
)

The .encode() is required to force the as_string() string into a bytes object. You will want to make sure you only use 7-bit ASCII in the message (for other SES reasons, too), otherwise you will get a UnicodeEncodeError. The Python MIME libraries generally take care of this for you, though you have to use slightly more complex code than the above if you need non-ASCII strings in the email headers.

Upvotes: 2

Swathi Krishnan
Swathi Krishnan

Reputation: 1

You can just add it to the Message, something like this one:

{ 'Message' : 'reply-to': {'Data': "email-id"} }

P.S: Follow RFC 5322 to construct the message.

Upvotes: 0

Related Questions