How to Send mail using Simple Email Services (SES) using Amazon Web Services (AWS) by Python Script

Sathish Nadarajan
 
Solution Architect
September 18, 2018
 
Rate this article
 
Views
4696

 

In this article, let us see how to send emails using AWS SES. AWS is providing one-year free license. Hope that is a straight forward approach to create an AWS Subscription. Hence, not focusing on that. Let us assume that, we have a Free/Paid Subscription already available.

1. Login to the AWS Console.

clip_image002

2. Click on the Services menu. We can see the list of services available.

clip_image004

3. Click on the “Simple Email Service” under the Category “Customer Engagement” section.

clip_image006

4. We need to Verify the From Address and the To Addresses to send and receive email through SES. The “To” Address can be a domain or a list of email address.

5. Click on the Verify a New Email Address.

clip_image008

clip_image010

clip_image012

clip_image014

clip_image016

clip_image018

1. Login to the URL https://console.aws.amazon.com/iam/home?#/security_credential from the AWS Console.

clip_image020

2. Click on the “Create New Access Key”.

3. A new access key will be generated. The same can be downloaded as a CSV file as well. By Default, the CSV file name would be “rootkey.csv

4. Make a note of the Access Key and the Secret Key.

Python and AWS CLI Installation

1. On the EMR Cluster, ensure that the Python has been installed. If not, we can download and install the Python from https://www.python.org/downloads/.

2. Ensure the AWS CLI also installed on the EMR Cluster. If not, by using the below command lines, we can install the AWS CLIENT and Boto3.

a. Python -m pip install awscli

b. Python -m pip install boto3

Shared Credentials File

1. We need to add the Access Key and the Secret Key which we generated few steps back to the Shared Config Credentials File.

2. The location of the Config file will be as shown in the below table.

If you’re using…Save the file as…
WindowsC:\Users\<yourUserName>\.aws\credentials
Linux, macOS or Unix~/.aws/credentials

3. Create a blank txt file with the name “credentials” without any extension.

4. Add the below lines of configuration values on the file.

[default]

aws_access_key_id = YOUR_AWS_ACCESS_KEY_ID

aws_secret_access_key = YOUR_AWS_SECRET_ACCESS_KEY

5. If we could not find the config file or cant able to create a file without extension, (when I try it from the Windows Server, I could not create the file), the workaround would be from the command prompt, enter the below command.

aws configure

clip_image022

6. Enter the Access Key, Secret Key and the Default Region When prompted.

clip_image024

7. Once, we register the aws Credentials on the EMR cluster from which we are planning to send email, then the below line of python script will be sending the email. The piece of code is self explanatory with the inline comments.

 

 #import the boto library
 import boto.ses
 
 #establish the connection with the access and secret keys
 conn = boto.ses.connect_to_region(
         'eu-west-1',
         aws_access_key_id='<YOUR_AWS_KEY_ID>',
         aws_secret_access_key='<YOUR_AWS_SECRET_KEY>')
 
 #verified Sender Mail ID
 SENDER = 'sathish@sppals.com'
 
 #verified Receiver Mail ID
 RECEIVER = 'sathish@sppals.com'
 
 #Subject Line of the Mail
 SUBJECT = 'Mail Triggerred from SES'
 
 #Actual HTML Content
 CONTENT='<html><body><table><tr>First Row</tr><tr>Second Row</tr></table></body></html>'
 
 #The HTML content is framed and kept in the TXT file.  The below lines should be implemented to read the actual file content
 #file=open("/home/Test.txt", "r")
 #read the content from the file
 #CONTENT = file.read()
 
 #Calling the Send Mail function with proper parameters
 conn.send_email(SENDER,SUBJECT,None,RECEIVER,format='html',html_body=CONTENT)
 

 

Python sendmail.py

clip_image026

Happy Coding,

Sathish Nadarajan.

Category : AWS, SES

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 15+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment