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.
2. Click on the Services menu. We can see the list of services available.
3. Click on the “Simple Email Service” under the Category “Customer Engagement” section.
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.
1. Login to the URL https://console.aws.amazon.com/iam/home?#/security_credential from the AWS Console.
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…|
|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.
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.
6. Enter the Access Key, Secret Key and the Default Region When prompted.
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 = 'email@example.com' #verified Receiver Mail ID RECEIVER = 'firstname.lastname@example.org' #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)