send html email w/inline images

If you've made a patch to quick fix a bug or to add a new feature not yet in the main tree then post it here so others can try it out.
muckleroy
Posts: 8
Joined: Wed Nov 26, 2008 3:34 pm

Post by muckleroy »

Hmmm if memory serves that is all it returned for me, thats why I added the yourdomain stuff.

Just a guess, but you may need to go back to your zoneminder options and see how you have the various parameters set.
Voltage54
Posts: 23
Joined: Sat Feb 21, 2009 1:35 am

Post by Voltage54 »

Argh! I still can't manage to get it to work.

I'm quite sure all the settings in the zoneminder web config are correct, however I realised that even if I make it work, I require a username/pass to access the http:// connection of zoneminder, so wouldn't it block my email client from viewing the picture anyway?

Viewing carteriii's original code (if you click the quote button on his post, you can see the plain text source code to avoid any problems with the <bcode> option), it seems to indicate that a *.jpg is attached in the email, rather than linked to (via http://yourdomain.com/zm/....picture.jpg). I have tried both sets of code he has provided and neither work for me... I keep getting a little box with a red cross in it in the place of the 'inline_image1'.

Has anyone managed to get this to work? Please let me know how you solved your problem.. It's really starting to frustrate me as I get quite a few emails per day and having to open each email, save the picture, then open the picture is driving me nuts :(
muckleroy
Posts: 8
Joined: Wed Nov 26, 2008 3:34 pm

Post by muckleroy »

Voltage54 wrote:Argh! I still can't manage to get it to work.

I'm quite sure all the settings in the zoneminder web config are correct, however I realised that even if I make it work, I require a username/pass to access the http:// connection of zoneminder, so wouldn't it block my email client from viewing the picture anyway?

Viewing carteriii's original code (if you click the quote button on his post, you can see the plain text source code to avoid any problems with the <bcode> option), it seems to indicate that a *.jpg is attached in the email, rather than linked to (via http://yourdomain.com/zm/....picture.jpg). I have tried both sets of code he has provided and neither work for me... I keep getting a little box with a red cross in it in the place of the 'inline_image1'.

Has anyone managed to get this to work? Please let me know how you solved your problem.. It's really starting to frustrate me as I get quite a few emails per day and having to open each email, save the picture, then open the picture is driving me nuts :(
Can you post up the contents of the e-mail you are receiving?
Voltage54
Posts: 23
Joined: Sat Feb 21, 2009 1:35 am

Post by Voltage54 »

nope because I got it working :D:D

For some reason, when the script is set to multipart/alternative ... the images will refuse to load. On multipart/mixed it works (and adds the plain text part as an attachment... which I can live with).

Anyway, the end result is (I believe this is a direct copy from the first post in this thread, however just for clarity):

zmfilter.pl:

