ABSTRACT

Sending bulk email through normal workspaces like Gmail, Hotmail, etc., that takes more time to receive email respective recipient or customers and send email individually, here we will sort out that problem, bulk email sending through amazon simple email services using amazon lambda functions, Usually normal compose emails are delivered within 15 seconds to the recipients. But sometimes, it may take a few couple minutes, amazon simple email services delivered the email instantly(within milliseconds or 2 seconds) to the recipients and present the sending message using HTML. To attach a file to an email, you have to encode the attachment using base64 encoding. Attachments are typically placed in dedicated MIME message parts.

SYSTEM ARCHITECTURE

LIST OF MODULES

  • Create a role in Identity and access management 
  • Deploy & Run the program in the AWS Lambda Functions
  • Create identity in Amazon Simple Email Service 
  • Testing the python program in Amazon Lambda Functions 
  • Send email to customers using Python and HTML 
  • Sending a raw email to customers using MIME code – SEE IN NEXT BLOG

CREATE A ROLE IN IDENTITY AND ACCESS MANAGEMENT

An IAM role is an IAM identity that you can create in your account that has specific permissions. An IAM role is similar to an IAM user, in that it is an AWS identity with permission policies that determine what the identity can and cannot do in AWS.

DEPLOY & RUN THE PROGRAM IN THE AWS LAMBDA FUNCTIONS

AWS Lambda is a serverless, event-driven compute service that lets you run code for virtually any type of application or backend service without provisioning or managing servers. You can trigger Lambda from over 200 AWS services and software as a service (SaaS) applications and only pay for what you use.

CREATE IDENTITY IN AMAZON SIMPLE EMAIL SERVICE

In Amazon SES, a verified identity is a domain or email address that you use to send or receive email. Before you can send an email using Amazon SES, you must create and verify each identity that you’re going to use as a “From”, “Source”, “Sender”, or “Return-Path” address. Verifying an identity with Amazon SES confirms that you own it and helps prevent unauthorized use. If your account is still in the Amazon SES sandbox, you also need to verify any email addresses which you plan on sending email to, unless you’re sending to test inboxes provided by the Amazon SES mailbox simulator. For more information, see Using the mailbox simulator manually. You can create an identity by using the Amazon SES console or the Amazon SES API. The identity verification process depends on which type of identity you choose to create.

TESTING THE PYTHON PROGRAM IN AMAZON LAMBDA FUNCTIONS

You can run Python code in AWS Lambda. Lambda provides runtimes for Python that run your code to process events. Your code runs in an environment that includes the SDK for Python (Boto3), with credentials from an AWS Identity and Access Management (IAM) role that you manage.

SEND EMAILS TO CUSTOMERS USING PYTHON AND HTML

Customer 1 (devanathan.s@ckcet.ac.in)

Customer 2 (prabakaran.s@ckcet.ac.in)

Sending email through Amazon SES using an AWS SDK

Normal Compose Email Receiving Time

16 Seconds 23 Milliseconds(Approximate) – This timing depends on my personal hardware and software.

Amazon SES Compose Email Sending and Receiving

3 Seconds 43 Milliseconds(Approximate) – This timing depends on my personal hardware and software but Amazon SES told compose email reflected on hosted server within milliseconds.

PROGRAM HOSTED IN AWS LAMBDA FUNCTION:

Here we have attached the source code file click on Source code button.

import boto3
from botocore.exceptions import ClientError

