AWS Integrating S3 with Bulk Email Service AWS SES.

Nithish Kumar
3 min readJul 4, 2023

--

Introduction:

In this tutorial, we will explore how to automate the process of sending emails using Amazon SES (Simple Email Service) and S3 (Simple Storage Service) integration. We’ll leverage the power of these AWS services to streamline email communication and enhance our email delivery capabilities.

Step 1: Create S3 bucket and verify your email addresses in AWS SES which are in txt file.

Click on “create identity” >> give your mail address.

Step 2: Create an AWS Lambda Function

Using the Boto3 library, we’ll write a Python script to automate the process of fetching the email content from S3, retrieving the recipient list, and sending the emails using Amazon SES. We’ll handle error handling, email personalization, and bulk sending efficiently. Here is the code:

import boto3
import json

def lambda_handler(event, context):
# Retrieve email IDs from the S3 file
s3_bucket = event[‘Records’][0][‘s3’][‘bucket’][‘name’]
# Provide the path to your file in the S3 bucket
s3_key = event[‘Records’][0][‘s3’][‘object’][‘key’]

s3_client = boto3.client(‘s3’)
s3_object = s3_client.get_object(Bucket=s3_bucket, Key=s3_key)
email_ids = s3_object[‘Body’].read().decode(‘utf-8’).split(‘\n’)

# Send email for each email ID
ses_client = boto3.client(‘ses’)
for email_id in email_ids:
email_id = email_id.strip() # Remove leading/trailing whitespace

# Perform email sending operations using SES client
response = ses_client.send_email(
Source=’<FromAddress>’,
Destination={‘ToAddresses’: [email_id]},
Message={
‘Subject’: {‘Data’: ‘Your Subject’},
‘Body’: {‘Text’: {‘Data’: ‘Your Email Body’}}
}
)

print(f”Email sent to {email_id}. Message ID: {response[‘MessageId’]}”)

Now create event-notification in S3 bucket

Step 3: Now give permissions to lambda to contact to SES service without any interruption. Click on configuration tab in lambda function. Give full access to SES service.

Step 4: Now upload txt file which has email data to S3 bucket. SES service will send emails automatically.

Automating the process of sending emails using Amazon SES and S3 integration can significantly enhance your email delivery capabilities, save time, and streamline your communication workflows. By leveraging the power of AWS services and Python scripting, you can achieve efficient, personalized, and scalable email sending.

…Signing Off…

--

--