Packetwatch.net

Email notifications using Msmtp in Red Hat Enterprise Linux



Last modified: Jun. 9, 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 Red
Hat Enterprise Linux. Msmtp is an SMTP client used to send email. This has been
tested in Red Hat Enterprise Linux 4 and 5.


2 - Msmtp installation

Download the msmtp RPM package from the ScopServer mirror and then install it.
# cd ~
# wget http://mirrors.scopserv.com/dist/packages/msmtp/msmtp-$ver.$dist.scopserv.$arch.rpm
# sudo rpm -ivh msmtp-*
Password: 
# rm -f msmtp-*


3 - Msmtp configuration

Find where the msmtp application was installed to.
# rpm -ql msmtp-* | grep bin
/usr/bin/msmtp

Find where the configuration file should be put.
# strings /usr/bin/msmtp | grep msmtprc
.msmtprc
# /usr/bin/msmtp --version | grep msmtprc
System configuration file name: /etc/msmtprc
User configuration file name: /home/user/.msmtprc

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
# rpm -ql msmtp-* | grep msmtprc
/usr/share/doc/msmtp-*/msmtprc-system.example
/usr/share/doc/msmtp-*/msmtprc-user.example
# sudo cp /usr/share/doc/msmtp-*/msmtprc-system.example /etc/msmtprc
Password:
# sudo chmod 600 /etc/msmtprc
Password:
# sudo vi /etc/msmtprc
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/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-2017.