Packetwatch.net

Email notifications using Msmtp (TLS) in Mythbuntu



Last modified: Apr. 6, 2013

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 Mythbuntu. Msmtp is an SMTP client used to send emails.
This has been tested in Mythbuntu 12.04.2 on the amd64 architecture.


2 - Dependencies

Install the following packages.
# sudo apt-get install apt-file
[sudo] password for user:
# sudo apt-get install wget
[sudo] password for user:
# apt-file update
# apt-file list wget | grep ^wget: | awk '{print $2}' | grep bin
/usr/bin/wget
# wget --no-check-certificate https://raw.github.com/bagder/curl/master/lib/mk-ca-bundle.pl
# perl mk-ca-bundle.pl
# sudo chown root:root ca-bundle.crt
[sudo] password for user:
# sudo chmod 555 ca-bundle.crt
[sudo] password for user:
# sudo mv ca-bundle.crt /etc/
[sudo] password for user:
# sudo rm -f certdata.txt mk-ca-bundle.pl
[sudo] password for user:


3 - Msmtp installation

Install the msmtp package.
# sudo apt-get install msmtp
[sudo] password for user:


4 - Msmtp configuration

Find where the msmtp binary was installed to.
# apt-file list msmtp | grep ^msmtp: | awk '{print $2}' | grep bin
/usr/bin/msmtp

Find where the configuration file should be put.
# /usr/bin/msmtp --version | grep msmtprc
System configuration file name: /etc/msmtprc
User configuration file name: /home/user/.msmtprc
# apt-file list msmtp | grep ^msmtp: | awk '{print $2}' | grep msmtprc
/usr/share/doc/msmtp/examples/msmtprc-system.example
/usr/share/doc/msmtp/examples/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
[sudo] password for user:
# sudo chmod 600 /etc/msmtprc
[sudo] password for user:


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_time=`$date +%H:%M`
current_day=`$date +%m/%d/%y`
printf=/usr/bin/printf
from="testuser@gmail.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.