Email via SMTP patch

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.
Post Reply
User avatar
rdmelin
Posts: 863
Joined: Wed Oct 29, 2003 2:23 pm
Location: Ellensburg, WA USA

Email via SMTP patch

Post by rdmelin »

The subject needs a little explaining.
In response to a users frustration with getting email working, I did some testing. I had it working some time back, but don't use it regularly. I was concerned to find that I could not send notification emails from ZM either using sendmail (postfix in my case) or SMTP.
Local delivery worked fine, but anything sent via my ISP's mailserver disappeared into the ether.
To make a long story short, since a software upgrade my ISP's mail server no longer forwards mail with a Return-Path: <apache@zm.local.domain>

There may be some way to convince postfix to rewrite the Return-Path header, but I spent several frustrating hours and couldn't find it. Neither could I convince the MIME::Entity perl module to set the Return-Path header to the same address as the From: email address.

I don't believe the average user on my ISP network could get email working from ZM. This will most likely be true of more users elsewhere as other ISP's update their servers.

My solution was to rewrite zmfilter.pl sub sendEmail and sub sendMessage to use the MIME::Lite and Net::SMTP perl modules instead of MIME::Entity.

Here is a patch for zmfilter.pl.z that impliments this solution.

Code: Select all

--- zmfilter.pl.z.orig       2005-02-24 06:39:58.000000000 -0800
+++ zmfilter.pl.z  2005-04-09 22:04:37.913023309 -0700
@@ -99,7 +99,8 @@
 my $email_body;
 if ( ZM_OPT_EMAIL )
 {
-       use MIME::Entity;
+       use MIME::Lite;
+       use Net::SMTP;

        ( $email_subject, $email_body ) = ZM_EMAIL_TEXT =~ /subject\s*=\s*"([^\n]*)".*body\s*=\s*"(.*)"/ms;
 }
@@ -108,7 +109,8 @@
 my $message_body;
 if ( ZM_OPT_MESSAGE )
 {
-       use MIME::Entity;
+       use MIME::Lite;
+       use Net::SMTP;

        ( $message_subject, $message_body ) = ZM_MESSAGE_TEXT =~ /subject\s*=\s*"([^\n]*)".*body\s*=\s*"(.*)"/ms;
 }
@@ -725,30 +727,39 @@

        eval
        {
-               my $mail = MIME::Entity->build(
+               ### Create the multipart container
+               my $mail = MIME::Lite->new (
                        From =>ZM_FROM_EMAIL,
-                       To=>ZM_EMAIL_ADDRESS,
-                       Subject=>$subject,
-                       Type=>(($body=~/<html>/)?'text/html':'text/plain'),
-                       Data=>$body
-               );
-
+                       To =>ZM_EMAIL_ADDRESS,
+                       Subject => $subject,
+                       Type =>'multipart/mixed'
+                       );
+               ### Add the text message part
+               $mail->attach (
+                       Type => 'TEXT',
+                       Data => $body
+                       );
+               ### Add the attachments
                foreach my $attachment ( @attachments )
                {
                        print( "Attaching '$attachment->{path}\n" );
                        $mail->attach(
                                Path=>$attachment->{path},
                                Type=>$attachment->{type},
-                               Encoding=>"base64"
+                               Disposition => 'attachment'
                        );
-               }
-
-               $mail->smtpsend();
+               }
+                ### Send the Message
+               MIME::Lite->send('smtp', ZM_EMAIL_HOST, Timeout=>60);
+               $mail->send;
        };
        if ( $@ )
        {
                warn( "Can't send email: $@" );
                return();
+       } else
+       {
+               print( "Notificaton email sent \n" );
        }
        my $sql = "update Events set Emailed = 1 where Id = ?";
        my $sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() );
@@ -782,30 +793,39 @@

        eval
        {
-               my $mail = MIME::Entity->build(
+               # Create the multipart container
+               my $mail = MIME::Lite->new (
                        From =>ZM_FROM_EMAIL,
-                       To=>ZM_MESSAGE_ADDRESS,
-                       Subject=>$subject,
-                       Type=>(($body=~/<html>/)?'text/html':'text/plain'),
-                       Data=>$body
-               );
-
+                       To =>ZM_EMAIL_ADDRESS,
+                       Subject => $subject,
+                       Type =>'multipart/mixed'
+                       );
+               # Add the text message part
+               $mail->attach (
+                       Type => 'TEXT',
+                       Data => $body
+                       );
+               # Add the attachments
                foreach my $attachment ( @attachments )
                {
                        print( "Attaching '$attachment->{path}\n" );
                        $mail->attach(
                                Path=>$attachment->{path},
                                Type=>$attachment->{type},
-                               Encoding=>"base64"
+                               Disposition => 'attachment'
                        );
-               }
-
-               $mail->smtpsend();
+               }
+               # Send the Message
+               MIME::Lite->send('smtp', ZM_EMAIL_HOST, Timeout=>60);
+               $mail->send;
        };
        if ( $@ )
        {
-               warn( "Can't send email: $@" );
+               warn( "Can't send message: $@" );
                return();
+       } else
+       {
+               print( "Notificaton message sent \n" );
        }
        my $sql = "update Events set Messaged = 1 where Id = ?";
        my $sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() );
User avatar
zoneminder
Site Admin
Posts: 5215
Joined: Wed Jul 09, 2003 2:07 pm
Location: Bristol, UK
Contact:

Post by zoneminder »

Thanks Ross. I'll see if I can incorporate this as an option so people can choose which method to use.

Phil
Post Reply