JavaQueen
JavaQueen

Reputation: 1205

How to send a plain text mail using AWS SES to gmail

I have a formatted file, with padding strings, when I send it to my account in gmail, all the paddings in the file are deleted and the mail is not well formatted because gmail does not keep the original format. What to do to avoid this ?

I tried this code, but it is not working using Raw email :

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

fileHandler = "logg.log"
session = Session(profile_name="test", region_name="eu-west-1")
ses = session.client('ses')
today = datetime.date.today() - datetime.timedelta(days=1)
for file in sorted(glob.glob(fileHandler + '*'), key=os.path.getmtime):
    body = MIMEText(open(file, 'r').read())
    body['Subject']="aws-log"
    response = ses.send_raw_email(
                            Source = "[email protected]",
                            Destinations=[
                                    "[email protected]",
                                ],
                            RawMessage={
                                'Data': body.as_string()
                                    },)

Even by using raw mails, the mail does not come in plain text.

Here is the file I send to gmail :

enter image description here

This is the mail I get:

enter image description here

Upvotes: 0

Views: 2451

Answers (3)

JavaQueen
JavaQueen

Reputation: 1205

I finally resolved my problem, this guy <pre></pre> saved me!! Here is how:

body = '<pre width="300" style="font-size: 14px; max-width: 100%;">' + body + '</pre>'
            subject = objet+" %s" % (today.strftime("%Y-%m-%d"))
            for email in emailarg.split(','):
                response = ses.send_email(
                            Source = email,
                            Destination={
                                'ToAddresses': [
                                    email,
                                ],
                            },
                            Message={
                                'Subject': {
                                    'Data': subject
                                },
                                'Body': {
                                    'Html': {
                                        'Data': body
                                    },
                                }
                            }
                        )

Upvotes: 1

Michael - sqlbot
Michael - sqlbot

Reputation: 179314

Look more closely at your received email.

Copy its body out and paste it into a text editor. You should find that the spaces are in fact perfectly correct.

The issue here -- I believe -- is not with SES.

The issue is that Gmail's UI displays plain text emails in a proportional typeface, instead of a fixed-width typeface. Note how IIIIII takes up less horizontal space than WWWWWW even though the number of characters is the same. This naturally destroys formatting with spaces. Conversely...

# IIIIII - code blocks on SO
# WWWWWW - use a fixed width typeface
# even though the rest of the post doesn't
# so I and W are the same width here but not above

If you want control over the display, you'll need to use HTML and specify a fixed-width typeface.

Upvotes: 0

Dave Maple
Dave Maple

Reputation: 8412

You shouldn't need send_raw_email. If you take a look at the send_email method it actually takes both Text and Html body arguments:

Message={
    'Subject': {
        'Data': 'string',
        'Charset': 'string'
    },
    'Body': {
        'Text': {
            'Data': 'string',
            'Charset': 'string'
        },
        'Html': {
            'Data': 'string',
            'Charset': 'string'
        }
    }
}

If you just supply a Text body that should get you the plain-text email you're looking for.

As far as gmail plain text formatting they will wrap lines at 78 characters as per RFC 2822 section 2.1.1: https://www.rfc-editor.org/rfc/rfc2822

This is an accepted standard for plain text emails. You could format the message yourself for plaintext but it would have to adhere to a 78 character limit per line to avoid reformatting.

Upvotes: 2

Related Questions