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()
Metadata
Release files for emailconstructor 2.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| emailconstructor-2.0.1.tar.gz | 12.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| emailconstructor-2.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 23.0 kB
Release files / emailconstructor-2.0.1.tar.gz
| Download URL | emailconstructor-2.0.1.tar.gz |
|---|---|
| Size | 12.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5dc978af4d59d796086149c39fec5a520c0e12020ac2d040bfe7f0f67b267505
|
|
BLAKE2b-256 checksum How to use checksums |
38d23b31b15b42c6684e0c5878e23bea09e2c695f920fac9166b581876cd5588
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.1.1 CPython/3.12.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 1, 2024.
Transparency logRelease files / emailconstructor-2.0.1-py3-none-any.whl
| Download URL | emailconstructor-2.0.1-py3-none-any.whl |
|---|---|
| Size | 10.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5503ec6fef903115e574af00eb6a00d41225f338842e9ea8a2d7009a543741df
|
|
BLAKE2b-256 checksum How to use checksums |
d1ff8078f807b8c040b55951202d73698f285feb26b35e3be03b2e73bd90c5db
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.1.1 CPython/3.12.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 1, 2024.
Transparency log