7

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='no-reply@domain.com',
            Destination={
                'ToAddresses': [
                    email_to,
                ],
            },
            Message={
                'Subject': {
                    'Data': subject,
                },
                'Body': {
                    'Text': {
                        'Data': body,
                        'Charset': 'UTF-8',
                    },
                    'Html': {
                        'Data': body,
                        'Charset': 'UTF-8',
                    },
                }
            },
            ReplyToAddresses=[
                'mharris@domain.com', # just in case someone replies to a no-reply@ address I'll receive them
            ],
            ReturnPath='mharris@domain.com', # 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!

morissette
  • 971
  • 7
  • 28
  • I do not understand your Q. SES does not remove new lines. No need to force UTF-8. What are you sending and what are you receiving? Limit your sample set to 2 or 3 lines. – helloV Feb 15 '16 at 20:02
  • I mean when I send the body seen in send_reset_email, the email comes in like: "Hello! We have received a request to reset your password. Please click the link below to proceed with resetting your password. Note: this link will expire in 1 hour. http://staging.company.com/password/reset/fac2534e-e815-4ef9-8dd4-239c6199df52 If you did not request this reset you can safely ignore this e-mail. Thank you for choosing Company! The Company Team www.company.com" - without formatting – morissette Feb 15 '16 at 20:07
  • Perhaps `\r\n` would be more correct? See [What is the proper newline in emails? LF or CRLF?](http://stackoverflow.com/q/6783863/1695906) – Michael - sqlbot Feb 15 '16 at 20:44
  • I also tried \r\n with no success - currently I implemented a email templating system w/ .html and .text files for each email I am sending and the full HTML ones work as expected. – morissette Feb 16 '16 at 15:48

3 Answers3

7

Edit: I was having a similar issue with outlook 2013 clients. Adding a tab character before the newline worked for me.

Replacing \n with \t\n Or \t\r\n

How do I format a String in an email so Outlook will print the line breaks?

Community
  • 1
  • 1
  • Perfect! Thank you! I was using a SNS topic for some simple alerting, SNS Doesn't use text/html and all other examples I found assumed I was! – 0xVox Oct 18 '18 at 14:23
5

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

john.2017
  • 51
  • 1
  • 2
4

I faced similar issue in sending a contents of log file (stored in variable) to HTML BODY. Replacing the new line with "<br />" as below helped solve the problem. Below command replaces all newline characters in the text with "<br />".

mytext = mytext.split("\n").join("<br />")
bubbleChaser
  • 105
  • 1
  • 9