Perl Script to send images from zoneminder events in a text

Forum for questions and support relating to the 1.34.x releases only.
Post Reply
TheBossHogg
Posts: 5
Joined: Tue Jan 26, 2021 2:47 pm

Perl Script to send images from zoneminder events in a text

Post by TheBossHogg »

Here is a script we made to send the image of the newest event and send it

This Script took a lot of work to figure out, but it is really cool and handy!

Note: This is working, with the "Save the Jpeg's" option set to off in Zoneminder

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