Skip to main content

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

# builder syntax
# when using gmail, you need an "App Password". follow this link if you need help creating that:
# 
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

# builder syntax 
import etl_utils

# this SES class accepts a boto3 session if you want to customize the setup
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

# note that the contents of the attachments can be either string or bytestring 
import email_builder

with open("report.csv", "rb") as f:
  report_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")
    .html("<h1> hello world </h1>")
    .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

# --- client types are SES or Gmail --- #

# Amazon SES that can accept a session object as needed
client = email_builder.clients.SES()
#or
import boto3
session = boto3.Session(...)
client = email_builder.clients.SES(session)

# Gmail that accepts a password and options to configure port/url if needed
client = email_builder.clients.SES(password="<password>")

# --- email options --- #
email = (
  email_builder.Email()

    # sender(sender:str) -> only one sender allowed, no chaining allowed on sender
    .sender("my.email@company.com")

    # to(*recipients:str) -> you can add as many as you want here, and chain to()
    #                        calls successively
    .to("recipient.1@company.com")
    .to("recipient.2@company.com")

    # cc(*recipients:str) -> you can add as many as you want here, and chain to()
    #                        calls successively
    .cc("recipient.1@company.com")
    .cc("recipient.2@company.com")

    # bcc(*recipients:str) -> you can add as many as you want here, and chain to()
    #                        calls successively
    .bcc("recipient.1@company.com")
    .bcc("recipient.2@company.com")

    # subject(subject:str) -> only one subject line allowed
    .subject("test email sent from my python lib")

    # --- EMAIL BODY --- #
    # need to choose between either html or text, can't use both

    # html(body:str) -> renders the given html in the email body, no chaining
    .html("<h1> hello world </h1>")

    # test(body:str) -> adds the given text to the email body as plain text
    .text("hello world")

    # attachment(attachment:email_builder.Attchment) -> chain as many of these as you want
    .attachment(email_builder.Attachment("report.csv", data))
    .attachment(email_builder.Attachment("test2.json", """{"key": "value"}"""))

)

# result contains a dictionary with status in the body
result = email.send(client)
print(result)

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

easy-email-builder-1.0.1.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

easy_email_builder-1.0.1-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file easy-email-builder-1.0.1.tar.gz.

File metadata

  • Download URL: easy-email-builder-1.0.1.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for easy-email-builder-1.0.1.tar.gz
Algorithm Hash digest
SHA256 42d2186528cfe739dbcb4c28943d8e2ae207bde34ba1ae26e8fdac7fa901afdd
MD5 72f69ac5799135672e3e1e18247e58cc
BLAKE2b-256 cd56c9e835b7e618fa8ce236cbe40c07655f66378c532d5e99212656c0783915

See more details on using hashes here.

File details

Details for the file easy_email_builder-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for easy_email_builder-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 45a1827abe04848de5e9679a01141cf92d2426d3897f74252ff9a7f93017127a
MD5 35d00b6d1d09c0f7f4365f5f25966619
BLAKE2b-256 9be5d7e8858af4bd6d569b1db3202cd3c471e34720af0a88880af0a64e834299

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page