Skip to main content

Send emails written in Markdown

Project description

nMdmail: Send emails written in Markdown

Fork of https://github.com/yejianye/mdmail, which looks dead.

PyPI version Tests pre-commit.ci status codecov Code style: black Ruff

nMdmail sends emails written in Markdown. It could be used as a standalone command-line script or as a python module. The features includes

  • Have a sane default CSS style and support CSS customization
  • Support local images as inline images

Screenshot of an email sent via nmdmail viewed in Google Inbox

To install nmdmail

$ python -m pip install nmdmail

Send Email in Command-line

When sending emails from command-line, the body of the email could be read from a file or stdin.

Email headers such as subject, from/to, cc etc could be specified at the beginning of the markdown file, Or be specified in command-line arguments.

Here is an example of Markdown file with email headers

Subject: Sample Email
From: foo@xyz.com
To: bar@xyz.com
Cc: baz@xyz.com

# Sample Email

-

![Embed local image](../assets/image.jpg)

To send this email with nmdmail

$ nmdmail sample_email.md

Here is an example of specifying subject, from/to in command-line

$ nmdmail --from=foo@xyz.com --to=bar@xyz.com --subject='Sample' sample_email.md

To read email content from stdin,

$ echo '# Sample Email' | nmdmail --from=foo@xyz.com --to=bar@xyz.com --subject='Sample'

SMTP server configurations are read from the following environment variables

export MDMAIL_HOST="" # default: localhost
export MDMAIL_PORT="" # default: 25
export MDMAIL_USE_TLS="" # default: false
export MDMAIL_USE_SSL="" # default: false
export MDMAIL_USERNAME="" # default: None
export MDMAIL_PASSWORD="" # default: None
export MDMAIL_DEFAULT_SENDER="" # default: None

Full help of nmdmail command-line script

usage: nmdmail [-h] [--subject SUBJECT] [--from FROM_] [--to TO] [--cc CC]
               [--bcc BCC] [--reply-to REPLY_TO] [--css CSS] [--print-only]
               [file]

Send email written in Markdown.

positional arguments:
  file                  Markdown file for email content. Default to STDIN.

optional arguments:
  -h, --help            show this help message and exit
  --subject SUBJECT, -s SUBJECT
                        Subject line
  --from FROM, -f FROM
                        From address
  --to TO, -t TO        To addresses, separated by comma
  --cc CC, -c CC        CC address, separated by comma
  --bcc BCC, -b BCC     Bcc address, separated by comma
  --reply-to REPLY_TO, -r REPLY_TO
                        Reply-to address
  --css CSS             Use a custom CSS file
  --print-only, -p      Only print out rendered html

Send Email in Python Code

Sending emails in python is straight-forward.

import nmdmail

email="""
# Sample Email

- Python is awesome
- Markdown is cool

![Embed local image](../assets/image.jpg)
"""

nmdmail.send(email, subject='Sample Email',
            from_email='foo@example.com', to_email='bar@example.com')

By default, it will use SMTP server on localhost. You could specify a SMTP server as well.

# Specify SMTP server
smtp = {
  'host: 'my-mailserver.com',
  'port': 25,
  'tls': False,
  'ssl': False,
  'user: '',
  'password': '',
}

nmdmail.send(content,
             subject='Sample Email',
             from_email='foo@example.com',
             to_email='bar@example.com',
             smtp=smtp)

API documentation nmdmail.send

  • email (str/obj): A markdown string or EmailContent object
  • subject (str): subject line
  • from_email (str): sender email address
  • to_email (str/list): recipient email addresses
  • cc (str/list): CC email addresses (string or a list)
  • bcc (str/list): BCC email addresses (string or a list)
  • reply_to (str): Reply-to email address
  • smtp (dict): SMTP configuration with following keys
    • host (str): SMTP server host. Default: localhost
    • port (int): SMTP server port. Default: 25
    • tls (bool): Use TLS. Default: False
    • ssl (bool): Use SSL. Default: False
    • user (bool): SMTP login user. Default empty
    • password (bool): SMTP login password. Default empty

Use nmdmail with Vim and Emacs

Since nmdmail can read from stdin and support email headers such as subject/from/to in the markdown file itself, integrating nmdmail with Vim, Emacs or other text editors is easy.

To use nmdmail in Vim, just write a markdown email with headers, and then execute w !nmdmail command, which will send current buffer as stdin to nmdmail.

In Emacs, you could write a small function to do the same thing

(defun nmdmail-send-buffer ()
  (interactive)
  (shell-command-on-region (point-min) (point-max) "nmdmail"))

Then M-x nmdmail-send-buffer will send current buffer to nmdmail.

Project details


Download files

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

Source Distribution

nmdmail-0.6.0.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nmdmail-0.6.0-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file nmdmail-0.6.0.tar.gz.

File metadata

  • Download URL: nmdmail-0.6.0.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for nmdmail-0.6.0.tar.gz
Algorithm Hash digest
SHA256 945353358c308664f0e72eca0146901ceb47fc16262c37dd4ed219aec5381319
MD5 0fab0ed45d4655fcaecff98b4a11b0b4
BLAKE2b-256 ce3b289fd4d3213f86fb97871723d9d6f60115cb3c40256e4c5cec9d7005fa3a

See more details on using hashes here.

Provenance

The following attestation bundles were made for nmdmail-0.6.0.tar.gz:

Publisher: release.yml on nim65s/nmdmail

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file nmdmail-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: nmdmail-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for nmdmail-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 52bb0fa4399c3157959d9faef17ed51b54c197f935c2c726fde560434147a448
MD5 437b83d415a299a9a23d76e4bf1c2823
BLAKE2b-256 b7b80221a2c05b846ac0fbff1d89a7223754cb3197116ce0c4a7752dd8d97a40

See more details on using hashes here.

Provenance

The following attestation bundles were made for nmdmail-0.6.0-py3-none-any.whl:

Publisher: release.yml on nim65s/nmdmail

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page