Integrating AWS Lambda with S3 and SES: Sending Emails to Multiple Recipients
Aman Saklani
Machine Learning | Deep Learning | AWS | Generative AI | Prompt Engineering | Python | Aspiring MLOps
In this tutorial, we will explore how to leverage the power of AWS Lambda, S3, and SES to send emails to multiple recipients. We will start by creating an S3 bucket and uploading a file containing multiple email IDs. Then, using the Boto3 library, we will retrieve the email IDs from the file. Finally, we will utilize the Boto3 library to send an email using the SES (Simple Email Service) for each retrieved email ID. This integration will enable you to automate email notifications or campaigns with ease using AWS serverless services
Step 1: Setting up AWS Resources To begin, we need to set up the necessary AWS resources. This includes creating an S3 bucket to store the email object file and configuring SES to send emails.
Step 2: Preparing the Email Object File Next, we’ll create a text file that contains multiple email IDs. We’ll upload this file to the S3 bucket we created earlier. This file will serve as the source for retrieving the email IDs in the Lambda function.
Step 3: Implementing the Lambda Function We’ll create a new Lambda function using the AWS Management Console. We’ll define the necessary IAM roles and permissions for the Lambda function. Then, we’ll write the Lambda code to retrieve email IDs from the S3 object file using the Boto3 library. We’ll install and import the Boto3 library, and utilize its S3 API to retrieve the email IDs from the object file stored in S3.
import jso
import boto3
print('hi')
def lambda_handler():
# TODO implement
s3_client = boto3.client('s3')
ses_client = boto3.client('ses', region_name='ap-south-1')
bkt = 'awsemailtask123312413231'
item = 'email.txt'
hostMail = '[email protected]'
obj = s3_client.get_object(Bucket=bkt, Key=item)
print("hi")
info = obj['Body'].read().decode('utf-8')
mails = info.split(",")
valid_emails = []
invalid_emails = []
for email in mails:
email = email.strip() # Remove leading/trailing whitespace
if email and '@' in email:
valid_emails.append(email)
else:
invalid_emails.append(email)
if invalid_emails:
invalid_emails = [email for email in invalid_emails if email] # Remove empty entries
#print(f"Invalid email addresses: {invalid_emails}")
# Handle invalid email addresses here (e.g., logging, error reporting)
if valid_emails:
ses_client.send_email(
Source=hostMail,
Destination={
'ToAddresses': valid_emails
},
Message={
'Subject': {
'Data': 'Test Email'
},
'Body': {
'Text': {
'Data': '''This is a sample message to test the working of SES service.'''
}
}
}
)n
Step 5: Leveraging SES to Send Personalized Emails Now comes the exciting part! We’ll configure SES with our verified sender email address. Then, using the retrieved email IDs, we’ll use the Boto3 SES API to send personalized emails to each recipient. We’ll craft the email subject, body, and other relevant details to create a customized email experience.
领英推荐
Step 6: Testing and Deployment Before deploying the solution, it’s essential to thoroughly test our Lambda function. We’ll use sample data to test the retrieval of email IDs and the successful sending of emails using SES. Once we’re confident in our implementation, we’ll deploy the Lambda function to the AWS cloud.
Conclusion:
By integrating AWS Lambda with S3 and SES, we’ve created an automated email distribution system that can send personalized emails to multiple recipients. This solution brings together the power of serverless computing, object storage, and email services provided by AWS. The possibilities are endless — you can use this system for various use cases, such as sending notifications, newsletters, or marketing campaigns. Now, it’s time to put this knowledge into action and start automating your email communication with AWS Lambda, S3, and SES.
Connect with me on my Medium blog site and lets together grow and explore more interesting integrations and used cases of AWS services.
Glad to have LinuxWorld Informatics Pvt Ltd , Vimal Daga , Preeti Chandak and technical team to guide me.. : )