DEV Community

Arjun Menon for AWS Community Builders

Posted on • Originally published at arjunmenon.hashnode.dev on

Building a Mass Emailing System Using AWS Lambda and SES

A mass emailing system allows businesses to reach out to customers efficiently, and building one using Amazon Web Services (AWS) can be both scalable and cost-effective. In this blog post, I'll guide you through setting up a mass emailing system using AWS Lambda, Simple Email Service (SES), and S3.

Step 1: Create an S3 Bucket

Amazon S3 will store the CSV files containing the email addresses of the recipients.

  1. Navigate to S3: In the AWS Management Console, click on S3 under the Storage category.

  2. Create a Bucket: Click on Create bucket. Name your bucket (it must be unique across AWS), select a region close to you for better performance, and then create the bucket.

Step 2: Set Up AWS Simple Email Service (SES)

AWS SES will handle the email sending operations.

  1. Navigate to SES

  2. Verify Your Email Address: Before sending emails, SES requires verification of your ownership of the email address. On the left pane, go to Identities, and then verify the email address you want to send from.

Important Note: As we are using SES in sandbox mode, you must manually verify each email address that you wish to send emails to before they can receive emails from your application. This is a restriction in the sandbox environment intended to prevent spam and abuse.

  1. Add and verify all Recipient Email Addresses: You can use Temp Mail for testing.

Step 3: Create an IAM Role for Lambda

This role will allow your Lambda function to access AWS resources securely.

  1. Navigate to IAM: Go to IAM under Security, Identity, & Compliance.

  2. Create a New Role: Click on Roles, then Create role. Choose Lambda for the service that will use this role, then proceed to permissions.

  3. Attach Policies: Add policies like AmazonSESFullAccess, AmazonS3FullAccess, and AWSLambdaExecute.

Step 4: Create a Lambda Function

This function will process the CSV file and send emails.

  1. Navigate to Lambda: Go to Lambda under Compute.

  2. Create Function: Choose Author from scratch, name your function, select Python 3.x for the runtime.

  3. Set Permissions: Choose the IAM role you created earlier.

  4. Create Function: Finalize the creation of your function.

Step 5: Write Lambda Function Code

The function will read from S3, parse CSV, and send emails via SES.

import boto3import csvdef lambda_handler(event, context): s3 = boto3.client('s3') ses = boto3.client('ses') bucket_name = event['Records'][0]['s3']['bucket']['name'] key = event['Records'][0]['s3']['object']['key'] response = s3.get_object(Bucket=bucket_name, Key=key) lines = response['Body'].read().decode('utf-8').split() for row in csv.DictReader(lines): ses.send_email( Source='your-verified-email@example.com', Destination={'ToAddresses': [row['email']]}, Message={ 'Subject': {'Data': 'Your Email Subject'}, 'Body': {'Text': {'Data': "Your email content"}} } )
Enter fullscreen mode Exit fullscreen mode

Step 6: Set Up S3 Event Trigger

Trigger your Lambda function when a CSV file is uploaded to S3.

  1. Select Your Function: In Lambda, open your functions configuration.

  2. Add Trigger: Choose S3 from the list. Configure the trigger for PUT events, and set the file prefix or suffix (like .csv).

  3. Save: Save your configuration.

Step 7: Test Your Setup

Upload a CSV file with an email column to your S3 bucket and check the Lambda execution logs. Make sure the emails are being sent out successfully.

Email Recieved:

Reminder: As we are using SES in sandbox mode, you must manually verify each email address that you wish to send emails to before they can receive emails from your application. This is a restriction in the sandbox environment intended to prevent spam and abuse.

Conclusion

You've now successfully set up a basic mass emailing system using AWS. This system is not only scalable but also cost-effective, perfect for businesses looking to manage their email marketing efforts. Always ensure to comply with legal requirements and manage opt-ins and opt-outs to respect user privacy and avoid spam.

Top comments (0)