Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amazon SES with Flask Python

I've just started with Python on Flask. I want to learn what is the best way to send emails from Flask using the Amazon SES?

I've seen the boto, but it's an interface to all the Amazon Services. I've seen some other custom examples as well.

What is the best, simple and efficient way to send emails in Flask?

Thanks.

like image 766
Zaeem Avatar asked May 19 '17 05:05

Zaeem


1 Answers

I had a similar situation. You should Send Formatted Email Using the Amazon SES API.

You could use boto, as you suggested. But this is not as updated as boto3. More information here.

Here is what I've done with success.

Include the following keys in your app.config:

# Amazon Web Services credentials
AWS_ACCESS_KEY_ID = 'your access key id'
AWS_SECRET_ACCESS_KEY = 'your secret access key'

# Amazon Simple Email Service
SES_REGION_NAME = 'us-west-2'  # change to match your region
SES_EMAIL_SOURCE = '[email protected]'

Note: The email source (sender) must be a verified email address, as configured in the SES Console.

Then, somewhere in your code, define a function like this:

import boto3

def send_email(app, recipients, sender=None, subject='', text='', html=''):
    ses = boto3.client(
        'ses',
        region_name=app.config['SES_REGION_NAME'],
        aws_access_key_id=app.config['AWS_ACCESS_KEY_ID'],
        aws_secret_access_key=app.config['AWS_SECRET_ACCESS_KEY']
    )
    if not sender:
        sender = app.config['SES_EMAIL_SOURCE']

    ses.send_email(
        Source=sender,
        Destination={'ToAddresses': recipients},
        Message={
            'Subject': {'Data': subject},
            'Body': {
                'Text': {'Data': text},
                'Html': {'Data': html}
            }
        }
    )

Of course, you can add more parameters to ses.send_email if you need more complex messages.

Then you can send emails like this:

recipients = ['[email protected]']
subject = 'Thanks for registering'

# You can render the message using Jinja2
html = render_template('email.html', name='John Doe')

send_email(current_app,
           recipients=recipients,
           subject=subject,
           html=html
           )

Improvements:

You can use a thread to send the email as an asynchronous task. This is better explained in the The Flask Mega-Tutorial, Part XI: Email Support, by Miguel Grinberg. This is actually what I am using.

Or, maybe better, you could use Celery for this. But since you are in the Amazon ecosystem, I'd use SQS.

Other options include:

  • You could use Flask-Mail and configure to access SES via the SMTP interface. This is a more general solution (can access other services, not only SES), but if you will stick to Amazon, it is better to use the API.
  • There are options like Flask-SES, but I don't think you need it. It basically wraps the send_mail function.
like image 123
Luciano Schirmer Avatar answered Sep 27 '22 16:09

Luciano Schirmer