Sending mail notification to different users

Forum for questions and support relating to the 1.30.x releases only.
Locked
kmg454
Posts: 58
Joined: Tue Apr 11, 2017 7:43 am

Sending mail notification to different users

Post by kmg454 »

We are running a cloud service for our customers, so there are 20 camera's connected to our Zoneminder cloud system.
A few of my clients want to receive an email when motion is detected but when I use a filter the email is always going to myself as the Zoneminder owner.
Now I had the id to make a php or perl script to send the mail when motion is triggered for a particular camera, so I can make a script for every user.

So making the sendmail script is no problem but how can I get the right link to the event in this email script?
rockedge
Posts: 1173
Joined: Fri Apr 04, 2014 1:46 pm
Location: Connecticut,USA

Re: Sending mail notification to different users

Post by rockedge »

Set up a filter and select to execute a command which would be the PERL script for example that sends the email from an event on a specific camera to a specific recipient.
Image

Might be a method that is easy.
kmg454
Posts: 58
Joined: Tue Apr 11, 2017 7:43 am

Re: Sending mail notification to different users

Post by kmg454 »

That's what I do now, but I want the URL to the event in the mail just like in the original mail from zoneminder, so the client can click on the link to go to the event directly from his mail.
rockedge
Posts: 1173
Joined: Fri Apr 04, 2014 1:46 pm
Location: Connecticut,USA

Re: Sending mail notification to different users

Post by rockedge »

ahhh I understand. I think I was looking into this scenario before. To use the same function that creates the original event link that is generated for the the stock issue zm email. I will see what info I have or find.
kmg454
Posts: 58
Joined: Tue Apr 11, 2017 7:43 am

Re: Sending mail notification to different users

Post by kmg454 »

In the documentation I can find that %EPS% shows the path to the event, so I need the variable from %EPS% send from the filter to the perl mailscript
rockedge
Posts: 1173
Joined: Fri Apr 04, 2014 1:46 pm
Location: Connecticut,USA

Re: Sending mail notification to different users

Post by rockedge »

I am looking into how and where the token %EPS% is being generated.
The email function code is in /usr/bin/zmfilter.pl around line 746. And the function substituteTags around line 582.
the variable tokens like %EC% are set around line 655
kmg454
Posts: 58
Joined: Tue Apr 11, 2017 7:43 am

Re: Sending mail notification to different users

Post by kmg454 »

I was also looking into zmfilter.pl but can't find out how to pass the right token to my mail.pl script. I found some information on the web but they suggest to modify zmfilter.pl. That is not what I want to do because I become in trouble when there is an Zoneminder update. Someone suggest to send the standard mail message to an gmail account and make an forward filter on base of the mail header. That's also a possibility but I prefer to do it with a good script.
rockedge
Posts: 1173
Joined: Fri Apr 04, 2014 1:46 pm
Location: Connecticut,USA

Re: Sending mail notification to different users

Post by rockedge »

I found a way. I am working on refining the script. But It is possible with out changing any ZM code.
create this script /usr/bin/geteventlink make it executable and in a terminal :

Code: Select all

#geteventlink

Code: Select all

#!/usr/bin/env perl

use strict;
use warnings;
use ZoneMinder;

$| = 1;

my @events;
my $dbh = zmDbConnect();
my $sql = "SELECT * FROM Events";
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 );
}

                my $evid = ($events[-1]->{Id});
                my $evmonid = ($events[-1]->{MonitorId});
                print "http://some_zm_domain/zm/index.php?view=event&mode=stream&mid=$evmonid&eid=$evid/g \n";
the script will return the last event recorded as a link.
rockedge
Posts: 1173
Joined: Fri Apr 04, 2014 1:46 pm
Location: Connecticut,USA

Re: Sending mail notification to different users

Post by rockedge »

Here is a script that when triggered by a ZM Filter will send an email with the current event link (I think). You will need to insure these PERL modules are present.

