Packetwatch.net

Configuring email for system event notification in FreeBSD



Last modified: Sep. 14, 2009

Contents
1 - Summary
2 - Sendmail configuration
3 - Example shell script
4 - Service check


1 - Summary

This guide will show you how to configure email using the mail utility for
system event notication in FreeBSD. Mail is a command line utility that sends
email using SMTP. This has been tested in FreeBSD 7.0.


2 - Sendmail configuration

Sendmail is included in FreeBSD by default. Edit /etc/rc.conf.local to have
the following options so they start on bootup. The sendmail submit process
allows posting messages. It can forward all outgoing messages to the mail
gateway specified in the SMART_HOST option. The sendmail msp_queue process
scans periodically the messages in the queue and make sure they don't get
stuck there.
  sendmail_enable="NO"
  sendmail_submit_enable="YES"
  sendmail_submit_flags="-L sm-mta -bd -q30m"
  sendmail_outbound_enable="NO"
  sendmail_msp_queue_enable="YES"
# sudo vi /etc/rc.conf.local
Password:

Get the hostname of the computer.
# hostname
server.test.com

Do the following to create an initial configuration file.
# sudo mkdir -p /root/backups/originals
Password:
# sudo cp -Rp /etc/mail/ /root/backups/originals/mail/
Password:
# cd /etc/mail
# sudo make
Password:

After this you should have some files that are named server.test.com. Make the
following changes to server.test.com.mc.
  FEATURE(masquerade_envelope)
  FEATURE(genericstable, `hash -o /etc/mail/genericstable')
  GENERICS_DOMAIN_FILE(`/etc/mail/generics-domains')
  define(`SMART_HOST', `mail.test.com')
  define(`confCW_FILE', `-o /etc/mail/local-host-names')
  dnl DAEMON_OPTIONS(`Name=IPv6, Family=inet6, Modifiers=O')
# sudo vi server.test.com.mc
Password:

Create the following file so that you can rewrite your (envelope) From:
address for your host.
  server.test.com
# sudo vi generics-domains
Password:

Create the following file which contains the mapping between your local email
address and your external one. We will use the root user so you might want to
change the comment field in /etc/passwd appropriately.
  root                noreply@test.com
# sudo vi genericstable
Password:

Create the following file so that the computer can deliver email to local users.
  server.test.com
  server
# sudo vi local-host-names
Password:

Rebuild the configuration files.
# sudo make all install stop
Password:
# sudo ps aux | grep sendmail
Password:

Edit server.test.com.cf to have the following to have sendmail not listen on
tcp port 587.
  #O DaemonPortOptions=Port=587, Name=MSA, M=E
# sudo vi server.test.com.cf
Password:

Start the sendmail service.
# sudo make all install start
Password:
# sudo cat /var/run/sendmail.pid
Password:
813
/usr/sbin/sendmail -L sm-mta -bd -q30m


3 - Example shell script

Here is an example shell script that sends an email.

#!/bin/sh

clear=/usr/bin/clear
echo=/bin/echo
mail=/usr/bin/mail
hostname=/bin/hostname
host=`$hostname`
date=/bin/date
current_time=`$date +%H:%M`
current_day=`$date +%m/%d/%Y`
recipient="testuser@test.com"
subject="Test sent from $host"
body="This is a test. It was sent at $current_time on $current_day."

$clear
$echo $body | $mail -s "$subject" $recipient
exit 0


4 - Service check

Reboot your computer. Log in like normal and check to see that the sendmail
service is running. That's it, now you have configured email for system event
notification in FreeBSD.
# sudo shutdown -r now
Password:
# sudo ls -1 /var/run/sendmail.pid
Password:
/var/run/sendmail.pid
# sudo cat /var/run/sendmail.pid
Password:
596
/usr/sbin/sendmail -L sm-mta -bd -q30m


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