How to get ssmtp working with Zoneminder

From ZoneMinder Wiki
Jump to navigationJump to search

<Home

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. MSMTP can also be used.

Background

SSMTP is a really light mail transfer agent (MTA) that a lot of people use in Linux. It is much more lightweight than 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. NOTE: After debian 9, ssmtp has been deprecated in favor of alternatives such as msmtp. See https://wiki.zoneminder.com/SMS_Notifications

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

root=pp@gmail.com
mailhub=smtp.gmail.com:587
hostname=localhost
RewriteDomain=gmail.com
UseSTARTTLS=YES
UseTLS=YES
AuthUser=pp@gmail.com
AuthPass=something  # remember if you use 2 factor auth, generate an app specific password

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:

root:pp@gmail.com:smtp.gmail.com:587
www-data:pp@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

Below two options in 1.30 or higher

  • SSMTP_Mail: check this.
  • SSMTP_Path: Depends on your distribution. Try $ whereis ssmtp

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.

While we are in this section, its also very easy to get zone minder to attach images of the alarm by modifying the EMAIL_BODY For example, I've added this line to my EMAIL_BODY in ZM

Here are the first and the the highest score image in the alarm: %EI1% %EIM%

You should read http://zoneminder.readthedocs.org/en/latest/userguide/options/options_email.html for more details on variables you can use

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

NOTE: This is NOT needed for ZM 1.30 and above

This text only applies to ZM < 1.30

(credit to user uboltun: http://www.zoneminder.com/forums/viewtopic.php?f=21&t=16611&p=66381&hilit=zmfilter+ssmtp+send#p66381)

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

Testing if it works

To be safe, restart ZM (Frankly I never needed to - but what the heck, in some odd case if it does not pick up these changes, why bang your head. ZM has many other things you are going to fret over :-) )

  • Generate an alarm - you can easily do this by "Forcing an Alarm" via one of your monitors (using the web interface)
  • Bring up the filter window, and check on "Email all matches" and use the right matching parameters for your new event
  • Click on "execute"

And cross your fingers and hope you get the email. It is very useful to do a "tail -f /var/log/syslog" in another terminal to monitor what zmfilter.pl is doing (assuming you enabled "Info" level logging in Options->Logging for LOG_LEVEL_SYSLOG in ZM web interface. You should.)

Gotcha while testing an event

I spent a good half hour frustrated by zmfilter.pl not matching my event I wanted to email. Remember, that everytime ZM thinks it has "emailed" an event out, it enables a flag next to the event in its DB that says "IsEmailed=1". This is how ZM knows which events it needs to email to you as zmfilter runs on schedule. So you can't keep testing email with an old event. In my case, I was testing an event when not using the "new mail modules" and ZM thought it sent out, but as I wrote earlier, the old mail modules don't work with ssmtp but because there was no error detected, ZM marked that event as "emailed" and zmfilter would never match it again. So be aware of this.

See Also