Packetwatch.net

Email notifications using Msmtp (TLS) in OpenBSD



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 OpenBSD. Msmtp is an SMTP client used to send email.
This has been tested in OpenBSD 5.2 i386.


2 - Dependencies

I've created a shell script that can search the list of available packages.

#!/bin/sh
# This script will look for packages with the search term.

echo=/bin/echo
lynx=/usr/bin/lynx
pkg_path=ftp://ftp.openbsd.org/pub/OpenBSD/$rel/packages/$arch/index.txt
tr=/usr/bin/tr
cut=/usr/bin/cut
grep=/usr/bin/grep

if [ $# -ne 1 ]
    then
        exit 0
    else
        # check if a valid parameter
        if [ "$1" == "-help" ]
            then
                $echo Usage: pkg_find [string]
            else
                $lynx -dump $pkg_path | $tr -s " " | $cut -d " " -f 10 | $grep -i $1
        fi
fi
exit 0

# cd ~
# vi pkg_find
# sudo chown root:bin pkg_find
Password:
# sudo chmod 555 pkg_find
Password:
# sudo mv pkg_find /usr/sbin/
Password:

Here is an example of what it can do.
# pkg_find -help
Usage: pkg_find [string]
# pkg_find msmtp 
clamsmtp-*.tgz
msmtp-*.tgz

Install the following packages.
# pkg_find p5-Crypt-SSLeay
p5-Crypt-SSLeay-*.tgz
# sudo pkg_add p5-Crypt-SSLeay-*.tgz
Password:
# pkg_find p5-LWP-UserAgent-Determined
p5-LWP-UserAgent-Determined-*.tgz
# sudo pkg_add p5-LWP-UserAgent-Determined-*.tgz
Password:
# pkg_find libidn
libidn-*.tgz
# sudo pkg_add libidn-*.tgz
Password:
# pkg_find wget
wget-*.tgz
# sudo pkg_add wget-*.tgz
Password:

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

Run ldd to display the shared objects (.so) needed to run the wget binary. If
there are any shared objects in /usr/local you will need to run ldconfig.
# ldd /usr/local/bin/wget
/usr/local/bin/wget:
        Start    End      Type Open Ref GrpRef Name
        1c000000 3c028000 exe  1    0   0      /usr/local/bin/wget
        0b2a4000 2b2af000 rlib 0    1   0      /usr/lib/libssl.so.18.0
        0fed4000 2ff11000 rlib 0    1   0      /usr/lib/libcrypto.so.20.1
        0652c000 26533000 rlib 0    1   0      /usr/lib/libz.so.4.1
        0cf72000 2cf9e000 rlib 0    1   0      /usr/local/lib/libidn.so.17.0
        0d6ed000 2d6f1000 rlib 0    2   0      /usr/local/lib/libintl.so.6.0
        0451f000 245ff000 rlib 0    2   0      /usr/local/lib/libiconv.so.6.0
        0762a000 27658000 rlib 0    1   0      /usr/lib/libc.so.65.0
        0ee1e000 0ee1e000 rtld 0    1   0      /usr/libexec/ld.so
# sudo ldconfig /usr/lib /usr/local/lib
Password:

# 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 /etc/
Password:
# rm -f certdata.txt mk-ca-bundle.pl


3 - Msmtp installation

Install the msmtp package.
# pkg_find msmtp
clamsmtp-*.tgz
msmtp-*.tgz
# sudo pkg_add msmtp-*.tgz
Password:


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.
# msmtp --version | grep 'System configuration'
System configuration file name: /etc/msmtprc
# pkg_info -L msmtp-* | grep msmtprc
/usr/local/share/examples/msmtp/msmtprc-system.example
/usr/local/share/examples/msmtp/msmtprc-user.example

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 /etc/ca-bundle.crt
  auth on
  user testuser@gmail.com
  password **********
  syslog LOG_MAIL
# sudo vi /etc/msmtprc
Password:
# sudo chmod 600 /etc/msmtprc
Password:


5 - Example 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=/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-2024.