def send_email():
    SENDER = "prabakaranjeeva2000@gmail.com" # must be verified in AWS SES Email
    lst=["prabakaran.s@ckcet.ac.in","seenivasan.s@ckcet.ac.in","devanathan.s@ckcet.ac.in"]
    for i in range(0,len(lst)):
        RECIPIENT = lst[i] # must be verified in AWS SES Email

        # If necessary, replace us-west-2 with the AWS Region you're using for Amazon SES.
        AWS_REGION = "us-east-1"

        # The subject line for the email.
        SUBJECT = "This is test email for testing purpose..!!"

        # The email body for recipients with non-HTML email clients.
        BODY_TEXT = ("Hey Hi...\r\n"
                    "This email was sent with Amazon SES using the "
                    "AWS SDK for Python (Boto)."
                    )
                    
        # The HTML body of the email.
        BODY_HTML = """<html>
        <!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:o="urn:schemas-microsoft-com:office:office">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width,initial-scale=1">
  <meta name="x-apple-disable-message-reformatting">
  <title></title>
  <!--[if mso]>
  <noscript>
    <xml>
      <o:OfficeDocumentSettings>
        <o:PixelsPerInch>96</o:PixelsPerInch>
      </o:OfficeDocumentSettings>
    </xml>
  </noscript>
  <![endif]-->
  <style>
    table, td, div, h1, p {font-family: Arial, sans-serif;}
  </style>
</head>
<body style="margin:0;padding:0;">
  <table role="presentation" style="width:100%;border-collapse:collapse;border:0;border-spacing:0;background:#ffffff;">
    <tr>
      <td align="center" style="padding:0;">
        <table role="presentation" style="width:602px;border-collapse:collapse;border:1px solid #cccccc;border-spacing:0;text-align:left;">
          <tr>
            <td align="center" style="padding:40px 0 30px 0;background:#70bbd9;">
              <img src="http://www.innovativecodesacademy.in/wp-content/uploads/2021/04/FullColor_1280x1024_72dpi-768x614.png" alt="" width="300" style="height:auto;display:block;" />
            </td>
          </tr>
          <tr>
            <td style="padding:36px 30px 42px 30px;">
              <table role="presentation" style="width:100%;border-collapse:collapse;border:0;border-spacing:0;">
                <tr>
                  <td style="padding:0 0 36px 0;color:#153643;">
                    <h1 style="font-size:24px;margin:0 0 20px 0;font-family:Arial,sans-serif;">Welcome to Innovative Codes Academy !</h1>
                    <p style="margin:0 0 12px 0;font-size:16px;line-height:24px;font-family:Arial,sans-serif;">On this website, we will teach and give tutorials about the Programming language, Cloud Computing, Mobile Application Development, Web Application Development, Data Analytics in Tableau without using machine learning or deep learning, Online Application Form Filling, and learning some technology, Purely based on learning website.</p>
                    <p style="margin:0;font-size:16px;line-height:24px;font-family:Arial,sans-serif;"><a href="http://www.example.com" style="color:#ee4c50;text-decoration:underline;">"Fly to high"</a></p>
                  </td>
                </tr>
                <tr>
                  <td style="padding:0;">
                    <table role="presentation" style="width:100%;border-collapse:collapse;border:0;border-spacing:0;">
                      <tr>
                        <td style="width:260px;padding:0;vertical-align:top;color:#153643;">
                          <p style="margin:0 0 25px 0;font-size:16px;line-height:24px;font-family:Arial,sans-serif;"><img src="https://assets.codepen.io/210284/left.gif" alt="" width="260" style="height:auto;display:block;" /></p>
                          <p style="margin:0 0 12px 0;font-size:16px;line-height:24px;font-family:Arial,sans-serif;">Lorem ipsum dolor sit amet, consectetur adipiscing elit. In tempus adipiscing felis, sit amet blandit ipsum volutpat sed. Morbi porttitor, eget accumsan dictum, est nisi libero ultricies ipsum, in posuere mauris neque at erat.</p>
                          <p style="margin:0;font-size:16px;line-height:24px;font-family:Arial,sans-serif;"><a href="http://www.example.com" style="color:#ee4c50;text-decoration:underline;">Blandit ipsum volutpat sed</a></p>
                        </td>
                        <td style="width:20px;padding:0;font-size:0;line-height:0;">&nbsp;</td>
                        <td style="width:260px;padding:0;vertical-align:top;color:#153643;">
                          <p style="margin:0 0 25px 0;font-size:16px;line-height:24px;font-family:Arial,sans-serif;"><img src="https://assets.codepen.io/210284/right.gif" alt="" width="260" style="height:auto;display:block;" /></p>
                          <p style="margin:0 0 12px 0;font-size:16px;line-height:24px;font-family:Arial,sans-serif;">Morbi porttitor, eget est accumsan dictum, nisi libero ultricies ipsum, in posuere mauris neque at erat. Lorem ipsum dolor sit amet, consectetur adipiscing elit. In tempus adipiscing felis, sit amet blandit ipsum volutpat sed.</p>
                          <p style="margin:0;font-size:16px;line-height:24px;font-family:Arial,sans-serif;"><a href="http://www.example.com" style="color:#ee4c50;text-decoration:underline;">In tempus felis blandit</a></p>
                        </td>
                      </tr>
                    </table>
                  </td>
                </tr>
              </table>
            </td>
          </tr>
          <tr>
            <td style="padding:30px;background:#ee4c50;">
              <table role="presentation" style="width:100%;border-collapse:collapse;border:0;border-spacing:0;font-size:9px;font-family:Arial,sans-serif;">
                <tr>
                  <td style="padding:0;width:50%;" align="left">
                    <p style="margin:0;font-size:14px;line-height:16px;font-family:Arial,sans-serif;color:#ffffff;">
                      &reg; Someone, Somewhere 2021<br/><a href="http://www.example.com" style="color:#ffffff;text-decoration:underline;">Unsubscribe</a>
                    </p>
                  </td>
                  <td style="padding:0;width:50%;" align="right">
                    <table role="presentation" style="border-collapse:collapse;border:0;border-spacing:0;">
                      <tr>
                        <td style="padding:0 0 0 10px;width:38px;">
                          <a href="http://www.twitter.com/" style="color:#ffffff;"><img src="https://assets.codepen.io/210284/tw_1.png" alt="Twitter" width="38" style="height:auto;display:block;border:0;" /></a>
                        </td>
                        <td style="padding:0 0 0 10px;width:38px;">
                          <a href="http://www.facebook.com/" style="color:#ffffff;"><img src="https://assets.codepen.io/210284/fb_1.png" alt="Facebook" width="38" style="height:auto;display:block;border:0;" /></a>
                        </td>
                      </tr>
                    </table>
                  </td>
                </tr>
              </table>
            </td>
          </tr>
        </table>
      </td>
    </tr>
  </table>
</body>
</html>
                    """            

        # The character encoding for the email.
        CHARSET = "UTF-8"

        # Create a new SES resource and specify a region.
        client = boto3.client('ses',region_name=AWS_REGION)

        # Try to send the email.
        try:
            #Provide the contents of the email.
            response = client.send_email(
                Destination={
                    'ToAddresses': [
                        RECIPIENT,
                    ],
                },
                Message={
                    'Body': {
                        'Html': {
            
                            'Data': BODY_HTML
                        },
                        'Text': {
            
                            'Data': BODY_TEXT
                        },
                    },
                    'Subject': {

                        'Data': SUBJECT
                    },
                },
                Source=SENDER
            )
        # Display an error if something goes wrong.	
        except ClientError as e:
            print(e.response['Error']['Message'])
        else:
            print("Email sent! Message ID:"),
            print(response['MessageId'])

def lambda_handler(event, context):
    # TODO implement
    send_email()

Output Screen Shots:

Conclusion

Nowadays some organizations used email services on the cloud for sending bulk emails and file attachments in encoding format, amazon simple email services delivered the email instantly(within milliseconds or 2 seconds) to the recipients and present the sending message using HTML. To attach a file to an email, you have to encode the attachment using base64 encoding. Attachments are typically placed in dedicated MIME message parts. Finally, we are sending bulk emails through amazon simple email service using AWS lambda functions.

Leave a Reply

Your email address will not be published. Required fields are marked *