Day 20: Sending HTML Emails from AWS Lambda using Python & SES.
Source: Dev.to
Beyond Plain Text
Welcome to Day 20. SNS is great for SMS or internal alerts, but for user‑facing emails, you need HTML. Enter Amazon SES.
Prerequisites
- Verify your email in the SES Console (Identities).
- Add
ses:SendEmailpermissions to your Lambda IAM Role.
The Code (Python)
Unlike SNS which just takes a string, SES expects a dictionary structure for MIME types.
client.send_email(
Source='me@example.com',
Destination={'ToAddresses': ['me@example.com']},
Message={
'Subject': {'Data': 'My Subject'},
'Body': {
'Html': {'Data': '''
## Hello World
'''}, # HTML part
'Text': {'Data': 'Hello World'} # Plain‑text fallback
}
}
)
Pro Tip
Use inline CSS for email templates, as Gmail and Outlook often strip external stylesheets.
