logo
Menu
Ensuring AWS SES Mail Delivery using Python Script

Ensuring AWS SES Mail Delivery using Python Script

This blog is for system admin/devops team to check/test email delivery programmatically

Published Feb 21, 2024
Amazon Simple Email Service (SES) is a cloud-based email sending service provided by Amazon Web Services (AWS). It enables businesses to send transactional, marketing, and notification emails effortlessly, with high deliverability rates and robust infrastructure.
What is AWS SES?
AWS SES simplifies the process of sending emails at scale while ensuring high deliverability, compliance, and security. It provides features such as:
  • Email Sending: Send bulk or individual emails securely through the AWS SES API or SMTP interface.
  • Email Receiving: Receive emails and process them using SES, with support for custom email addresses and domains.
  • Content Filtering: Filter emails based on content, attachments, and sender reputation to prevent spam and ensure compliance.
  • Bounce and Complaint Handling: Automatically handle bounce and complaint notifications to maintain sender reputation and improve deliverability.
  • Dedicated IPs: Optionally, use dedicated IP addresses to manage sender reputation and enhance email deliverability.
When to Use AWS SES?
AWS SES is suitable for various use cases and scenarios, including:
  1. Transactional Emails: Send emails triggered by user actions, such as account registration, password reset, order confirmation, and shipping notifications.
  2. Marketing Campaigns: Execute email marketing campaigns, newsletters, promotions, and announcements to engage with customers and prospects effectively.
  3. Notifications and Alerts: Deliver timely notifications, alerts, and updates to users regarding system events, service status, and application activities.
  4. Transactional Email Forwarding: Implement email forwarding functionality for applications, allowing users to forward emails to designated addresses.
  5. Application-to-User Communication: Facilitate communication between applications and users by sending critical information, updates, and reminders via email.
By leveraging AWS SES, businesses can ensure reliable email delivery, streamline communication processes, and maintain a positive sender reputation. Whether you're a small startup or a large enterprise, AWS SES provides the scalability, flexibility, and reliability needed to meet your email sending requirements.
Now, let's dive into testing the email delivery of AWS SES using a Python script in a Linux environment. Follow the steps below to initiate SES testing effortlessly.
When it comes to testing the email delivery of Amazon Simple Email Service (SES) using Python scripts in a Linux environment, the process can be straightforward and seamless. Follow these steps to initiate SES testing effortlessly:
  1. Ensure Python3 Availability:
    • Verify that Python3 is accessible on your system. Most recent Ubuntu and Amazon Linux 2 distributions come pre-installed with Python3.6 or above, making it readily available for usage.
  2. Open an Editor and Create a Python Script:
    • Launch any text editor like Nano or Vim and create a new file with a descriptive name, such as sestest.py.
  3. Paste the Script Content:
    • Copy the following Python script into the newly created file and save it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import smtplib
import os

# Retrieve AWS credentials from environment variables
access_key_id = os.environ.get('AWS_ACCESS_KEY_ID')
secret_access_key = os.environ.get('AWS_SECRET_ACCESS_KEY')

if not access_key_id or not secret_access_key:
print("AWS credentials not found in environment variables.")
exit()

# Update the following credentials as per your AWS SES account
user = access_key_id
pw = secret_access_key
host = 'email-smtp.YOUR_AWS_REGION.amazonaws.com'
port = 465
me = 'YOUR_SENDER_EMAIL_ADDRESS'
you = ('RECIPIENT_EMAIL_ADDRESS',) # Use a tuple if sending to multiple recipients
body = 'Test from AWS SES'
msg = ("From: %s\r\nTo: %s\r\n\r\n" % (me, ", ".join(you)))
msg += body

try:
# Establish connection with the SMTP server
s = smtplib.SMTP_SSL(host, port)
s.set_debuglevel(1) # Enable debugging output
# Login to the SMTP server
s.login(user, pw)
# Send the email
s.sendmail(me, you, msg)
print("Email sent successfully!")
except Exception as e:
print("An error occurred while sending the email:", str(e))
finally:
# Close the connection
if 's' in locals():
s.quit()
Next we can folow the below steps
4.Update Credentials:
Replace placeholders like mail-smtp.YOUR_AWS_REGION.amazonaws.com, YOUR_SENDER_EMAIL_ADDRESS, and RECIPIENT_EMAIL_ADDRESS with your actual AWS SES credentials and email addresses.This section of the script retrieves AWS access key ID and secret access key from environment variables AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY). If these environment variables are not found, it prints a message indicating that AWS credentials are missing and exits the script. Otherwise, it proceeds with using the retrieved credentials for sending the email.
5.Execute the Script:
Run the Python script using the command python3 sestest.py. You will receive debug output indicating the progress of the email transmission.
6.Check Your Inbox:
Optionally, verify the success of the test by checking your email inbox for the test email sent from AWS SES.
By following these steps, you can easily conduct SES testing using Python scripts in a Linux environment, ensuring the smooth functioning and reliability of your email delivery system powered by Amazon SES.
 

1 Comment