RT#7869: fix error messages when send_email fails in the job queue
[freeside.git] / FS / FS / Misc.pm
index 39be1fb..895681f 100644 (file)
@@ -343,21 +343,26 @@ sub send_email {
   $smtp_opt{'port'} = $port;
 
   my $transport;
-  if ( $enc eq 'starttls' ) {
+  if ( defined($enc) && $enc eq 'starttls' ) {
     $smtp_opt{$_} = $conf->config("smtp-$_") for qw(username password);
     $transport = Email::Sender::Transport::SMTP::TLS->new( %smtp_opt );
   } else {
     if ( $conf->exists('smtp-username') && $conf->exists('smtp-password') ) {
       $smtp_opt{"sasl_$_"} = $conf->config("smtp-$_") for qw(username password);
     }
-    $smtp_opt{'ssl'} = 1 if $enc eq 'tls';
+    $smtp_opt{'ssl'} = 1 if defined($enc) && $enc eq 'tls';
     $transport = Email::Sender::Transport::SMTP->new( %smtp_opt );
   }
-
-  eval { sendmail($message, { transport => $transport }); };
-  ref($@) ? ( $@->code ? $@->code.' ' : '' ). $@->message
-          : $@;
-
+  
+  local $@; # just in case
+  eval { sendmail($message, { transport => $transport }) };
+  if(ref($@) and $@->isa('Email::Sender::Failure')) {
+    return ($@->code ? $@->code.' ' : '').$@->message
+  }
+  else {
+    return $@;
+  }
 }
 
 =item send_fax OPTION => VALUE ...