Skip to main content

mailcoil

Mailcoil is a small SMTP wrapper around Python's native functionality that makes several tasks easier, namely constructing mails that contain referenced inline attachments (e.g., images that are referenced from within HTML mail) or signing/encrypting emails via CMS (commonly known as S/MIME). It also has support for IMAP so that sent mails can be stored in a "Sent" folder using the same mechanism as sending them through SMTP.

Usage

For usage of mailcoil you first need to define a dropoff at which mails should be posted. This is typically your MTA or smart relay:

dropoff = mailcoil.MailDropoff(mailcoil.MailDropoff.Scheme.SMTPS, "smtp.my-server.com")

You then can create an email:

mail = mailcoil.Email(from_address = "Johannes Bauer <johannes.bauer@gmx.de>", subject = "What's up?").to("someone@gmx.de")

CC and BCC are also supported using the same syntax as the .to() method:

mail.cc("cc1@gmx.de", "cc2@gmx.de").bcc("bcc@gmx.de")

Emails can use text/plain, text/html, or both bodies:

mail.text = "This is the text content!"
mail.html = "<html><body><h1>Hey!</h1><p>This is the HTML content!</body></html>"

To attach files to this mail, simply you can use the attach method:

mail.attach("foo.jpg")

By default, the Content-Disposition of attachments is "attachment". However, you can also create "inline" attachments that you can then reuse in your HTML portion, e.g.:

src_cid = mail.attach("foo.jpg", inline = True)
mail.html = f"<html><body><h1>Hey!</h1><p>This is the HTML content!<img src='${src_cid}'></body></html>"

By default, the Content-ID is auto-generated, which means that it depends on the number of attachments. If you want fixed CIDs, you can also do that. Note that You are then responsible for not creating collisions in the CIDs:

src_cid = mail.attach("foo.jpg", inline = True, cid = "foobar")
mail.html = f"<html><body><h1>Hey!</h1><p>This is the HTML content!<img src='cid:foobar'></body></html>"

If you wish to sign or encrypt mail using CMS (commonly known as S/MIME), you need to set the security property of your email and configure it properly. For example, for just signing, this works:

mail.security = mailcoil.CMS().sign(signer_keyfile = "my_key.pem", signer_certfile = "my_cert.pem", ca_certfile = "my_ca.pem")

If you additionally want to encrypt for three different targets, you can also do:

mail.security.encrypt("target1.pem", "target2.pem", "target3.pem")

Similarly, you may also only encrypt without signing:

mail.security = mailcoil.CMS().encrypt("target.pem")

When your mail is finally set up, you can drop it off at the previously defined dropoff:

dropoff.post(mail)

A complete example is therefore:

import mailcoil
dropoff = mailcoil.MailDropoff(mailcoil.MailDropoff.Scheme.SMTPS, "smtp.my-server.com")
mail = mailcoil.Email(from_address = "Johannes Bauer <johannes.bauer@gmx.de>", subject = "What's up?").to("someone@gmx.de")
mail.cc("cc1@gmx.de", "cc2@gmx.de").bcc("bcc@gmx.de")
mail.text = "This is the text content!"

# Attach file and use it in HTML portion
src_cid = mail.attach("foo.jpg", inline = True)
mail.html = f"<html><body><h1>Hey!</h1><p>This is the HTML content!<img src='${src_cid}'></body></html>"

# Sign and encrypt
mail.security = mailcoil.CMS().sign(signer_keyfile = "my_key.pem", signer_certfile = "my_cert.pem", ca_certfile = "my_ca.pem").encrypt("target1.pem", "target2.pem", "target3.pem")

dropoff.post(mail)

License

GNU GPL-3.

Metadata

Release files for mailcoil 0.0.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mailcoil 0.0.9
File Size Uploaded
mailcoil-0.0.9.tar.gz 24.5 kB Details

Release files / mailcoil-0.0.9.tar.gz

Download URL mailcoil-0.0.9.tar.gz
Size 24.5 kB
Tags Source
SHA-256 checksum
How to use checksums
c8a341bb62574ed1a3fbedde42b9fa5e20403d01858942e0faa90959e2273b66
BLAKE2b-256 checksum
How to use checksums
65319bcd4c7f9cb9b4fc45fe1320c088ef8dad848ab18f11b69a377cb533fc6e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.3

Release history Release notifications | RSS feed

This release

0.0.9 This release

1 release file

0.0.8

1 release file

0.0.7

1 release file

0.0.6

1 release file

0.0.5

1 release file

0.0.4

1 release file

0.0.2

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page