Skip to main content

testmsg

Generate RFC822 compliant e-mail messages for tests and send it over SMTP.

While it's easy to send test messages like echo asdf | mail you@gmail.com or via telnet mx.example.com 25 I need a tool which:

  • Generates valid messages
  • Messages should not look spammy or very suspicious
  • Easy to use and repeat test
  • Ability to customize messages
  • Work well with msmtp or other full-featured SMTP client (e.g. which can send over secure SMTP connection with authentication)
  • Support DKIM signatures
  • Show SMTP conversation

Installation

For current user, using pipx

pipx install testmsg

or globally:

PIPX_HOME=/opt/pipx PIPX_BIN_DIR=/usr/local/bin pipx install testmsg

or (old way, better if inside virtualenv):

pip3 install testmsg

Usage example

Just generate minimal valid message, print to stdout (not sending).

$ testmsg --to you@gmail.com  --lorem 
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: quoted-printable
MIME-Version: 1.0
Subject: Sent with github.com/yaroslaff/testmsg
From: from@example.com
To: you@gmail.com

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempo=
r incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut al=
iquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore =
eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia des=
erunt mollit anim id est laborum.

Options

You can pass options either as command-line arguments or in .env file. Example:

FROM=noreply@example.com
# Optional return-path, could be different
# RETURN=noreply@example.com

TO=me@example.net
SUBJECT="My subj"

# Only one of next three is possible at same time
# TEXT="Hello, world!"
# MSG=/tmp/helloworld.txt
LOREM=1

# add timestamp to message body
TIMESTAMP=1

# if we want to send message, set mailserver host there. Otherwise testmsg will just print to stdout
SEND=localhost

# You may override port (e.g. 587)
# PORT=25

# SMTP authentication
# If SMTPPASS given, will use SMTP authentication. Default SMTPUSER is same as FROM
# SMTPUSER=aaa@bbb.com
SMTPPASS="MySecretPassword"

# use 1 for True, anything else for False
SSL=0
STARTTLS=1

# DKIM (very very optional)
# DKIM_SELECTOR="mail"
# DKIM_PRIVKEY=/etc/ssl/private/test.example.com.pem

VERBOSE=1

To enable boolean option use value "1", to disable - any other value.

With such .env file, you can send message with just testmsg command (no options).

Sending message

To actually send message via SMTP server add --send localhost and optionally add -v for verbosity.

testmsg -v --lorem --from you@example.net --to you@gmail.com --send localhost

See below about how to use authentication and SSL/TLS and how to use with msmtp (or other smtp clients).

Customize message

Use --from, --to and --subject to override basic properties of message, use --add HEADER VALUE to add custom header(s).

To set Return-Path header (address used in MAIL FROM SMTP command) to custom value, use -r / --return, e.g. --return noreply@gmail.com.

Default message text is empty, use --text "blah blah blah" or --lorem or --msg PATH or --msg - .(to read from stdin). Add --time to add current time as an prefix to text.

Add attachments

Use --attachment (or --att) to add attachments: --att FILE1 FILE2 ...

Sign with DKIM

Generate DKIM RSA keys:

# generate private RSA key for DKIM
openssl genrsa -out example.com.pem 1024
# generate public key for DKIM
openssl rsa -in example.com.pem -out example.com.pub -pubout

Make DKIM DNS record with as SELECTOR._domainkey.example.com and verify it (here I decide to use selector mail):

$ host -t txt mail._domainkey.example.com
mail._domainkey.example.com.net descriptive text "v=DKIM1; h=sha256; k=rsa; p=MII...."

send DKIM signed message to gmail or mail-tester.com! Use --selector and --privkey arguments.

testmsg -f test@example.com -t mailbox@gmail.com --lorem --selector mail --privkey example.com.pem -v --send localhost

Use with authentication and SSL/STARTTLS SMTP servers

To use authentication, use --user and --password (or --pass) parameters. Use --ssl to use SSL-capable SMTP server (port 465), or use --starttls to use STARTTLS SMTP command. If neither --ssl nor --starttls is given, message and authentication credentials are sent over plain unencrypted connection, which is highly insecure.

If --user is not given (but --password given), testmsg will use username same as from (--from / -f) address.

Example:

testmsg -v -f test1@example.com -t somebody@example.net --lorem --pass "MyTestPass" --send mx.example.com --starttls

Use together with msmtp

Here we send with TLS and authentication (using msmtp). Username for authentication (--user) is same as FROM address. Testmsg generates valid message and msmtp sends it.

FROM=sender@example.com
TO=recipient@gmail.com

testmsg -f $FROM -t $TO --lorem | msmtp -v --host smtp.office365.com --port 587 --user $FROM --passwordeval='echo MyTestPass' -f $FROM --tls=on --auth=on $TO

Metadata

Release files for testmsg 0.0.24

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

Source distribution (sdist)

Source distribution for testmsg 0.0.24
File Size Uploaded Provenance
testmsg-0.0.24.tar.gz 9.3 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for testmsg 0.0.24
File Interpreter ABI Platform
testmsg-0.0.24-py3-none-any.whl Python 3 none any Details

Total release size: 17.7 kB

Release files / testmsg-0.0.24.tar.gz

Download URL testmsg-0.0.24.tar.gz
Size 9.3 kB
Tags Source
SHA-256 checksum
How to use checksums
046fcb60c9c67fef9be878707b7733dac84ec26184e775db7b1440c2a4945fac
BLAKE2b-256 checksum
How to use checksums
69230439fdd068c2332545cd58283965ebc30409532f6341d2b85ba5f994b99a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via Hatch/1.16.3 cpython/3.13.5 HTTPX/0.28.1

Release files / testmsg-0.0.24-py3-none-any.whl

Download URL testmsg-0.0.24-py3-none-any.whl
Size 8.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7861cc3a77c75933481853e1b512f589253ac022dc7d8a40d395707431e96123
BLAKE2b-256 checksum
How to use checksums
653466f4ba2fe949122538bb70ba804b73a50507f7d4adabb06ab92a5cce51fd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via Hatch/1.16.3 cpython/3.13.5 HTTPX/0.28.1

Security for testmsg 0.0.24

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

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