I do not have a large email infrastructure but I need to send mass email quickly. How can I do this using the Amazon Simple Email Service (SES) and AWS Lambda?

Traditionally, the hardware expenditure, software license costs, and technical expertise necessary to send mass email can be cost prohibitive. You can use Amazon Web Services technology to send mass email messages very quickly at a fraction of the investment required to build and maintain an in-house email infrastructure. 

Use Amazon SES and AWS Lambda in conjunction with Amazon Simple Storage Service (S3) to rapidly send HTML or text based email to a large number of recipients. The solution described in this article can also send multipart or alternative formatted email.

Note
If you send mass email to invalid email addresses, you risk having your SES TCP/IP address flagged as a spam email server. Spam servers are aggressively blacklisted by Internet Service Providers (ISPs), and if your SES IP address is blacklisted it will be unable to send email using ISPs that honor the blacklist. This solution is not recommended for sending email to a large number of recipients who might have invalid email addresses.

This solution quickly reads a list of email addresses from a mailing list that you publish to an S3 bucket. A Lambda function reads the list of email addresses and sends out a preformatted html or text email that is also contained in an S3 bucket to each of the recipients contained in the mailing list.

Follow these steps to implement this solution:

  1. Deploy ses_mailer.py on AWS Lambda with a timeout setting of 5 minutes. This setting ensures that any script timeouts occur within Lambda’s hard 5 minute script timeout period. If the script timeout setting exceeds 5 minutes, unexpected behavior can result because S3 invokes Lambda asynchronously. This could cause failed email delivery to some recipients with multiple successful deliveries to other recipients.
  2. Create an S3 bucket and set the S3 PUT Bucket notification event to trigger the Lambda function. Verify that the access permissions policy associated with your Lambda function grants Amazon S3 permissions to invoke the Lambda function. For more information about the permissions that are required for a Lambda function to access an S3 bucket, see the Implementation Summary section of Tutorial: Using AWS Lambda with Amazon S3.
  3. In the S3 events configuration, set the event suffix to .gz.
  4. Make sure the Lambda role has both S3 read/write permissions and the ses:SendRawEmail permission to the S3 bucket. Configure Lambda environment variables appropriate to your usage scenario. For example, the following variables would be valid for a given use case:
    REGION=us-east-1, MAX_THREADS=10, TEXT_MESSAGE_FILE=email_body.txt, HTML_MESSAGE_FILE=email_body.html.
  5. Write your HTML-formatted email to the file html_message.html and upload to the S3 bucket.
  6. Write the plain text version of your email message to the file text_message.txt and upload to the S3 bucket.
  7. Create a mailing list file in .CSV format and ensure that recipient information is specified using the following format. Do not enter header fields in the first row of the document. 

    Sender Name <me@example.com>, Recipient Name <you@example.com>, subject
  8. Compress the file using gzip. For example, the command
    gzip -kf mailing_list_14032016.csv creates mailing_list_14032016.csv.gz
  9. Upload the compressed .gzip file mailing_list_14032016.csv.gz to the S3 bucket to trigger the Lambda function.
  10. The function will start sending email to all addresses in the csv file and log failures in <FILENAME>_error.log.

Note
If the number of recipient addresses exceeds a few hundred thousand, you can take additional steps to improve sending performance:

  • Split email lists into multiple smaller csv files.
  • Increase the MAX_THREADS environment variable value. This value is subject to the SES TPS limit for your account.

The ses_mailer.py file that Lambda uses to send emails via SES is available for download from the AWS Labs Github repository.

Amazon SES, AWS Lambda, Amazon S3, mass email, spam, deliverability, SPF, Amazon SES Query API, error logging, parallel processing, statistics, notifications


Did this page help you? Yes | No

Back to the AWS Support Knowledge Center

Need help? Visit the AWS Support Center

Published: 2016-12-30