import rt 3.4.6
[freeside.git] / rt / lib / RT / Action / SendEmail.pm
index 3c70dc4..1ebcb0c 100755 (executable)
@@ -253,12 +253,28 @@ sub SendMessage {
 
     if ( $RT::MailCommand eq 'sendmailpipe' ) {
         eval {
-            open( MAIL, "|$RT::SendmailPath $RT::SendmailArguments" ) || die $!;
-            print MAIL $MIMEObj->as_string;
-            close(MAIL);
+            # don't ignore CHLD signal to get proper exit code
+            local $SIG{'CHLD'} = 'DEFAULT';
+
+            my $mail;
+            unless( open $mail, "|$RT::SendmailPath $RT::SendmailArguments" ) {
+                die "Couldn't run $RT::SendmailPath: $!";
+            }
+
+            # if something wrong with $mail->print we will get PIPE signal, handle it
+            local $SIG{'PIPE'} = sub { die "$RT::SendmailPath closed pipe" };
+            $MIMEObj->print($mail);
+
+            unless ( close $mail ) {
+                die "Close failed: $!" if $!; # system error
+                # sendmail exit statuses mostly errors with data not software
+                # TODO: status parsing: core dump, exit on signal or EX_*
+                $RT::Logger->warning( "$RT::SendmailPath exitted with status $?" );
+            }
         };
         if ($@) {
-            $RT::Logger->crit( $msgid . "Could not send mail. -" . $@ );
+            $RT::Logger->crit( $msgid . "Could not send mail: " . $@ );
+            return 0;
         }
     }
     else {
@@ -284,13 +300,12 @@ sub SendMessage {
         }
     }
 
-    my $success =
-      ( $msgid
-      . " sent To: "
-      . $MIMEObj->head->get('To') . " Cc: "
-      . $MIMEObj->head->get('Cc') . " Bcc: "
-      . $MIMEObj->head->get('Bcc') );
-    $success =~ s/\n//gi;
+    my $success = "$msgid sent";
+    foreach (qw(To Cc Bcc)) {
+        next unless my $addresses = $MIMEObj->head->get($_);
+        $success .= " $_: ". $addresses;
+    }
+    $success =~ s/\n//g;
 
     $self->RecordOutgoingMailTransaction($MIMEObj) if ($RT::RecordOutgoingEmail);
 
@@ -323,7 +338,7 @@ sub AddAttachments {
         FIELD => 'TransactionId',
         VALUE => $self->TransactionObj->Id
     );
-    $attachments->OrderBy('id');
+    $attachments->OrderBy( FIELD => 'id');
 
     my $transaction_content_obj = $self->TransactionObj->ContentObj;
 
@@ -445,7 +460,7 @@ sub SetRTSpecialHeaders {
 
       # If there is one, and we can parse it, then base our Message-ID on it
       if ($msgid 
-          and $msgid =~ s/<(rt-.*?-\d+-\d+)\.(\d+-0-0)\@$RT::Organization>$/
+          and $msgid =~ s/<(rt-.*?-\d+-\d+)\.(\d+)-\d+-\d+\@\Q$RT::Organization\E>$/
                          "<$1." . $self->TicketObj->id
                           . "-" . $self->ScripObj->id
                           . "-" . $self->ScripActionObj->{_Message_ID}