Skip to content

Send Bulk Email with PDF Attachment Using Python and AWS SES

Python 107 Views 5 min read Updated
Send Bulk Email with PDF Attachment Using Python and AWS SES - cover image

Send Bulk Email with PDF Attachment Using Python and AWS SES

This tutorial shows how to send an email to a list of recipients using Python and AWS SES SMTP, with a PDF file attached to every email.

Important: Send email only to recipients who are authorized to receive it. Follow AWS SES sending limits and applicable anti-spam requirements. Never publish real SMTP credentials.

What This Script Does

  1. Connects to AWS SES using SMTP.
  2. Reads recipients from list_of_emails.txt.
  3. Creates a plain-text email.
  4. Attaches a PDF file.
  5. Sends the email to each recipient.
  6. Logs successful and failed sends.

1. Prepare the Files

Keep the Python script and recipient list in the same directory:

BulkEmail/
├── bulk_email_attachment.py
├── list_of_emails.txt
└── email_send_status.txt

The PDF can be stored at any path accessible by the script.

2. Create the Email List

Create list_of_emails.txt with one email address per line:

student1@example.com
student2@example.com
student3@example.com

3. Configure AWS SES

Update these values in the script:

aws_smtp_server = "email-smtp.ap-south-1.amazonaws.com"
aws_smtp_port = 587

aws_smtp_username = "YOUR_SES_SMTP_USERNAME"
aws_smtp_password = "YOUR_SES_SMTP_PASSWORD"

sender_email = "Customer Help Desk <helpdesk@yourdomain.com>"

Also update the PDF path:

pdf_file_path = r"E:\scripts\customer\E-Library_in_customer_Portal.pdf"
Security Note: Do not publish real SES SMTP usernames or passwords. Use environment variables, AWS Secrets Manager, or another secure credential store in production.

4. Complete Python Script

Save the following as bulk_email_attachment.py:

import os
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
from datetime import datetime

# AWS SES SMTP settings
aws_smtp_server = "email-smtp.ap-south-1.amazonaws.com"
aws_smtp_port = 587

aws_smtp_username = "YOUR_SES_SMTP_USERNAME"
aws_smtp_password = "YOUR_SES_SMTP_PASSWORD"

sender_email = "Customer Help Desk <helpdesk@yourdomain.com>"

subject = "Regarding Use of E-Library in Customer Portal"

body = """Dear Student,

Please find attached the step-by-step process to use the E-Library platform.

Regards,
Customer Support Team
"""

# PDF attachment
pdf_file_path = r"E:\scripts\customer\E-Library_in_customer_Portal.pdf"
pdf_file_name = os.path.basename(pdf_file_path)

# Sending status log
output_file = "email_send_status.txt"


def send_email(to_email):
    msg = MIMEMultipart()

    msg["From"] = sender_email
    msg["To"] = to_email
    msg["Subject"] = subject

    # Email body
    msg.attach(MIMEText(body, "plain"))

    # Attach PDF
    with open(pdf_file_path, "rb") as attachment:

        part = MIMEBase(
            "application",
            "octet-stream"
        )

        part.set_payload(attachment.read())

    encoders.encode_base64(part)

    part.add_header(
        "Content-Disposition",
        f"attachment; filename={pdf_file_name}"
    )

    msg.attach(part)

    # Connect to AWS SES SMTP
    with smtplib.SMTP(
        aws_smtp_server,
        aws_smtp_port
    ) as server:

        server.starttls()

        server.login(
            aws_smtp_username,
            aws_smtp_password
        )

        server.send_message(msg)


# Read email list
with open("list_of_emails.txt", "r") as file:
    with open(output_file, "a") as output:

        for line in file:

            email = line.strip()

            if not email:
                continue

            try:

                send_email(email)

                status = (
                    f"{datetime.now():%Y-%m-%d %H:%M:%S} - "
                    f"Email sent successfully to {email}"
                )

                print(status)
                output.write(status + "\n")

            except Exception as e:

                status = (
                    f"{datetime.now():%Y-%m-%d %H:%M:%S} - "
                    f"Failed to send email to {email}: {e}"
                )

                print(status)
                output.write(status + "\n")

5. How the Script Works

Read the Recipient List

with open("list_of_emails.txt", "r") as file:

The script reads one recipient at a time from the email list.

Create the Email

msg = MIMEMultipart()
msg["From"] = sender_email
msg["To"] = to_email
msg["Subject"] = subject

The message contains the sender, recipient, subject, and body.

Attach the PDF

with open(pdf_file_path, "rb") as attachment:
    part = MIMEBase("application", "octet-stream")
    part.set_payload(attachment.read())

encoders.encode_base64(part)

The PDF is read in binary mode, encoded for email transport, and added as an attachment.

Send Through AWS SES

server.starttls()
server.login(
    aws_smtp_username,
    aws_smtp_password
)

server.send_message(msg)

The script enables TLS, authenticates with the SES SMTP credentials, and sends the completed message.

6. Run the Script

python bulk_email_attachment.py

The script processes every address in list_of_emails.txt and attaches the configured PDF to each email.

7. Example Output

2026-09-18 14:30:10 - Email sent successfully to student1@example.com
2026-09-18 14:30:11 - Email sent successfully to student2@example.com
2026-09-18 14:30:12 - Failed to send email to student3@example.com: SMTP error

Quick Reference

# Run the script
python bulk_email_attachment.py

Important Notes

  • Verify the sender identity in AWS SES.
  • Make sure the PDF path is correct.
  • Keep the attachment size within your email provider's limits.
  • Follow AWS SES sending quotas and rate limits.
  • Handle bounces and complaints appropriately.
  • Do not send to purchased or scraped email lists.
  • Keep SES SMTP credentials secret.

Conclusion

Python can be used with AWS SES SMTP to send bulk email messages with a PDF attachment. The script keeps the process simple by reading recipients from a text file, attaching the same PDF to each message, and logging the result of each send attempt.

Discussion (0)