Code: Select all

#!/usr/bin/env perl

use strict;
use warnings;
use ZoneMinder;
use POSIX qw( strftime );
use Email::Sender::Simple qw(sendmail);
use Email::Sender::Transport::SMTP ();
use Email::Simple ();
use Email::Simple::Creator ();


$| = 1;


my @events;
my $dbh = zmDbConnect();
my $sql = "SELECT * FROM Events WHERE MonitorId = 1";
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 );
}

my $evid = ($events[-1]->{Id});
my $evmonid = ($events[-1]->{MonitorId});
my $url = "http://some.server.name/zm/index.php?view=event&mode=stream&mid=$evmonid&eid=$evid/g \n";
my $time = localtime();
my $smtpserver = 'smtp.gmail.com';
my $smtpssl = 'SSL';
my $smtpport = 465;
my $smtpuser   = some.name@gmail.com';
my $smtppassword = 'somepassword';

my $transport = Email::Sender::Transport::SMTP->new({
  host => $smtpserver,
  ssl  => $smtpssl,
  port => $smtpport,
  sasl_username => $smtpuser,
  sasl_password => $smtppassword,
 });

my $email = Email::Simple->create(
  header => [
    To      => some.name@gmail.com',
    From    => 'needs.a.name@gmail.com',
    Subject => 'Events occured!',
  ],
  body => "$time Monitor- $evmonid: An Event occured $url \n ",
 );
 
 sendmail($email, { transport => $transport });

exit 1;	
with this Filter setup:
Image
Hope it helps.
kmg454
Posts: 58
Joined: Tue Apr 11, 2017 7:43 am

Re: Sending mail notification to different users

Post by kmg454 »

Thanks for guide me in the right direction, I simplified the script a bit because I use the smtp mailer on the zoneminder server

Code: Select all

#!/usr/bin/perl -w

use strict;
use warnings;
use ZoneMinder;
use MIME::Lite;

$| = 1;


my @events;
my $dbh = zmDbConnect();
my $sql = "SELECT * FROM Events WHERE MonitorId = 4";
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 );
}

my $evid = ($events[-1]->{Id});
my $evmonid = ($events[-1]->{MonitorId});
my $url = "http://server-name/zm/index.php?view=event&mode=stream&mid=$evmonid&eid=$evid/g \n";
my $subject   = "zoneminder has detected motion";
my $sender    = 'sender-mailadres';
my $recipient = 'my-mailadres';


my $data = qq{ <BODY BGCOLOR=#FFFFFF> 
<H3>There was motion on camera 4</H3>
<P ALIGN="left">
<A href="$url">Click her for more</A> 
</P> 

</BODY>
};

my $mime = MIME::Lite->new(
    'From'    => $sender,
    'To'      => $recipient,
    'Subject' => $subject,
    'Type'    => 'text/html',
    'Data'    => $data
);

$mime->send() or die "Failed to send mail\n";
rockedge
Posts: 1173
Joined: Fri Apr 04, 2014 1:46 pm
Location: Connecticut,USA

Re: Sending mail notification to different users

Post by rockedge »

Well done. Looks even better using the same mail methods as ZM.
kmg454
Posts: 58
Joined: Tue Apr 11, 2017 7:43 am

Re: Sending mail notification to different users

Post by kmg454 »

I want also send different mails for a zone but can't find the zone names in the db
rockedge
Posts: 1173
Joined: Fri Apr 04, 2014 1:46 pm
Location: Connecticut,USA

Re: Sending mail notification to different users

Post by rockedge »

you want each individual zone setup for a single camera to send the mail notification of being triggered?
kmg454
Posts: 58
Joined: Tue Apr 11, 2017 7:43 am

Re: Sending mail notification to different users

Post by kmg454 »

I put this question in the wrong topic it must be here viewtopic.php?f=36&t=26066&p=100134#p100134
Locked