How to send Bulk Email with personalised PDF attachments

0

Can anyone help advise how i go about sending bulk email with personalised email attachments without using individual Http requests.

  • I have personalised PDF documents i need to attach to emails
  • I need to send the emails out in bulk

My current provider only allows individual Http requests, and if I am sending to 30 000 people, it becomes a issue with 30,000 Http requests. This causes rate limiting issues.

Is there a way to send bulk emails with personalised attachments in AWS without using individual Http requests?

Bart1GV
asked 10 months ago282 views
2 Answers
1
Accepted Answer

Yes it does indeed! SES is built specifically for this. If you are using python as IO showed above here are some you can look at:

  1. Jinja2: Jinja2 is a powerful and widely used templating engine for Python. It provides a flexible syntax for creating dynamic HTML templates. You can use Jinja2 to define your email templates and easily render them with dynamic data in your Python code.
  2. Mako: Mako is another templating engine for Python that allows you to create HTML templates. It provides a concise and easy-to-use syntax for generating dynamic content. Mako templates can be used to create email templates and render them with your Python code.
  3. Django Templates: If you're already familiar with Django, you can leverage its template system for creating email templates. Django provides a robust and feature-rich template engine that can be used outside of the Django web framework as well. You can use Django templates to define your email templates and render them using Django's template rendering API.
  4. Handlebars: Handlebars is a popular templating language that can be used with various programming languages, including Python. It provides a simple syntax for creating HTML templates. You can use a Python library like pybars3 to render Handlebars templates with dynamic data.

Let me know if these solve the issue or I can keep tracking it unless you accept one of these as working answer on your side.

-Zac

profile picture
Zac Dan
answered 10 months ago
profile pictureAWS
EXPERT
reviewed 10 months ago
1

Not sure if you had any more constraints on the problem, but I think you could build a solution from SES and Lambda. One possible workflow could be:

  1. Generate Personalized PDFs: Use whatever method you have for generating these PDFs and store each PDF in an S3 bucket with a filename that corresponds to the recipient's email address or other unique identifier. This way, you can programmatically associate each recipient with their specific PDF.
  2. Create a Recipient List: You'll need a list of recipients and the corresponding S3 links for their PDFs. This list can be an Excel sheet, CSV file, or a database table, whatever works best for you.
  3. Use Lambda to Send Emails: Create a Lambda function that will read from your recipient list, pull the appropriate PDF from S3, and then use SES to send the email.

Lambda could look something like this:

import boto3
import botocore

ses = boto3.client('ses')
s3 = boto3.resource('s3')

def send_email_with_pdf(recipient, pdf_filename):
    try:
        # Get PDF from S3
        bucket_name = 'your-bucket-name'
        s3_object = s3.Object(bucket_name, pdf_filename)
        pdf_file = s3_object.get()['Body'].read()

        # Create SES email
        email = {
            'Source': 'your-email@example.com',
            'Destination': {
                'ToAddresses': [recipient],
            },
            'Message': {
                'Subject': {
                    'Data': 'Your Subject',
                },
                'Body': {
                    'Html': {
                        'Data': 'Your Email Body',
                    }
                }
            },
            'Attachments': [{
                'Name': pdf_filename,
                'Data': pdf_file,
                'ContentType': 'application/pdf',
            }]
        }

        # Send SES email
        ses.send_email(**email)

    except botocore.exceptions.ClientError as e:
        print(e.response['Error']['Message'])
    else:
        print(f'Email sent to {recipient}!')

def lambda_handler(event, context):
    # This part depends on how your recipient list is stored
    # But you want to loop through each recipient
    for recipient in recipients:
        # Retrieve their PDF filename from your data storage
        pdf_filename = get_pdf_filename_for_recipient(recipient)
        # Send them an email with their PDF
        send_email_with_pdf(recipient, pdf_filename)

profile picture
Zac Dan
answered 10 months ago
  • Hi Zac, Thank you very much for your response and helpful feedback. This would work well and we will definately try.

    Do you know if the ability to send personalised attachments in bulk will work with AWS? I.e can SES handle bulk sends with personalised attachments?

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions