Package to dynamically construct and send HTML-based emails.
Project description
Introduction
emailconstructor
is a simple tool for dynamically constructing and sending HTML-based emails. Some of it's high level features include:
- Composing email content using text and styled HTML elements.
- Adding email file attachments.
- Adding styled inline images.
- Creating HTML tables or lists from data sets with optional conditional styling applied per row or per data item.
emailconstructor
was originally written for the specific use case of quickly building and sending conditional alert emails containing large amounts of tabular data, however it is suitable to be used in a wide variety of other applications.
Installation
pip install emailconstructor
Usage
All functionality is contained within the EmailConstructor
class.
from emailconstructor import EmailConstructor
email = EmailConstructor(
smtp_server_url="smtpserver.domain.com",
smtp_server_port=100,
sender_address="senderemail@domain.com"
)
The subject, primary recipients, and CC recipients can all be set during initialization, or can be set/modified after.
email.subject = "Your subject"
email.primary_recipients.extend(["recipient1@domain.com", "recipient2@domain.com"])
email.cc_recipients.append("recipient3@domain.com")
Content can be added to the email using the add
method while HTML elements are opened and closed using context manager classes.
email.add("This is some basic text followed by line breaks.")
email.line_break(count=3)
with email.span({"color": "red"}):
email.add("This text is inside the span tag and will appear red.")
with email.div({"font-size": "30px"}):
email.add("This text will appear in a large font ")
with email.span({"background-color": "yellow"}):
email.add("and this text will be highlighted.")
Tables and bullet lists can be easily created from data sets.
list_data = ["bullet 1", "bullet 2", "bullet 3"]
# Builds a simple <ul> bulleted list from the data
email.build_list(list_data)
table_data = [
OrderedDict({"column1": 1, "column2": 2}),
OrderedDict({"column1": 3, "column2": 4}),
OrderedDict({"column1": 5, "column2": 6}),
]
get_cell_style = lambda cell: {"color": "blue"} if cell % 2 == 0 else {}
# Builds a table with bolded headers and with even numbered cells colored blue
email.build_table(
table_data, header_style={"font-weight": "bold"}, get_cell_style=get_cell_style
)
Inline images and file attachments can be added by path.
email.add_inline_image("my_image.jpg", {"width": "100%"})
email.attachments.append("my_attachment.pdf")
Once finished, emails can be sent with the send
method. A call to reset
will clear out the email body, recipients, and subject so a new email can be built and sent.
email.send("emailpassword")
email.reset()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for emailconstructor-2.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d9f3137329599034ee22ba45457187cf109d6d6e29ccb32c88dd57c47908b395 |
|
MD5 | 91737278310f3c406550bc360ebb355d |
|
BLAKE2b-256 | cf869ffa1de29bf392f3c27e78232eb78d5a2eff3aebb4fc95770a22b95eed1f |