Skip to main content

Simple HTML e-mail generator

Project description

EMailBuilder

Simple HTML e-mail template builder python library. Allows for embedded images and matplotlib charts.

Table of Contents


Installation

You can install the latest release with pip:

pip install emailbuilder

Or build and install the development version yourself:

git clone https://github.com/coheleth/emailbuilder &&\
pip install ./emailbuilder

Usage

Import EmailBuilder:

import emailbuilder as eb

Create an emailbuilder.EMail object and append elements to it:

# Create eb.EMail object
# (see the "EMail class" section for more details)
email = eb.EMail(
    "EMail Subject",
    "sender@example.com",
    "receiver@example.com"
)

#  Append components to the eb.EMail object
# (see the "Components" section for more details)
email.append(eb.Header("Hello World!"))
email.append(eb.Paragraph("Lorem ipsum dolor sit amet consectetur"))

You can also append matplotlib figures:

plt.figure()
plt.plot([1, 2])
plt.title("Example")

email.append(eb.Figure(plt.gcf(), "example-figure"))

Then, you can send the e-mail via your preferred method. For instance:

s = smtplib.SMTP(...)

...

s.send_message(email.message())
s.quit()

The EMail Class/Object

This object is used for setting the information relating to the e-mail, and provides methods to render the e-mail contents.

Attributes

Subject

The e-mail's subject, as a string

Sender

The sender's e-mail, as a string

Receiver

The receiver(s)'s e-mail(s), as either a string or a list

Style

A dictionary containing the basic style rules for the e-mail. (More information concerning styling to be added)

Methods

email.append()

email.append(component)

Appends a component to the end of the e-mail

email.attach()

with open("file.png", "rb") as f:
  email.attach(
      item = f.read(),
      type = "image",
      extension = "png",
      cid = "my_image",
      mime = MIMEImage(f.read()) # Optional
  )

Adds an attachment to the e-mail.

email.html()

print(email.html())

Returns the e-mail as HTML.

email.plain() (W.I.P.)

print(email.plain())

Returns the e-mail as plain text.

email.message()

s.send_message(email.message())

Returns the e-mail as a EmailMessage object.


Components

Below are the included components in the emailbuilder library.

Basic Elements

Header

eb.Header(
    content: str,
    style: dict = {}
)

An <h1> element, with the text from the content parameter.

Paragraph

eb.Paragraph(
    content: str,
    style: dict = {}
)

A single paragraph, with the text from the content parameter.

Embedabbles

Image

eb.Image(
    src: str,
    alt: str = "",
    cid: str = <file name>,
    style: dict = {}
)

An embedded image, loaded from the src path. Alternative text, used for text-only e-mails is passed through the alt parameter.

ImageRaw

eb.ImageRaw(
    image: bytes,
    extension: str,
    alt: str = "",
    cid: str = <hashed bytes>,
    style: dict = {}
)

An embedded image, loaded as bytes from the image parameter. An image format must be provided through the extension parameter.

Figure

eb.Figure(
    figure: matplotlib figure,
    alt: str,
    style: dict = {},
    kwargs: dict = {}
)

An embedded MatPlotLib figure. Custom arguments for the savefig function can be passed through the kwargs parameter.

Containers

Container

eb.Container(
    style: dict = {}
)

A <div> element. Items can be appended with the append(item) method, just like with the EMail object.


To-Do

  • Write basic usage guide
  • Add win32com.client outlook support
  • Improve classes and functions documentation
  • Add table component
  • Document style argument
  • Improve plain text function

Project details


Download files

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

Source Distribution

emailbuilder-0.1.3.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

emailbuilder-0.1.3-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file emailbuilder-0.1.3.tar.gz.

File metadata

  • Download URL: emailbuilder-0.1.3.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for emailbuilder-0.1.3.tar.gz
Algorithm Hash digest
SHA256 f210937bff79f6f0489aa69c62e896c4c63151d2175c2d58492e5e5978562998
MD5 677a75f9bdb49455af50daef30d40a92
BLAKE2b-256 946b4742a6738a79a21526b10fb47066a3d3a6b7ede8e228cd3c1f1a2dd272cf

See more details on using hashes here.

File details

Details for the file emailbuilder-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for emailbuilder-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 6ee3c4aeddc11b90e40c9f1d036ac23b227f6e2a39d8587f7c540575c41c6696
MD5 c64c71a1eb60720c715616e029a6c617
BLAKE2b-256 fb85f7f09136b8e55cd4b0fbdacf6672ae00d73ccc62c44ff9542ea84eb7b76e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page