Testing SMTP Server Authentication

I recently set up a new mail server, and I wanted to make sure that SMTP authentication was working as I expected. The msmtp command (from http://msmtp.sourceforge.net/) turned out to be just what I needed.

msmtp is an SMTP client that can use by as an “SMTP plugin” for mutt or other Mail User Agents (MUAs), and it supports authenticated SMTP with various methods (PLAIN, LOGIN and CRAM-MD5) and TLS encrypted connections, and can be configured on the command line or with configuration files.

I stuck a test message (with headers) in a tmp file, and checked my new SMTP server with commands like these:

% msmtp \
  -f test@example.com -t \
  --host=smtp.example.com \
  --auth=on --user=emailuser@example.com \
  recipient@example.com < /tmp/testmsg
msmtp: cannot use a secure authentication method
msmtp: could not send mail

That one failed because it couldn’t do an encrypted connection.

% msmtp \
  -f test@example.com -t \
  --host=smtp.example.com \
  --auth=plain --user=emailuser@example.com \
  recipient@example.com < /tmp/testmsg
password for emailuser@example.com at smtp.example.com:

and when compiled with openssl (on FreeBSD with the ports collection: make WITH_OPENSSL=yes)

% msmtp --tls-certcheck=off --tls=on \
  -f test@example.com -t \
  --host=smtp.example.com \
  --auth=on --user=emailuser@example.com \
  recipient@example.com < /tmp/testmsg
password for emailuser@example.com at smtp.example.com:

I could even build this into a nagios plugin, and monitor that it keeps on working.

I don’t think this kind of testing was necessarily envisioned by the msmtp authors, but I sure found it a useful ability, and it demonstrates once again the power of the command line and the UNIX tool philosophy.

Updated: February 19, 2018 — 8:27 pm

Leave a Reply

Your email address will not be published. Required fields are marked *