Packetwatch.net

Email notifications using Msmtp in FreeBSD



Last modified: Mar. 22, 2010

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


1 - Summary

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


2 - Msmtp installation

Install the msmtp package.
# sudo pkg_add -r msmtp
Password:


3 - Msmtp configuration

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

Find where the configuration file should be put.
# grep msmtp.conf /usr/local/bin/msmtp
# 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 touch /usr/local/etc/msmtp.conf
Password:
# sudo chmod 600 /usr/local/etc/msmtp.conf
Password:
# sudo vi /usr/local/etc/msmtp.conf
Password:


4 - 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-2024.