sub sendEmail
{
my $filter = shift;
my $event = shift;

if ( !ZM_FROM_EMAIL )
{
warn( "No 'from' email address defined, not sending email" );
return( 0 );
}
if ( !ZM_EMAIL_ADDRESS )
{
warn( "No email address defined, not sending email" );
return( 0 );
}

Info( "Creating notification email\n" );

my $subject = substituteTags( ZM_EMAIL_SUBJECT, $filter, $event );
return( 0 ) if ( !$subject );
my @attachments;
my $body = substituteTags( ZM_EMAIL_BODY, $filter, $event, \@attachments );
return( 0 ) if ( !$body );

Info( "Sending notification email '$subject'\n" );

eval
{
if ( ZM_NEW_MAIL_MODULES )
{
### Create the multipart container
my $mail = MIME::Lite->new (
From => ZM_FROM_EMAIL,
To => ZM_EMAIL_ADDRESS,
Subject => $subject,
Type => "multipart/mixed"
);

### Create an image tag for each attachment
my $attachment_count = 0;
my $img_tags = "";
foreach my $attachment ( @attachments )
{
$attachment_count++;
$img_tags .= qq {<img src="cid:inline_image$attachment_count"><br>};
}

### Add an html message part
$mail->attach (
Type => "text/html",
Data => qq {<body>$img_tags<br>$body</body>}
);

### Add the text message part
$mail->attach (
Type => "TEXT",
Data => $body
);
### Add the attachments
### Add the counter to create an image Id
### Note that is must be declared "my" or else it will
### be considered global and cause the entire zmfilter.pl to fail
$attachment_count = 0;
foreach my $attachment ( @attachments )
{
Info( "Attaching '$attachment->{path}\n" );
$mail->attach(
Path => $attachment->{path},
Type => $attachment->{type},
Id => ('inline_image' . ++$attachment_count),
Disposition => "attachment"
);
}
### Send the Message
MIME::Lite->send( "smtp", ZM_EMAIL_HOST, Timeout=>60 );
$mail->send();
}
else
{
my $mail = MIME::Entity->build(
From => ZM_FROM_EMAIL,
To => ZM_EMAIL_ADDRESS,
Subject => $subject,
Type => (($body=~/<html>/)?'text/html':'text/plain'),
Data => $body
);

foreach my $attachment ( @attachments )
{
Info( "Attaching '$attachment->{path}\n" );
$mail->attach(
Path => $attachment->{path},
Type => $attachment->{type},
Encoding => "base64"
);
}
$mail->smtpsend( Host => ZM_EMAIL_HOST, MailFrom => ZM_FROM_EMAIL );
}
};
if ( $@ )
{
warn( "Can't send email: $@" );
return( 0 );
}
else
{
Info( "Notification email sent\n" );
}
my $sql = "update Events set Emailed = 1 where Id = ?";
my $sth = $dbh->prepare_cached( $sql ) or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
my $res = $sth->execute( $event->{Id} ) or Fatal( "Can't execute '$sql': ".$sth->errstr() );

return( 1 );
}


And, the ZM_EMAIL_BODY:

%EI1%

%EIM%

<b>WARNING!</b><br><br>
An alarm has been detected on your installation of ZoneMinder.<br><br>

The alarm occurred at <b>%MN%</b> at %ET%.<br><br>

The cause was %EC%, with a maximum event score of %ESM%.<br><br>

Currently, there are %MET% total events logged at %MN%.<br><br>

This alarm was matched by the %FN% filter and can be viewed at<br>
<br>
<A href="%EPS%">http://yourdomain.com/zm</A>

NB: I have ZM_NEW_MAIL_MODULES selected
muckleroy
Posts: 8
Joined: Wed Nov 26, 2008 3:34 pm

Post by muckleroy »

Good job, now just sit back and enjoy!
insippo
Posts: 39
Joined: Mon Dec 27, 2010 11:45 am
Location: estonia

Post by insippo »

Voltage54 wrote:nope because I got it working :D:D

For some reason, when the script is set to multipart/alternative ... the images will refuse to load. On multipart/mixed it works (and adds the plain text part as an attachment... which I can live with).

Anyway, the end result is (I believe this is a direct copy from the first post in this thread, however just for clarity):

zmfilter.pl:

