email-builder
This is a module to make it simple to send emails. The code follows the builder design pattern and the
email code is completely separated from the client used to actually send the email, making it easy to switch to
a new client if we ever need to move away from SES for whatever reason.
Sample Usage
Install
pip install easy-email-builder
Setup
import email_builder
Send an email from Gmail
import email_builder
client = email_builder.clients.Gmail(password="<gmail APP password>")
email = (
email_builder.Email()
.sender("your.email@gmail.com")
.to("someones.email@gmail.com")
.cc("copy.email@gmail.com")
.bcc("another.copy.email@gmail.com")
.subject("test email sent from my python lib")
.text("hello world")
)
email.send(client)
Send an email Amazon SES with just some html as the body
import email_builder
client = email_builder.clients.SES()
email = (
email_builder.Email()
.sender("my.email@company.com")
.to("recipient.1@company.com")
.to("recipient.2@company.com")
.subject("test email sent from my python lib")
.html("<h1> hello world </h1>")
)
email.send(email_builder.clients.SES())
Send an email with attachments
import email_builder
with open("report.csv", "rb") as f:
data = f.read()
client = email_builder.clients.SES()
email = (
email_builder.Email()
.sender("my.email@company.com")
.to("recipient.1@company.com")
.to("recipient.2@company.com")
.subject("test email sent from my python lib")
.markdown("# hello world")
.attachment(email_builder.Attachment("report.csv", data))
.attachment(email_builder.Attachment("test2.json", """{"key": "value"}"""))
).send(email_builder.clients.SES())
Email Options
import email_builder
with open("report.csv", "rb") as f:
data = f.read()
client = email_builder.clients.SES()
import boto3
session = boto3.Session(region="us-west-2", etc)
client = email_builder.clients.SES(session)
client = email_builder.clients.SES(password="<password>")
email = (
email_builder.Email()
.sender("my.email@company.com")
.to("recipient.1@company.com")
.to("recipient.2@company.com")
.cc("recipient.1@company.com")
.cc("recipient.2@company.com")
.bcc("recipient.1@company.com")
.bcc("recipient.2@company.com")
.subject("test email sent from my python lib")
.html("<h1> hello world </h1>")
.markdown("# hello world")
.text("hello world")
.attachment(email_builder.Attachment("report.csv", data))
.attachment(email_builder.Attachment("test2.json", """{"key": "value"}"""))
)
result = email.send(client)
print(result)
Success result:
{
"status": "passed",
"error": None
}
Failure result:
{
"status": "failed",
"error": Exception(...)
}