Skip to main content

html2eml is a simple package for converting HTML text to EML format (MIME RFC 2822)

Project description

html2eml

Build Status MIT License badge

html2eml is a simple package for converting HTML text to EML format (MIME RFC 2822).

Getting started

Create simple message with to and from headers

>>> import html2eml

>>> msg = html2eml.from_html('<html><body><p>Hello world</p></body></html>', to='spam@example.com', from_='eggs@example.com', subject='Sausage')

>>> print(msg.as_string())
MIME-Version: 1.0
Content-Type: text/html; charset="utf-8"
Content-Transfer-Encoding: base64
To: spam@example.com
From: eggs@example.com
Subject: =?utf-8?q?Sausage?=

PGh0bWw+PGJvZHk+PHA+SGVsbG8gd29ybGQ8L3A+PC9ib2R5PjwvaHRtbD4=

html2eml allows changing charset of html message

>>> msg = html2eml.from_html('<html><body><p>Hello world</p></body></html>', charset='ISO-8859-1', to='spam@example.com')

>>> print(msg.as_string())
MIME-Version: 1.0
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
To: spam@example.com

<html><body><p>Hello world</p></body></html>

Outside of To and From fields we can also specify CC and BCC

>>> msg = html2eml.from_html('<html><body><p>Hello world</p></body></html>', to='foo@example.com', from_='bar@example.com', cc='spam@example.com', bcc='eggs@example.com')

>>> print(msg.as_string())
MIME-Version: 1.0
Content-Type: text/html; charset="utf-8"
Content-Transfer-Encoding: base64
To: foo@example.com
From: bar@example.com
CC: spam@example.com
BCC: eggs@example.com

PGh0bWw+PGJvZHk+PHA+SGVsbG8gd29ybGQ8L3A+PC9ib2R5PjwvaHRtbD4=

In case of multiple recipients we can pass a list in any of those fields

>>> msg = html2eml.from_html('<html><body><p>Hello world</p></body></html>', to=['foo@example.com', 'spam@example.com', 'eggs@example.com'], from_='bar@example.com')

>>> print(msg.as_string())
MIME-Version: 1.0
Content-Type: text/html; charset="utf-8"
Content-Transfer-Encoding: base64
To: foo@example.com, spam@example.com, eggs@example.com
From: bar@example.com

PGh0bWw+PGJvZHk+PHA+SGVsbG8gd29ybGQ8L3A+PC9ib2R5PjwvaHRtbD4

Library also supports converting plain EML messages to MIME Multipart messages

>>> msg = html2eml.from_html('<html><body><p>Hello world</p></body></html>', to=['foo@example.com', 'spam@example.com', 'eggs@example.com'], from_='bar@example.com')

>>> multipart = html2eml.convert_eml_to_multipart(msg)

>>> print(multipart.as_string())
MIME-Version: 1.0
Content-Type: multipart/mixed; charset="utf-8"; boundary="===============2813024326530809151=="
Content-Transfer-Encoding: base64
To: foo@example.com, spam@example.com, eggs@example.com
From: bar@example.com

--===============2813024326530809151==
Content-Type: text/html; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: base64

PGh0bWw+PGJvZHk+PHA+SGVsbG8gd29ybGQ8L3A+PC9ib2R5PjwvaHRtbD4=

--===============2813024326530809151==--

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for html2eml, version 0.0.5
Filename, size File type Python version Upload date Hashes
Filename, size html2eml-0.0.5-py3-none-any.whl (3.8 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size html2eml-0.0.5.tar.gz (2.7 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page