[Rt-commit] r9933 - rt/branches/3.7-RTIR-RELENG/lib/RT/Interface

ruz at bestpractical.com ruz at bestpractical.com
Thu Dec 13 09:54:59 EST 2007


Author: ruz
Date: Thu Dec 13 09:54:59 2007
New Revision: 9933

Modified:
   rt/branches/3.7-RTIR-RELENG/lib/RT/Interface/Email.pm

Log:
fix sending emails via SMTP 
* MIME::Entity->send doesn't handle SMTP in the right way
* there is smtpsend method, however it doesn't work right
  and send bodies as one line
* use Net::SMTP directly
* use streams instead scalars to avoid high memory usage


Modified: rt/branches/3.7-RTIR-RELENG/lib/RT/Interface/Email.pm
==============================================================================
--- rt/branches/3.7-RTIR-RELENG/lib/RT/Interface/Email.pm	(original)
+++ rt/branches/3.7-RTIR-RELENG/lib/RT/Interface/Email.pm	Thu Dec 13 09:54:59 2007
@@ -443,6 +443,46 @@
             return 0;
         }
     }
+    elsif ( $mail_command eq 'smtp' ) {
+        require Net::SMTP;
+        my $smtp = do { local $@; eval { Net::SMTP->new(
+            Host  => RT->Config->Get('SMTPServer'),
+            Debug => RT->Config->Get('SMTPDebug'),
+        ) } };
+        unless ( $smtp ) {
+            $RT::Logger->crit( "Could not connect to SMTP server.");
+            return 0;
+        }
+
+        # duplicate head as we want drop Bcc field
+        my $head = $args{'Entity'}->head->dup;
+        my @recipients = map $_->address, Mail::Address->parse(
+            map $head->get($_), qw(To Cc Bcc)
+        );
+        $head->delete('Bcc');
+
+        my $sender = RT->Config->Get('SMTPFrom')
+            || $args{'Entity'}->head->get('From');
+        chomp $sender;
+
+        my $status = $smtp->mail( $sender )
+            && $smtp->recipient( @recipients );
+
+        if ( $status ) {
+            $smtp->data;
+            my $fh = $smtp->tied_fh;
+            $head->print( $fh );
+            print $fh "\n";
+            $args{'Entity'}->print_body( $fh );
+            $smtp->dataend;
+        }
+        $smtp->quit;
+
+        unless ( $status ) {
+            $RT::Logger->crit( "$msgid: Could not send mail via SMTP." );
+            return 0;
+        }
+    }
     else {
         local ($ENV{'MAILADDRESS'}, $ENV{'PERL_MAILERS'});
 
@@ -451,11 +491,6 @@
             $ENV{'PERL_MAILERS'} = RT->Config->Get('SendmailPath');
             push @mailer_args, split(/\s+/, RT->Config->Get('SendmailArguments'));
         }
-        elsif ( $mail_command eq 'smtp' ) {
-            $ENV{'MAILADDRESS'} = RT->Config->Get('SMTPFrom') || $args{'Entity'}->head->get('From');
-            push @mailer_args, ( Server => RT->Config->Get('SMTPServer') );
-            push @mailer_args, ( Debug  => RT->Config->Get('SMTPDebug') );
-        }
         else {
             push @mailer_args, RT->Config->Get('MailParams');
         }


More information about the Rt-commit mailing list