Pages

Thursday, February 6, 2020

Sending Emails using SES - Simple Email Service

Amazon Ses ( Simple Email Service ) is a service which sends an email regarding marketing, transactions and notifications.  It is suitable for any type of business as the cost is less and it is reliable. Amazon Ses can be integrated to an existing application with the SMTP interface and Amazon SDK. Various other services can be integrated with Ses to send emails.

Verify the Emails 
The first thing we need to do is to add our email address to verify. the emails address that are added here will be sent mails. Emails that are not available here will not be receiving emails from Aws services. So if you want to send emails to your clients, add their mail id and verify them.


Create a Email Template
Templates are nothing but a personalised email that we can send to customers. create a file with the below content,


{
 "Template": {
    "TemplateName": "test-template",
"SubjectPart": "Test Email From Jagadish",
"TextPart": "Test Email Body",
"HtmlPart": "<html>\r\n\r\n<head>\r\n  <title> Test Email Template<\/title>\r\n  \r\n<body> \r\n <h1> User Name is Jagadish <\/h1>\r\n<\/body>\r\n\r\n<\/body>\r\n<\/html>"
 }
}

save this as ses-template.json. Now run the aws cli to create this template,

jagadishm@[/Volumes/Work/aws/templates]: aws ses create-template --cli-input-json fileb:///Volumes/Work/aws/templates/ses-template.json --region eu-west-1

Once we run this command, a template is created. Send an Email using Python boto3. Run the below code ,
jagadishm@[/Volumes/Work/aws/code]: cat send-email-ses.py 
#!/usr/bin/python

import boto3

# Create SES client
ses = boto3.client('ses',region_name='eu-west-1')

response = ses.send_templated_email(
  Source='jagadishm.middleware@gmail.com',
  Destination={
    'ToAddresses': [
      'jagadish.manchala@gmail.com',
    ],
    'CcAddresses': [
      'jagadish.manchala@gmail.com',
    ]
  },
  ReplyToAddresses=[
    'importantmail82@gmail.com',
  ],
  Template='test-template',
  TemplateData='{ \"REPLACEMENT_TAG_NAME\":\"REPLACEMENT_VALUE\" }'
)

print(response)

We can see the response as below,
jagadishm@[/Volumes/Work/aws/code]: python send-email-ses.py 
{'MessageId': '01020170188e21d7-8ebe8322-58e1-4b5b-97cd-03f5d4515ce1-000000', 'ResponseMetadata': {'RequestId': '29ee7014-ba5b-4cdf-ba5f-fdeb9afc3831', 'HTTPStatusCode': 200, 'HTTPHeaders': {'x-amzn-requestid': '29ee7014-ba5b-4cdf-ba5f-fdeb9afc3831', 'content-type': 'text/xml', 'content-length': '362', 'date': 'Thu, 06 Feb 2020 03:32:12 GMT'}, 'RetryAttempts': 0}}

we can see email coming to email account as below,

No comments :

Post a Comment