Setting up msmtp with FastMail

I recently decided to set up a basic mail sender on my Debian install, and chose msmtp. I use FastMail as my mail host.

The first step, of course, is to install msmtp (and ca-certificates, if you don’t already have them):

$ sudo apt-get update
$ sudo apt-get install msmtp ca-certificates

Once the install is done, msmtp --version will show me that the install is working:

$ msmtp --version
msmtp version 1.4.32
Platform: i486-pc-linux-gnu
TLS/SSL library: GnuTLS
Authentication library: GNU SASL
Supported authentication methods:
plain scram-sha-1 cram-md5 external digest-md5 login ntlm 
IDN support: enabled
NLS: enabled, LOCALEDIR is /usr/share/locale
Keyring support: none
System configuration file name: /etc/msmtprc
User configuration file name: /home/jared/.msmtprc

Copyright (C) 2014 Martin Lambers and others.
This is free software.  You may redistribute copies of it under the terms of
the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
There is NO WARRANTY, to the extent permitted by law.

I decided to set up a global (system) configuration, and from the above version information, I can see that the config file for that should be located at /etc/msmtprc

This file does not exist by default, so I will create it:

$ sudo vim /etc/msmtprc

Using the documentation from msmtp and FastMail’s SMTP settings, I created the configuration file below. A few notes:

  • FastMail’s SMTP is on port 465
  • FastMail uses TLS, and STARTTLS needs to be disabled.
  • You will want to consider creating an alternative login for your FastMail account - I created one just for msmtp, which allows me to disable just that login in FastMail if something happens to my server.
  • Consider carefully before using my password advice! msmtp supports a couple different authentication methods, and mine is not really the best.
# ---------------------------------------
# msmtp System Wide Configuration file
# ---------------------------------------
# A system wide configuration is optional.
# If it exists, it usually defines a default account.
# This allows msmtp to be used like /usr/sbin/sendmail.
# ---------------------------------------
# Account
# ---------------------------------------
# FastMail
account fastmail
host mail.messagingengine.com
from noreply@yourdomain.com

auth on
port 465
user yourusername@fastmail.com
password **your alternate login password**

tls on
tls_trust_file /usr/share/ca-certificates/mozilla/DigiCert_High_Assurance_EV_Root_CA.crt
tls_starttls off

# ---------------------------------------
# Configuration
# ---------------------------------------

# Syslog logging with facility LOG_MAIL instead of the default LOG_USER.
syslog LOG_MAIL

# Set a default account
account default : fastmail
# ---------------------------------------

Once the file is created, we can use msmtp’s --pretend option to display the configuration settings:

$ msmtp --pretend

If everything is set up correctly, your configuration settings should be displayed (your password will be displayed as *)

The next step is to test the configuration, and the easiest way to do that is to send an email to yourself:

$ echo "This is a test email" | msmtp --debug your@emailaddress.com

If everything is set up correctly, you’ll see msmtp print your configuration settings, and then you’ll see the conversation msmtp has with FastMail as it delivers the message. Moments later, you should have a new email waiting.

If you want to include a subject, or have a more complex email, you have to send the mail headers as well. You can do this easily by creating a text file and then piping that over to msmtp.

The email file:

From: your@fromaddress.com
To: your@toaddress.com
Subject: This is the email subject
Mime-Version: 1.0
Content-Type: text/html

<h1>HTML email</h1>
This is how you can <strong>easily</strong> send <span style="color:red; font-weight:bold;">HTML</span> mail from the command line.

To send this email:

cat mymessage.mail | msmtp your@toaddress.com

layouts/post/single.html