Quickstart

Installation

Install from PyPI with pip:

pip install mail-cleaner

Usage

Sending e-mail shortcuts

Utility layer on top of django.core.mail.

mail_cleaner.mail.send_mail_plus(subject: str, message: str, from_email: str | None, recipient_list: Sequence[str] | None, cc: Sequence[str] | None, fail_silently: bool = False, auth_user: str | None = None, auth_password: str | None = None, connection=None, html_message: str | None = None, attachments: Iterable[_AttachmentTuple] | None = None, headers: Dict[str, str] | None = None) int

Send outgoing email.

modified copy of django.core.mail.send_mail() with:

  • attachment support

  • extract datauri images from html and attach as inline-attachments

Producing plain text from rich content

Utilities to convert HTML email into plain-text bodies.

Taken from Open Forms (EUPL 1.2 licensed), which used the code from other Maykin Media projects.

mail_cleaner.text.strip_tags_plus(text: str, keep_leading_whitespace: bool = False) str

Strip HTML tags from input text.

This utility wraps around django’s django.utils.html.strip_tags() and cleans up the output to make it suitable for plain text display.

Warning

This renders unescaped user-data and should never be used for display as HTML content (XSS risk).

This is originally copied and modified from Maykin Media’s “Werkbezoek” project.

Sanitizing content

mail_cleaner.sanitizer.sanitize_content(content: str, allowlist: List[str] | None = None) str

Sanitize the content by stripping untrusted content.

This function is meant to protect against untrusted user input in e-mail bodies. It performs the following sanitizations:

  • strip URLs that are not present in the explicit allow list