How to get ssmtp working with Zoneminder

From ZoneMinder Wiki
Jump to navigationJump to search

Why do you need this

If you want Zoneminder to send you emails about alarms, you need to configure a way for ZM to send emails. SSMTP is one of several ways, and a lightweight and efficient one.


Background

SSMTP is a really light mail transfer agent (MTA) that a lot of people use in Linux. It is much more lightweight that sendmail for sure and I am told postfix too. Note that SSMTP does not really run a daemon process in your box, so trying "telnet localhost 25" is going to fail (and also why a traditional Zoneminder configuration of using local host fails) Instead what is does is this:

  • It installs a program called ssmtp that you can use to send emails
  • Creates a soft link to point sendmail to ssmtp, so you can use the sendmail command too

If you are running ZM on a "not to high end" machine and are monitoring more than 3-4 HD cameras, you will want to be prudent on what you want to install. SSMTP is a great choice.


Installation of SSMTP

Installation is very simple

sudo apt-get update
apt-get install ssmtp
apt-get install mailutils

You may not need mailutils, but that installs the "mail" utility that I find quite handy to send emails from the command line.


Configuration of SSMTP

Let's assume you want to hook it up to send emails using GMAIL as your outbound SMTP

ssmtp.conf

The SSMTP configuration file lie in /etc/ssmtp/ssmtp.conf. Here is a file that works for gmail. You will need to edit it to fit your gmail configuration

#
# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
root=postmaster

# The place where the mail goes. The actual machine name is required no 
# MX records are consulted. Commonly mailhosts are named mail.domain.com
mailhub=mail

# Where will the mail seem to come from?
#rewriteDomain=

# The full hostname
hostname=camerapc # ARC: Change this to your zone minder hostname

# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address
# NO - Use the system generated From: address
#FromLineOverride=YES
root=email@gmail.com              # ARC: Change this to your gmail id
mailhub=smtp.gmail.com:587
rewriteDomain=
hostname=email@gmail.com     #ARC: I use my gmail id here too
UseSTARTTLS=YES
AuthUser=email                         #ARC: I use my gmail id without domain here
AuthPass=xxxxxx                       #ARC: If you use 2 step authentication in gmail, you MUST generate an app specific password for gmail and add that here
FromLineOverride=YES

revaliases

Next up you will need to edit /etc/ssmtp/revaliases to configure it with the correct "From" addresses while sending email

Here is mine:

 sSMTP aliases
# 
# Format:       local_account:outgoing_address:mailhub
#
# Example: root:your_login@your.domain:mailhub.your.domain[:port]
# where [:port] is an optional port number that defaults to 25.
root:user@gmail.com:smtp.gmail.com:587
www-data:user@gmail.com:smtp.gmail.com:587

What I've done above is told ssmtp if "root", or "www-data" users ever send emails, use "user@gmail.com" as From (Change this to your gmail address). I wasn't sure if ZM was using www-data or root to send emails, so I specified both. I think you only need root.

Checking to see mail works

Try this from your shell command line

echo "Hello, World" | mail -s "My email check" user@gmail.com

Replace user@gmail.com with your gmail. See if you get your email. If not, go back and see what went wrong. Checking /var/syslog is a great place to debug. For example, here is the snipped from /var/syslog as I sent the email:

root@camerapc:/home/user# tail -f /var/log/syslog
Mar 22 07:50:09 camerapc sSMTP[30843]: Creating SSL connection to host
Mar 22 07:50:10 camerapc sSMTP[30843]: SSL connection using RSA_ARCFOUR_SHA1
Mar 22 07:50:13 camerapc sSMTP[30843]: Sent mail for user@gmail.com (221 2.0.0 closing connection 10sm6941954qha.38 - gsmtp) uid=0 username=root outbytes=424


Configuration of ZoneMinder

Right, all you've done so far is make sure your linux box can send emails. ZoneMiner still does not have a clue. To make ssmtp work, you need to unfortunately modify /usr/bin/zmfilter.pl as it's not currently set up correctly to use a mail solution that does not run a mail server daemon

So let's get started. First you need to make some config changes via the ZM web interface:

  • Go to Options->Email
  • Make sure OPT_EMAIL is checked (zmfilter.pl checks to see if this option is checked. If not, it will not send emails)
  • In the EMAIL_ADDRESS field enter the email address you want to get these alarms
  • EMAIL_SUBJECT and EMAIL_BODY are self explanatory
  • Make sure NEW_MAIL_MODULES is checked (VERY IMPORTANT)
  • EMAIL_HOST: put in localhost
  • FROM_EMAIL: I put in root@localhost
  • URL: If you have configured your ZM to be accessible using a public DNS (maybe using no-ip DDNS or others), you should put in the web address here. It will be something like http://yourzoneminderurl.domain:port/zm (unless you have changed it). When you get alarms, ZM provides a link and uses this url as a base. You can put in a LAN IP here too, but then you will only be able to access it from your LAN

BTW, the reason you need to use NEW_MAIL_MODULES with SSMTP is that if this is checked, ZM uses two Perl packages called MIME::Lite and Net::SMTP instead of MIME::Entity. MIME::Entity uses configuration from mail files that SSMTP does not use. And if you don't check this, you will see your logs saying "mail is sent" but nothing is really sent.

Making sure you have MIME::Lite and Net::SMTP

So after getting various errors, I discovered Ubuntu Server did not install MIME::Lite with its default install.

It's good idea to install them if you don't have it. To check if you have them:

perl -MMIME::Lite -e "print \"Module installed.\\n\";"
perl -MNet::SMTP -e "print \"Module installed.\\n\";"

And to install:

sudo perl -MCPAN -e shell
install MIME::Lite
install Net::SMTP

Making changes to /usr/bin/zmfilter.pl

zmfilter.pl is the king kahuna of filter management. This is the file that ZM uses to regularly monitor filters that you have created (or installed by default, like the one that purges if your disk is 95% full). Its pretty simple to see what is going on. The line you need to change is inside the function "sub sendEmail" and is around line 1030 (not exact, as I've inserted debugging statements in other places)

Here is what you need to comment out and what you need to add in

The code needs to inside the "if ( $Config{ZM_NEW_MAIL_MODULES} )" code block (remember we are using the new mail modules) Around line 1030 or so: (comment out two lines, add a new one):

### Send the Message
#MIME::Lite->send( "sendmail", $Config{ZM_EMAIL_HOST}, Timeout=>60 );
$mail->send('sendmail','/usr/sbin/ssmtp',$Config{ZM_EMAIL_ADDRESS});
#$mail->send();

Basically, you are telling perl to use ssmtp to send the emails