[AWS SES] [SES API V2] Code Syntax to Send Email attachments (up to 40MB) using Amazon SES API v2

Published by projectsflix on

Hello everyone!

We all know that we can send emails up to 10 MB using Amazon SES. Recently, Amazon SES updated its default email size limit to 40 MB when we use Amazon SES API v2 or SMTP to send emails.

Today, I decided to test sending emails of size greater than 10 MB.

I went ahead and downloaded the python code from the AWS official documentation and updated it with my ARN, FROM address, To address, attachment path, etc.,

Then I ran the python code and it gave me the same error as before. i.e, the same error I used to get before the default email size limit was increased by the SES.

After a few tests, I noticed that the code is still using the SES v1 API and not the SES v2 API.

The default email size limit is only 10 MB when we use SES v1 API.

Therefore, I checked the AWS SES latest documentation and modified my code, and updated it to use SES v2 API. Then, I tested sending my email with a 20 MB attachment and It was delivered successfully.

Here is the python SDK code used by me to send emails of sizes up to 40 MB using Amazon SES v2 API.

Python code syntax to send email attachments using SES V2 API:

import os
import boto3
from botocore.exceptions import ClientError
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
# Replace [email protected] with your "From" address.
# This address must be verified with Amazon SES.
SENDER = "Sender Name <[email protected]>"
# Replace  with your "arn:aws:ses:us-west-2:1234567890:identity/[email protected]" verified identity ARN.
SOURCEARN="arn:aws:ses:us-west-2:1234567890:identity/[email protected]"
# Replace [email protected] with a "To" address. If your account 
# is still in the sandbox, this address must be verified.
RECIPIENT = "[email protected]"
# Specify a configuration set. If you do not want to use a configuration
# set, comment the following variable, and the 
# ConfigurationSetName=CONFIGURATION_SET argument below.
# If necessary, replace us-west-2 with the AWS Region you're using for Amazon SES.
AWS_REGION = "us-west-2"
# The subject line for the email.
SUBJECT = "Customer service contact info"
# The full path to the file that will be attached to the email.
ATTACHMENT = "path/to/customers-to-contact.xlsx"
# The email body for recipients with non-HTML email clients.
BODY_TEXT = "Hello,\r\nPlease see the attached file for a list of customers to contact."
# The HTML body of the email.
BODY_HTML = """\
<p>Please see the attached file for a list of customers to contact.</p>
# The character encoding for the email.
CHARSET = "utf-8"
# Create a new SES resource and specify a region.
client = boto3.client('sesv2',region_name=AWS_REGION)
# Create a multipart/mixed parent container.
msg = MIMEMultipart('mixed')
# Add subject, from and to lines.
msg['Subject'] = SUBJECT 
msg['From'] = SENDER 
msg['To'] = RECIPIENT
# Create a multipart/alternative child container.
msg_body = MIMEMultipart('alternative')
# Encode the text and HTML content and set the character encoding. This step is
# necessary if you're sending a message with characters outside the ASCII range.
textpart = MIMEText(BODY_TEXT.encode(CHARSET), 'plain', CHARSET)
htmlpart = MIMEText(BODY_HTML.encode(CHARSET), 'html', CHARSET)
# Add the text and HTML parts to the child container.
# Define the attachment part and encode it using MIMEApplication.
att = MIMEApplication(open(ATTACHMENT, 'rb').read())
# Add a header to tell the email client to treat this part as an attachment,
# and to give the attachment a name.
# Attach the multipart/alternative child container to the multipart/mixed
# parent container.
# Add the attachment to the parent container.
    #Provide the contents of the email.
    response = client.send_email(
            'ToAddresses': [RECIPIENT]
            'Raw': {
                'Data': msg.as_string()
        # Specify a configuration set. If you do not want to use a configuration
        # set, comment the following variable, and the 
        # ConfigurationSetName=CONFIGURATION_SET argument below.
# Display an error if something goes wrong.	
except ClientError as e:
    print("Email sent! Message ID:"),

Using the above python code, you can send emails up to 40 MB using AWS SES. Visit this documentation for modifying this code to suit your need using SES API v2.

Use the comment section below to post your questions/doubts.

Categories: AWS SES


Leave a Reply

Avatar placeholder

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.