Notice
We Are Not Pair!!! Unofficial Guide!!!
Please note, msmtp
is an SMTPCLIENT. This guide covers installing and configuring on your own Linux machine, and NOTon your pair hosting server! In order to send mail using msmtp
you must configure it using the same mail server settings that you would use to configure a graphical client such as Outlook or Thunderbird.
Tags
Account Type: Any
Difficult Level: Medium
Pre-Requisites
You must have a basic knowledge of Linux / UNIX shell commands (just general life advice right there too). We’re assuming you know how to install packages, navigate the directory structure, edit files, ect.
You must have root
access on your own (non-pair) Linux or UNIX system to follow this guide. This guide is for setting up your own system to relay mail though your Pair hosting Email server. DO NOTattempt to install msmtp
on a Pair Networks server. Their servers are already configured with a working sendmail client if you need one on there.
Overview
Msmtp
is a lightweight – sendmail compatible SMTP client. Its free and open source software (GPL-3). In this example, we’re going to be going over installing and setting up msmtp
on a NON-pair Linux / UNIX system that you control.
This is ideal for a home lab / small organization usecase where you have servers on your LAN and you want them to be able to send Email (cronjobs, scripts, webforms, ect.) but you don’t want to go through the hassle of setting up a whole Postfix server. Msmtp uses plain SASL authentication (same as many other Email clients) when connecting to your Pair mail server to send. This means messages sent via msmtp should also be DKIM and SPF authenticated (assuming you have those features enabled for your domain).
If you already have a Pair account you can setup the msmtp
client on your local system and use it to relay mail out through your Pair account’s mail server.
We’re going to be going over installing on Ubuntu. However, msmtp
can be installed on pretty much any flavor of Linux or FreeBSD.
Installation
On the NON-pair system that you control you’ll want to install msmtp
using your system’s package manager.
On Ubuntu that’s apt
. We’re going to install two apt packages, msmtp
and msmtp-mta
.
sudo apt install msmtp msmtp-mta
Msmtp is “sendmail compatible” meaning essentially alias sendmail=/usr/bin/msmtp
. And that’s exactly what that second msmtp-mta
package does, it just creates a symbolic link between /usr/sbin/sendmail
and /usr/bin/msmtp
.
Setup and Configuration
The main config for msmtp on Linux is:
/etc/msmtprc
(Aside, its /usr/local/etc/msmtprc
on FreeBSD).
In this example, we’re only going to setup msmtp for sending as root. However, its worth noting you can also create user specific ~/.msmtprc
files.
First we’ll want to edit our system’s main /etc/msmtprc
. If you’re not familiar with vim
, use nano
.
sudo vim /etc/msmtprc
And add the following contents.
# Set default values for all following accounts.
defaults
auth on
tls on
tls_trust_file /usr/local/share/certs/ca-root-nss.crt
logfile /var/log/msmtp
# BLAH Account Entry
account BLAH
host YOUR_SMTP_SERVER
port 587
auto_from off
from YOUR_EMAIL_ADDRESS
user YOUR_EMAIL_ADDRESS
passwordeval "gpg --quiet --for-your-eyes-only --no-tty --decrypt /root/.msmtp-pair.gpg"
# Set a default account
account default : BLAH
Obviously replace the ALL_CAPS placeholder values with those for your mailbox. BLAH can be whatever you want to identify the account by.
If you don’t know your Pair SMTP server name you can use the official Pair help guide below to find it.
How to Find Your Email’s Incoming and Outgoing Servers
On Linux the main msmtp conf has to be world readable (sigh). So we’re going to replace the plain text password with one stored in a GPG encrypted file that can only be read by root.
We can setup a GPG key for our root user by running the command below.
sudo gpg --batch --yes --passphrase '' --quick-gen-key $USER default default
(Note, for msmtp
to work in an automated fashion for cronjobs and things like that we’ll want to leave the passphrase on the GPG key empty. Otherwise every time a cronjob tries to send an email it’ll just get held up waiting for the GPG key passphrase. Not what we want!).
Next we can run the command below to encrypt our password and store it in /root/.msmtp-pair.gpg
.
Be sure to replace YOUR_MAIL_PASSWORD
with your mailboxes password!
sudo bash -c "gpg --yes --encrypt --sign --armor -r root <<< 'YOUR_MAIL_PASSWORD' -o /root/.msmtp-pair.gpg"
Finally, we’ll want to set our permissions accordingly.
sudo chmod 600 /root/.msmtp-pair.gpg
sudo chmod 744 /etc/msmtprc
Congratulations, msmtp should now be setup and working! You should be ready to send Email!
Logging
Mail transfers are logged to the following file.
/var/log/msmtp
This can be useful to debug setup problems if you encounter client side configuration issues.
Sending an Email via MSMTP
Sending using the msmtp
CLI utility:
sudo printf "Subject: Test Email\nThis is a test. Please disregard." | msmtp TO_ADDRESS
Sending using the CLI mail
tool (routes through sendmail -> msmtp):
sudo echo "This is a test" | mail -s "Test Email" TO_ADDRESS
Sending using the aliased sendmail
tool (symlinked to -> msmtp):
From man sendmail
:
-t Extract recipients from message headers. These are added to any recipients specified on the command line.
email.txt
From: FROM_ADDRESS
To: TO_ADDRESS
Subject: Test Email
This is a test Email. Please disregard.
sudo /usr/sbin/sendmail -t < email.txt
Likewise, its worth noting the PHP mail()
function is also configured by default to use sendmail
to relay mail so this should work if your local machine needs to send mail via PHP based web form as well.