Perl Script to send images from Zoneminder Events which can be used with sending event details to Multiple Destinations

Add any particular hints or tricks you have found to help with your ZoneMinder experience.
Post Reply
TheBossHogg
Posts: 5
Joined: Tue Jan 26, 2021 2:47 pm

Perl Script to send images from Zoneminder Events which can be used with sending event details to Multiple Destinations

Post by TheBossHogg »

Here is a script we made to send the image of the newest event and send it through either text or email depending on how you configure it

This Script took a lot of work to figure out, but it is really cool and handy!
Steps to get this to work:
Create a filter in zoneminder and select the script to be ran
Through the filter you select the script, and through the filter you can set the times and dates you want it to run
This Scripts needs to run in the background
This also works with motion detection option on zoneminder

Please test it and let me know, would this one is naturally friendlier to the system i am using, i tried to make it as general as possible but let me know and we can troubleshoot!!!!!!

Note: This is working, with the "Save the Jpeg's" option set to off in Zoneminder
Note: This can be set in the filters in zoneminder and run it through there
Note:You can send to multiple people and events based on this script
Note: this works in multiple ways depending on how you configure it, you can configure it to send to an email or text
Note: this script works from command line but you need to tell zoneminder to use this script
Note: ensure that the monitor ID is correct for the camera or surveillance system you want to use.
note: when enetering the script in zoneminder the name of the script will suffice no need to put /usr/bin just the name of the script
Note: please always be careful and test it before you use it live, if monitor id or filter is not set up right you will get bombarded with alerts
Edit: if you need any help please send me a message on here, i check my inbox once a day, so please let me know id love to help and debug and keep this community strong and going!!!
We can communicate through here or using any Voice service (Discord, Skype ETC) just message me for the details if you need to!!!

Here is the Script, im sure this could be simplified but this is a working version:
#!/usr/bin/perl -w
# Only edit between the stanzas where it says edit before and after.
# This setup works when you have Save JPEGs turned OFF in you Monitor Storage Settings.

use strict;
use warnings;
use ZoneMinder;
use MIME::Lite;
use POSIX qw(strftime);
#use Getopt::Std;

$| = 1;

my @events;
my $dbh = zmDbConnect();
my $sql = "SELECT * FROM Events WHERE MonitorId = 3";
my $sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() );
my $res = $sth->execute() or die( "Can't execute '$sql': ".$sth->errstr() );

while ( my $events = $sth->fetchrow_hashref() ) {
push( @events, $events );
}

# ---- Edit after this comment only ----
my $client = "Johnny Zoneminder";
my $sender = 'root@johnnyzoneminder.com';
my $recipient = 'XXXXXXXXXX@mms.att.net';
#my $recipient = 'root@johnnyzoneminder.com';
# ---- Edit before this comment only ----

my $datestring = strftime "%F", localtime;
my $evid = ($events[-1]->{Id});
my $evmonid = ($events[-1]->{MonitorId});
my $subject = "$client Zoneminder has detected motion";

# ---- Edit after this comment only ----
my $image = "/Mystuff/zm/$evmonid/$datestring/$evid/snapshot.jpg";
my $url = "http://johnnyzoneminder.com/zm/index.ph ... id=$evid/g \n";
# ---- Edit before this comment only ----


my $data = qq{ <BODY BGCOLOR=#999999>
<H3>There was motion on camera $evmonid</H3>
<P ALIGN="left">
<A href="$url">Click here for more.</A> This was Event ID $image
</P>

</BODY>
};

my $mime = MIME::Lite->new(
#my $mime = MIME::Lite:HTML(
'From' => $sender,
'To' => $recipient,
'Subject' => $subject,
# 'Type' => 'text/html', #This one will send the link
'Type' => 'image/jpeg',
#'Data' => $image,
'Encoding' => 'base64',
'Path' => $image,
);

$mime->send() or die "Failed to send mail\n";
Post Reply