sub sendEmail
{
my $filter = shift;
my $event = shift;

if ( !ZM_FROM_EMAIL )
{
warn( "No 'from' email address defined, not sending email" );
return( 0 );
}
if ( !ZM_EMAIL_ADDRESS )
{
warn( "No email address defined, not sending email" );
return( 0 );
}

Info( "Creating notification email\n" );

my $subject = substituteTags( ZM_EMAIL_SUBJECT, $filter, $event );
return( 0 ) if ( !$subject );
my @attachments;
my $body = substituteTags( ZM_EMAIL_BODY, $filter, $event, \@attachments );
return( 0 ) if ( !$body );

Info( "Sending notification email '$subject'\n" );

eval
{
if ( ZM_NEW_MAIL_MODULES )
{
### Create the multipart container
my $mail = MIME::Lite->new (
From => ZM_FROM_EMAIL,
To => ZM_EMAIL_ADDRESS,
Subject => $subject,
Type => "multipart/mixed"
);

### Create an image tag for each attachment
my $attachment_count = 0;
my $img_tags = "";
foreach my $attachment ( @attachments )
{
$attachment_count++;
$img_tags .= qq {<img><br>};
}

### Add an html message part
$mail->attach (
Type => "text/html",
Data => qq {<body>$img_tags<br>$body</body>}
);

### Add the text message part
$mail->attach (
Type => "TEXT",
Data => $body
);
### Add the attachments
### Add the counter to create an image Id
### Note that is must be declared "my" or else it will
### be considered global and cause the entire zmfilter.pl to fail
$attachment_count = 0;
foreach my $attachment ( @attachments )
{
Info( "Attaching '$attachment->{path}\n" );
$mail->attach(
Path => $attachment->{path},
Type => $attachment->{type},
Id => ('inline_image' . ++$attachment_count),
Disposition => "attachment"
);
}
### Send the Message
MIME::Lite->send( "smtp", ZM_EMAIL_HOST, Timeout=>60 );
$mail->send();
}
else
{
my $mail = MIME::Entity->build(
From => ZM_FROM_EMAIL,
To => ZM_EMAIL_ADDRESS,
Subject => $subject,
Type => (($body=~/<html>/)?'text/html':'text/plain'),
Data => $body
);

foreach my $attachment ( @attachments )
{
Info( "Attaching '$attachment->{path}\n" );
$mail->attach(
Path => $attachment->{path},
Type => $attachment->{type},
Encoding => "base64"
);
}
$mail->smtpsend( Host => ZM_EMAIL_HOST, MailFrom => ZM_FROM_EMAIL );
}
};
if ( $@ )
{
warn( "Can't send email: $@" );
return( 0 );
}
else
{
Info( "Notification email sent\n" );
}
my $sql = "update Events set Emailed = 1 where Id = ?";
my $sth = $dbh->prepare_cached( $sql ) or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
my $res = $sth->execute( $event->{Id} ) or Fatal( "Can't execute '$sql': ".$sth->errstr() );

return( 1 );
}


And, the ZM_EMAIL_BODY:

%EI1%

%EIM%

<b>WARNING!</b><br><br>
An alarm has been detected on your installation of ZoneMinder.<br><br>

The alarm occurred at <b>%MN%</b> at %ET%.<br><br>

The cause was %EC%, with a maximum event score of %ESM%.<br><br>

Currently, there are %MET% total events logged at %MN%.<br><br>

This alarm was matched by the %FN% filter and can be viewed at<br>
<br>
<A>http://yourdomain.com/zm</A>

NB: I have ZM_NEW_MAIL_MODULES selected
Is it necessary to new file a zmfilter.pl such content?
My mother tongue is not english.Sorry if I do not speak correctly.
kazy
Posts: 7
Joined: Mon Jan 23, 2012 1:42 pm

Re: send html email w/inline images

Post by kazy »

Hi, all

Forgive my leak of knowlage, but i'm pretty new on linux - zoneminder - perl scripts :)

This script is for add more pictures to mail alerts notification ?
Or just to show alerts pictures in mail content ?

I search a way to send to send a bundle of alerts pictures ( something like 1 picture for every 3 sec of the event) instead of a video. But zoneminder only accept "Firt" and "Max score" picture attachement.

This script can help me ?

thx
User avatar
iconnor
Posts: 2896
Joined: Fri Oct 29, 2010 1:43 am
Location: Toronto
Contact:

Re: send html email w/inline images

Post by iconnor »

So this is an old thread. Just wanted to say that I merged the ideas here into current master. 1.37.31. attached images are now inline.
Post Reply