Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amazon SES send_email Text body with spacing/newlines

I am facing an issue where all of my text e-mails are scrunched together and do not have new lines persisting through the sending process.

Here is the code:

def send_ses_message(email_to, subject, body):
    ses = init_ses_internal()
    if ses:
        ses.send_email(
            Source='[email protected]',
            Destination={
                'ToAddresses': [
                    email_to,
                ],
            },
            Message={
                'Subject': {
                    'Data': subject,
                },
                'Body': {
                    'Text': {
                        'Data': body,
                        'Charset': 'UTF-8',
                    },
                    'Html': {
                        'Data': body,
                        'Charset': 'UTF-8',
                    },
                }
            },
            ReplyToAddresses=[
                '[email protected]', # just in case someone replies to a no-reply@ address I'll receive them
            ],
            ReturnPath='[email protected]', # bounce backs will come to me also
        )
        return True

I have most recently tried forcing UTF-8 hoping that would allow the newlines to persist. After that I added \n where a new line should exist.

Here is an example of a email:

    def send_reset_email(self, email_to, unique_id):
        subject = "Company Password Reset"
        body = """
Hello!\n\n

We have received a request to reset your password.\n

Please click the link below to proceed with resetting your password. Note: this link will expire in 1 hour.\n\n

http://staging.domain.com/password/reset/{}\n\n

If you did not request this reset you can safely ignore this e-mail.\n\n

Thank you for choosing Company!\n\n

The Company Team\n
www.company.com\n
""".format(unique_id)
        send_ses_message(email_to, subject, body)

Please let me know what I can do to ensure that newlines are persistent across Amazon SES. Thanks!

like image 738
morissette Avatar asked Feb 15 '16 18:02

morissette


1 Answers

The default content type in SES seems to be text/html, so using <br> instead of \n worked for me

like image 51
john.2017 Avatar answered Dec 08 '22 00:12

john.2017