API¶
Anything documented here is part of the public API that Email-Simplified provides, unless otherwise indicated. Anything not documented here is considered internal or private and may change at any time.
Handlers¶
- email_simplified.get_handler_class(name)¶
Get a
EmailHandlerimplementation by name.First looks up if the name is in the
email_simplified.handlerentry point namespace, and loads the referenced class if it is. Libraries providing handler implementations should register a handler’s name so that it’s automatically available when installed.If an entry point is not found, attempts to treat the name as an import path in the form
module.submodule:handler_class. This is useful for users writing their own handler locally where adding an entry point may not be possible. May also be passed an actual handler class, in which case it’s returned directly.- Parameters:
name (str | type[EmailHandler]) – The registered entry point name, import path, or handler class to load and return.
- Return type:
- class email_simplified.SMTPEmailHandler(*, host=None, port=None, use_tls=None, use_starttls=False, tls_context=None, timeout=None, username=None, password=None, default_from=None, recipients_per_message=None)¶
Email handler that sends with SMTP using Python’s built-in
smtplib. All arguments are optional,smtplib.SMTPuses default values if something is not given.- Parameters:
host (str | None) – Host to connect to.
port (int | None) – Port to connect to.
use_tls (bool | None) – The connection should be established with TLS. Default is
Trueifportis 465.use_starttls (bool) – The connection should be upgraded to TLS after establishing a plain connection first. You should prefer
use_tlsinstead if your SMTP server supports it.tls_context (SSLContext | None) – An
ssl.SSLContextto use whenuse_tlsoruse_starttlsis enabled. Defaults tossl.create_default_context(), which enables verifying any server cert trusted by the OS. You should only need to change this if the server uses a custom cert, or needs a client cert sent as well.timeout (float | None) – Connection timeout. Default is no timeout.
username (str | None) – Username to log in with.
password (str | None) – Password to log in with.
default_from (str | None) – Default address to send from if the
SenderorFromheader is not set on a message.recipients_per_message (int | None) – If a message has more than this number of recipients, split the send calls to batches of this size. This is to support servers that have a limit configured. By default, no batching is done.
- host¶
Host to connect to.
- port¶
Port to connect to.
- timeout¶
Connection timeout.
- username¶
Username to log in with.
- password¶
Password to log in with.
- default_from¶
Default address to send from if the
SenderorFromheader is not set on a message.
- recipients_per_message¶
If a message has more than this number of recipients, split the low-level send calls to batches of this size. This is to support servers that have a limit configured. By default no batching is done.
- use_tls¶
The connection should be established with TLS.
- use_starttls: bool¶
The connection should be upgraded to TLS after establishing a plain connection first.
- tls_context¶
An
ssl.SSLContextto use whenuse_tlsoruse_starttlsis enabled.
- classmethod from_config(config)¶
Create a handler from a config dict. Config keys match the arguments to
SMTPEmailHandler, and are all optional.
- connect()¶
Context manager that creates an
smtplib.SMTPclient, connects, logs in, then closes when exiting the block.
- send(messages)¶
Send one or more email messages.
Messages should typically be
Message. However, they may also beemail.message.EmailMessagefor cases where a non-standard MIME construction is needed. It is up to the handler to decide whether that is supported based on what their service is capable of.- Parameters:
messages (list[Message | EmailMessage]) – A list of messages to send.
- Return type:
None
- class email_simplified.TestEmailHandler¶
Email handler that appends messages to a list rather than sending them. Useful when testing.
- outbox: list[Message | EmailMessage]¶
List of messages that have been sent with this handler.
- classmethod from_config(config)¶
Create an instance of this handler using arguments from
configas needed. This is useful for frameworks when paired withget_handler_class(), allowing the deployment to configure an arbitrary handler and its arguments.
- send(messages)¶
Send one or more email messages.
Messages should typically be
Message. However, they may also beemail.message.EmailMessagefor cases where a non-standard MIME construction is needed. It is up to the handler to decide whether that is supported based on what their service is capable of.- Parameters:
messages (list[Message | EmailMessage]) – A list of messages to send.
- Return type:
None
- async send_async(messages)¶
Send one or more email messages, as with
send(), but in anasynccontext.- Parameters:
messages (list[Message | EmailMessage])
- Return type:
None
- class email_simplified.handlers.EmailHandler¶
Interface for sending email messages. Subclasses will define how to send using a service, such as SMTP, an email provider’s API, etc.
- send(messages)¶
Send one or more email messages.
Messages should typically be
Message. However, they may also beemail.message.EmailMessagefor cases where a non-standard MIME construction is needed. It is up to the handler to decide whether that is supported based on what their service is capable of.- Parameters:
messages (list[Message | EmailMessage]) – A list of messages to send.
- Return type:
None
- async send_async(messages)¶
Send one or more email messages, as with
send(), but in anasynccontext.- Parameters:
messages (list[Message | EmailMessage])
- Return type:
None
- classmethod from_config(config)¶
Create an instance of this handler using arguments from
configas needed. This is useful for frameworks when paired withget_handler_class(), allowing the deployment to configure an arbitrary handler and its arguments.
Messages¶
- class email_simplified.Message(*, subject=None, text=None, from_addr=None, reply_to=None, to=None, cc=None, bcc=None, attachments=None, html=None, inline_attachments=None)¶
A representation of the typical data found in an email message. Can be converted to and from an
email.message.EmailMessage.While unlikely, MIME messages can be constructed pretty much arbitrarily. This class only represents a “common”/”standard” message structure with text, HTML, download attachments, and inline attachments.
Addresses passed to the various arguments/attributes can be strings in the form
user@domainorName <user@domain>, or instances ofemail.headerregistry.Address. If theuserpart has non-ASCII characters, SMTP servers must support theSMTPUTF8extension. The domain part will be encoded using IDNA.- Parameters:
subject (str | None) – The text in the subject line of the message.
text (str | None) – The plain text content.
from_addr (str | Address | None) – The address to show the message was sent from.
reply_to (str | Address | None) – Instruct users to reply to this address rather than
from_addr.to (list[str | Address] | None) – The primary recipients, visible and replied to by all other recipients.
cc (list[str | Address] | None) – The secondary recipients, visible and replied to by all other recipients.
bcc (list[str | Address] | None) – Hidden recipients. Not visible or replied to by any other recipients. When sending mass email, use this instead of
toandccto avoid “reply all email storms”.attachments (list[Attachment] | None) – Downloadable files separate from the content.
html (str | None) – The HTML text content.
textcontent should also be provided, but if it’s not then text is extracted from the HTML.inline_attachments (list[Attachment] | None) – Files that can be linked and displayed inside HTML content. Only relevant when
htmlcontent is provided.
- html: str | None¶
The HTML text content.
textcontent should also be provided, but if it’s not then text is extracted from the HTML.
- attachments: list[Attachment]¶
Downloadable files separate from the content.
- inline_attachments: list[Attachment]¶
Files that can be linked and displayed inside HTML content. Only relevant when
htmlcontent is provided.
- property to: AddressList¶
The primary recipients, visible and replied to by all other recipients.
- property cc: AddressList¶
The secondary recipients, visible and replied to by all other recipients.
- property bcc: AddressList¶
Hidden recipients. Not visible or replied to by any other recipients When sending mass email, use this instead of
toandccto avoid “reply all email storms”.
- to_mime()¶
Convert this
email_simplified.Messageto anemail.message.EmailMessage.- Return type:
- classmethod from_mime(message)¶
Convert an
email.message.EmailMessagemessage to aemail_simplified.Message.MIME messages can potentially have arbitrary structures. This only supports converting from a “standard” structure where a message has text, html, inline attachment, and download attachment parts. Can be one of the following:
One
text/plainpart.One
multipart/alternativepart containing onetext/plainpart then either:One
text/htmlpart.One
multipart/relatedpart containing onetext/htmlpart then one or more inline attachment parts.
One
multipart/mixedpart containing one of the above parts then one or more download attachment parts.
- Parameters:
message (EmailMessage) – The MIME part message to convert.
- Return type:
- class email_simplified.Attachment(data, *, filename=None, mimetype=None)¶
Structured representation of an email attachment.
- Parameters:
- data¶
Text or bytes data to attach.
- filename¶
Filename to show for the attachment.
- mimetype: str¶
Mimetype describing the attached data. Defaults to guessing from
filenameif possible, ortext/plainfor text data orapplication/octet-streamfor bytes data.
- property cid: str¶
The id used to refer to the inline attachment in HTML. Generated on access if not set.
- add_to_mime(message, *, inline=False)¶
Add this attachment to the given
email.message.EmailMessage. When attaching inline, include the :attr:`cid, otherwise thefilename.- Parameters:
message (EmailMessage) – The message to attach to.
inline (bool) – Attach inline for linking from HTML.
- Return type:
None