Sending E-mail Using Amazon SES With PDF Attachment

Doesn’t it look like the good old email is standing the test of time — tall and strong? Whether it’s sending a message to a colleague or writing down a well-wor

Sending E-mail Using Amazon SES With PDF Attachment

Doesn’t it look like the good old email is standing the test of time — tall and strong? Whether it’s sending a message to a colleague or writing down a well-wor

Doesn’t it look like the good old email is standing the test of time — tall and strong? Whether it’s sending a message to a colleague or writing down a well-worded business proposal to a client, the email never goes out of style! 

Even if you need to share an image with your recipient — that’s where email attachments, particularly sending a PDF document come in handy. Are you looking for a way to easily send high-volume emails for your business, without breaking the bank? Look no further than email using Amazon SES!  In this exciting blog, we'll dive into the world of Amazon SES and sending email with PDF attachment using Amazon SES.

Specifically, we'll be using AWS Lambda to create a script that will automatically send your emails when triggered. With this powerful combination of tools, you can streamline your email communication process and save time and effort. So buckle up and get ready to learn how to send professional-looking email using Amazon SES with ease!

 

What is Amazon SES?

Amazon ses logo

As a cloud-based email service provided by Amazon, SES enables businesses of all sizes to send transactional emails, marketing messages, and other types of communication with ease. But that's not all - SES is also scalable and reliable, ensuring that your messages get delivered to your recipients without fail. Additionally, sending email with PDF attachment using Amazon SES offers a seamless solution for sharing important documents with your customers or clients.

 

Implementation

In this blog, we will only focus on Amazon SES email sending with PDF attachment and not how to set up userpool.

Amazon SES is a cost-effective email service that charges only for the number of emails sent and received. If you’re wondering how to send bulk email with Amazon SES, know that it is designed to be highly scalable, which means it can handle a large volume of emails without any issues.
 

Step 1: Set up Amazon SES

Now, you are a step closer to Amazon SES email sending with attached PDF.

 

Step 2: Create an IAM user for SES

We need to give  lambda role and policies to use cognito-idp services and ses services

    ## policy for allowing cognito-idp start ##

        lambda_role.add_to_policy(

            statement=iam.PolicyStatement(

                actions=['cognito-idp:*'],

                resources=['*']

            )

        )

        ## policy for allowing cognito-idp end ##

 

        ## policy to allow using ses start ##

        lambda_role.add_to_policy(

            statement=iam.PolicyStatement(

                actions=['ses:*'],

                resources=['*']

            )

        )

        ## policy to allow using ses end ##

 

These are the two services we are using and we are providing access to those resources for our lambda function.

 

Step 3: Creating Lambda Function for Sending Email 

Now, let’s move to sending email with Amazon SES using lambda function.

The next step is to create a lambda function that will send the email with the PDF attachment. Follow the steps below to create a lambda function:

 

Taking receiver email id from cognito user pool

Now for the Amazon SES email sending with PDF attachment, we will use the pdf file stored in s3 bucket, which we will attach to our email to send it as an attachment.

AWS SES offers two functions, namely send_email and send_raw_email, for sending emails. When it comes to sending an email with a PDF attachment, it is necessary to utilize the send_raw_email function to ensure successful delivery of the email along with the attached PDF file.
 

For the Amazon SES send email with attachment, here's the sample code that demonstrates how to send an email with a PDF attachment using Amazon SES:

 

from os import environ

import json

import boto3

import time

from datetime import datetime

from botocore.exceptions import ClientError  #added for email

from email.mime.multipart impor