Sysadmin

Send server reports by email with msmtp

What is msmtp

msmtp is an SMTP client that allows you to transmit an email to an SMTP server which takes care of further delivery. If you do not host your own mail server (and instead you use a third-party provider such as G Suite to host your mail), then you do not need a full-fledged mail server (MTA) such as Postfix, Exim, etc. Instead, you can just use this lightweight SMTP client to transmit your mail to Google's SMTP server.

Install msmtp

Install dependencies:

# yum groupinstall "Development Tools"
# yum install texinfo
# yum install gnutls gnutls-devel
# yum install ca-certificates

Install msmtp:

# git clone https://git.marlam.de/git/msmtp.git
# cd msmtp
# autoreconf -i
# ./configure
# make
# make install

Configure msmtp

# vim ~/.msmtprc
# Set default values for all following accounts.
defaults
auth           on
tls            on
tls_starttls   on
tls_trust_file /etc/ssl/certs/ca-bundle.crt
logfile        ~/.msmtp.log

# Gmail
account        gmail
host           smtp-relay.gmail.com # host for G Suite users, change it if you use Gmail without G suite
port           587
from           bounce@example.com
user           bounce@example.com
password       thepassword

# Gmail
# account        anotherprovider
# host           smtp.anotherprovider.net

# Set a default account
account default : gmail

Send email via the command line

# echo "hello world" | /usr/local/bin/msmtp report@example.com
# printf "To: sysadmin@example.com\nFrom: servicename-servername@example.com\nSubject: Email Test Using MSMTP\n\nHello there. This is email test from MSMTP." | /usr/local/bin/msmtp sysadmin@example.com

Note: Google has blocked plain smtp authentication, and requires OAuth. Absurdly it worked for a day then all emails were blocked. Instead of trying to make OAuth work, G Suite allowed me to add a list of server IPs allowed to send email and would then let me disable smtp authentication altogether (switched auth to off). The alternative is to allow "less secured apps" in Google's settings.

Comments

Comments including links will not be approved.