core.mail

Attributes

specials_regex

alphanumeric

qp_prefix

qp_suffix

QP_PREFIX_LENGTH

QP_SUFFIX_LENGTH

QP_MAX_WORD_LENGTH

QP_CONTENT_LENGTH

Classes

Attachment

Represents a mail attachment that can be passed to prepare_email

Functions

needs_qp_encode(→ bool)

qp_encode_display_name(→ str)

Applies Quoted Printable encoding to the display name according

coerce_address(→ email.headerregistry.Address)

Coerces a string type into a email.headerregistry.Address object

format_single_address(→ str)

Formats a single Address according to Postmark API rules that is

format_address(→ str)

Convenience function that accepts both a single Address and a

prepare_email(, cc, bcc, subject, content, plaintext, ...)

Creates a dictiornary that can be turned into JSON as is and sent

Module Contents

core.mail.specials_regex[source]
core.mail.alphanumeric[source]
core.mail.qp_prefix = '=?utf-8?q?'[source]
core.mail.qp_suffix = '?='[source]
core.mail.QP_PREFIX_LENGTH[source]
core.mail.QP_SUFFIX_LENGTH[source]
core.mail.QP_MAX_WORD_LENGTH = 75[source]
core.mail.QP_CONTENT_LENGTH[source]
core.mail.needs_qp_encode(display_name: str) bool[source]
core.mail.qp_encode_display_name(display_name: str) str[source]

Applies Quoted Printable encoding to the display name according to Postmark API’s rules that can be parsed losslessly back into the original display_name with the EmailMessage API.

core.mail.coerce_address(address: email.headerregistry.Address | str) email.headerregistry.Address[source]

Coerces a string type into a email.headerregistry.Address object by parsing the string as a sender header.

It acts like parseaddr for string values, but undoes QP-encoding for the display_name which parseaddr does not.

NOTE: This function should probably go away, once we switch to

using Address objects everywhere. Or we make it more strict by asserting that string values need to be an email address without display_name, so we can use Address(addr_spec=address) to coerce it, which should be faster than header_factory.

core.mail.format_single_address(address: email.headerregistry.Address | str) str[source]

Formats a single Address according to Postmark API rules that is cross-compatible with email.message.EmailMessage for raw SMTP sends.

The rules state that backslash escaping quotes is illegal and quoted printable encoded display names need to be split into space-separated encoded words of maximum length 75, with the entire display name enclosed in double quotes if it contains more than one word.

Parameters:

address – email.headerregistry.Address or preformatted string

core.mail.format_address(addresses: SequenceOrScalar[Address | str]) str[source]

Convenience function that accepts both a single Address and a sequence of Address, otherwise identical to format_single_address

It enforces a limit of 50 addresses, due to Postmark API restrictions

Parameters:

addresses – Single Address/str or sequence thereof

class core.mail.Attachment(filename: _typeshed.StrPath, content: SupportsRead[str | bytes] | str | bytes | None = None, content_type: str | None = None)[source]

Represents a mail attachment that can be passed to prepare_email

__slots__ = ('filename', 'content', 'content_type')[source]
filename: str[source]
content: bytes[source]
content_type: str[source]
filename[source]
_content: str | bytes[source]
content[source]
content_type[source]
prepare() core.types.AttachmentJsonDict[source]

Prepares attachment so it can be sent to Postmark API.

core.mail.prepare_email(sender: email.headerregistry.Address | str, reply_to: email.headerregistry.Address | str | None = None, receivers: SequenceOrScalar[Address | str] = (), cc: SequenceOrScalar[Address | str] = (), bcc: SequenceOrScalar[Address | str] = (), subject: str | None = None, content: str | None = None, plaintext: str | None = None, attachments: Iterable[Attachment | StrPath] = (), headers: dict[str, str] | None = None, stream: str = 'marketing') core.types.EmailJsonDict[source]

Creates a dictiornary that can be turned into JSON as is and sent to the Postmark API.

Parameters:
  • content – HTML content.

  • attachments – Either a list of onegov.core.email.Attachment or a list of filenames/os.PathLike to attach to the email.

  • headers – Dictionary containing additional headers to be set