Packetwatch.net

Email notifications using Msmtp in OpenBSD



Last modified: Mar. 23, 2010

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


1 - Summary

This guide will show how to send emails for system events using msmtp in
OpenBSD. Msmtp is an SMTP client used to send email. This has been tested in
OpenBSD 4.6.


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


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

Run ldd to display the shared objects (.so) needed to run the msmtp binary. If
there are any shared objects in /usr/local you will need to run ldconfig.
# ldd /usr/local/bin/msmtp
/usr/local/bin/msmtp:
        Start    End      Type Open Ref GrpRef Name
        1c000000 3c008000 exe  1    0   0      /usr/local/bin/msmtp
        085b3000 285b7000 rlib 0    2   0      /usr/local/lib/libintl.so.4.0
        09ec3000 29fa2000 rlib 0    2   0      /usr/local/lib/libiconv.so.6.0
        0b847000 2b854000 rlib 0    1   0      /usr/lib/libssl.so.15.0
        03c39000 23c79000 rlib 0    1   0      /usr/lib/libcrypto.so.18.0
        0cb58000 2cb84000 rlib 0    1   0      /usr/local/lib/libidn.so.16.30
        0ed02000 2ede1000 rlib 0    1   0      /usr/local/lib/libiconv.so.6.0
        0433c000 24340000 rlib 0    1   0      /usr/local/lib/libintl.so.4.0
        0141c000 21455000 rlib 0    1   0      /usr/lib/libc.so.51.0
        08b4d000 08b4d000 rtld 0    1   0      /usr/libexec/ld.so
# sudo ldconfig /usr/lib /usr/local/lib
Password:

Add the following to the /etc/rc network script.
  /sbin/ldconfig /usr/lib /usr/local/lib
# sudo vi /etc/rc
Password:

Find where the configuration file should be put.
# strings /usr/local/bin/msmtp | grep msmtp.conf 
# pkg_info -L msmtp-* | grep msmtp.conf     

You can create your own configuration file. Here is a simple example.
  account default
  host mail.test.com
  port 25
  protocol smtp
  from noreply@test.com
  syslog LOG_MAIL
# sudo mkdir /usr/local/etc
Password:
# sudo touch /usr/local/etc/msmtp.conf
Password:
# sudo chmod 600 /usr/local/etc/msmtp.conf
Password:
# sudo vi /usr/local/etc/msmtp.conf
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="Test <noreply@test.com>"
recipient="testuser@test.com"
#recipient="testuser1@test.com,testuser2@test.com"
msmtp=/usr/local/bin/msmtp
msmtp_config=/usr/local/etc/msmtp.conf

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.