Simple Mail Notifications using SMTP protocol on Linux systems

Ravinayag
3 min readJan 20, 2022

In many cases, we used to configure the postfix/Sendmail mail systems for simply sending email notifications from the command line. but it was a very hard task to make it happen and also needs maintenance for several security reasons.
A long time ago i used to do with the ssmtp package but this library is not maintained, recently come to know with mstmp package and it was a replacement for ssmtp when i read some blogs and has some good features too. I wrote this article partly as a reference for myself.

A quick brief about the ssmtp/msmtp :

[s,m]SMTP is a small simple utility that let's relay your Linux system emails to mail servers like Google , Microsoft Office 365 accounts or other mail providers. It's a pretty simple and straightforward utility that forwards your Linux system’s emails to reliable relay mail servers like Google or other providers and then email is forwarded to the destination addresses using those SMTP servers addresses.(smtp.gmail.com or mail.yourdomain.com).
Please keep in mind: This is not a full-fledged mail server, it's just a utility that forwards emails to an external email address for automation purposes or some other.

Note: For the people using Gsuite accounts.
If you using Gmail an email account, use pgp to store your password securely. If your account gets locked due to the different locations between you and the server.
If you find difficulties logging in from your server to Gmail, you might want to follow the steps provided in Google Support Site.

Let's begin to install

Before we install MSMTP, we’ll update our server.

$ sudo apt-get update

Installing MSMTP

$ sudo apt-get -y install msmtp

Installing CA- certificates

We need Certificate Authority (CA) certificates so that we can connect using SSL / TLS to the email server.
Check whether your server already has a ca-certificates package installed or not using the command below :

$ dpkg -l | grep ca-certificates
ii ca-certificates 20210119~20.04.2 all Common CA certificates

you will get similar to above if already installed. If not, then you need to install this package by running the below command :

$ sudo apt-get -y install ca-certificates

Configuring MSMTP

msmtp supports two locations: ~/.msmtprc and /etc/msmtprc, at your preference. one is for the local users and the other is for the whole system. In this tutorial, we'll use /etc/msmtprc.
Create an MSMTP configuration on /etc/msmtprc with the content below. You will have to enter your Gmail username and password on this file.

# Set default values for all following accounts.
defaults
tls on
auth on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
logfile /var/log/mail/msmtp.log
# Gmail
account gmail
host smtp.gmail.com
port 587
from youremail@gmail.com
user youremail@gmail.com
password xxxxxx
# yandex
account otherprovider
host secure.emailsrvr.com
port 587
from devops@otherprovider.com
user devops@otherprovider.com
password xxxxx
# Set a default account
account default : gmail

You have configured two email accounts as above, we can use either of the account when sending email by calling profile names. we do not need to call the default account since it's mentioned in the last line. for more customization options refer to this link and this link

that's it, we can start to use mstmp to send mails in the below format.

$ echo -e “Subject: MySubject\r\n\r\nThis is Text of mybody message” | msmtp -v admin@example.com

However many applications use the mailx or mail command to send emails by default mail.utils program since it will be installed in ubuntu by default. so msmtp format is not compatible with default mail.utils package. so we have to use bsd_mailx package for the mail formats. Use the below link to download and install using apt commands.

$ sudo apt install http://archive.ubuntu.com/ubuntu/pool/main/libb/libbsd/libbsd0_0.10.0-1_amd64.deb
$ sudo apt install http://archive.ubuntu.com/ubuntu/pool/main/b/bsd-mailx/bsd-mailx_8.1.2-0.20180807cvs-1_amd64.deb
$ sudo apt install libsecret-tools

Note : don’t install the default from ubuntu repository of bsd_mailx package because its always use the stable version which is 8.1.2- 2016 version, but we have to use 8.1.2–2018 version, you can download/install from the given link https://ubuntu.pkgs.org/20.04/ubuntu-main-amd64/

Once you install it will automatically create the link to bsd_mailx. then and there you can use the mail/mailx to send emails.

/bin/echo TEXT | /usr/bin/mail -s SUBJECT you@mailmee.com

Tips : If you need to encrypt the password field from clear plain text in the config file, Consider using the ‘passwordeval’ command or a key ring instead of this command

passwordeval gpg -d ~/.msmtp.password.gpg

Hope it helps for the ref….

Thank you

--

--

Ravinayag

Blockchain enthusiast & Research | DevOps Explorer | Hyperledger Explorer