Packetwatch.net

Email notifications using Msmtp (TLS) in FreeBSD



Last modified: Dec. 9, 2012

Contents
1 - Summary
2 - Dependencies
3 - Msmtp installation
4 - Msmtp configuration
5 - Example shell script


1 - Summary

This guide will show you how to send emails for system events using msmtp with
TLS/SSL capabilities in FreeBSD. Msmtp is an SMTP client used to send emails.
This has been tested in FreeBSD 9.0 i386.


2 - Dependencies

Install following packages.
# sudo pkg_add -r ca_root_nss
Password:
# sudo pkg_add -r p5-LWPx-ParanoidAgent
Password:
# sudo pkg_add -r libidn
Password:
# sudo pkg_add -r wget
Password:
# rehash
# wget --no-check-certificate https://github.com/bagder/curl/raw/master/lib/mk-ca-bundle.pl
# perl mk-ca-bundle.pl
# sudo chown root:wheel ca-bundle.crt
Password:
# sudo chmod 555 ca-bundle.crt
Password:
# sudo mv ca-bundle.crt /usr/local/etc/
Password:
# rm -f certdata.txt mk-ca-bundle.pl
# sudo pkg_add -r portupgrade
Password:
# sudo portsnap fetch extract
Password:
There is an entry in /usr/ports/UPDATING regarding pkg-config.
# vi /usr/ports/UPDATING
# rehash
# sudo portupgrade -fo devel/pkgconf pkg-config-\*
Password:


3 - Msmtp installation

Instal the msmtp port.
# cd /usr/ports/mail/msmtp/
Select only the following options.
  [*] OPENSSL        Adds support for SSL (Secure Socket Layer)
  [*] IDN            Enable Internationalized Domain Names
# sudo make config
Password:
# sudo make
Password:
# sudo make install
Password:
# sudo make clean
Password:
# cd ~


4 - Msmtp configuration

Find where the msmtp binary was installed to.
# pkg_info -L msmtp-* | grep bin
/usr/local/bin/msmtp

Find where the configuration file should be put.
# rehash
# msmtp --version | grep 'System configuration'
System configuration file name: /usr/local/etc/msmtprc
# pkg_info -L msmtp-* | grep msmtprc

You can create your own configuration file. Here is an example that uses a
Gmail account which utilizes TLS.
  account default
  host smtp.googlemail.com
  port 587
  from testuser@gmail.com
  tls on
  tls_starttls on
  tls_trust_file /usr/local/etc/ca-bundle.crt
  auth on
  user testuser@gmail.com
  password **********
  syslog LOG_MAIL
# sudo vi /usr/local/etc/msmtprc
Password:
# sudo chmod 600 /usr/local/etc/msmtprc
Password:


5 - Shell script

Here is an example shell script that sends an email.

#!/bin/sh

hostname=/bin/hostname
host=`$hostname -s`
date=/bin/date
current_day=`$date +%m/%d/%y`
current_time=`$date +%H:%M:%S`
printf=/usr/bin/printf
from="testuser@gmail.com"
recipient="testuser@test.com"
#recipient="testuser1@test.com,testuser2@test.com"
msmtp=/usr/local/bin/msmtp
msmtp_config=/usr/local/etc/msmtprc

body="This is a test."
subject="Test sent from $host [$current_day $current_time]"
$printf "From: $from\nTo: $recipient\nSubject: $subject\n\n$body" | $msmtp --file=$msmtp_config -t
exit 0


Last modified: Thu Jan 1 00:00:00 1970 UTC
Packetwatch Research 2002-2017.