A Python implementation for sending emails using Amazon Simple Email Service (AWS SES). The main purpose of this python code is to send out mass marketing html emails using AWS SES service.
- Python 3.11.7
- boto3 1.34.66 (AWS SDK for Python)
- AWS Account with SES access
- AWS Access Key and Secret Access Key
Install the required AWS SDK for Python:
pip install boto3
You'll need to configure the following AWS credentials:
aws_access_key_id = <Your Access Key>
aws_secret_access_key = <Your secret access key>
myregion = <your region>
import boto3
from botocore.exceptions import ClientError, WaiterError
import logging
The main functionality is provided through the Send_SES_Email
function. Here's a working example from the implementation:
message_id = Send_SES_Email(
aws_access_key_id,
aws_secret_access_key,
'ap-southeast-1',
'[email protected]',
['[email protected]', '[email protected]'],
[], # CC addresses (empty in this example)
[], # BCC addresses (empty in this example)
'Test Email Subject 5',
'This is a test email body 5',
'This is a test email html body 5'
)
aws_access
: Your AWS access key IDaws_secret
: Your AWS secret access keyaws_region
: AWS region for SES service (e.g., 'ap-southeast-1')vsource
: Sender email addressvTo
: List of recipient email addressesvCC
: List of CC recipient email addressesvBCC
: List of BCC recipient email addressesvsubject
: Email subject linevtext
: Plain text version of the email bodyvhtml
: HTML version of the email body
The function returns a message ID if the email is sent successfully. Example of a successful message ID:
'010e018e039375e6-a2a37041-f498-4938-af11-9f0168b781ab-000000'
The implementation includes error handling using try-except blocks:
- Logs successful sends with message ID and source address
- Catches and logs ClientError exceptions
- Uses Python's logging module for error tracking
- boto3
- botocore.exceptions (ClientError, WaiterError)
- logging