From 0ff4174b5ea7f839562f5109defab1c2ac92e735 Mon Sep 17 00:00:00 2001 From: Frédéric Buclin Date: Wed, 1 Oct 2014 12:15:23 +0200 Subject: Bug 1070640: Update (and rename) Bugzilla::Send::Sendmail to work with Email::Sender::Transport::Sendmail r=dylan a=justdave --- Bugzilla/Mailer.pm | 6 +- Bugzilla/Send/Sendmail.pm | 104 ---------------------------------- Bugzilla/Sender/Transport/Sendmail.pm | 95 +++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 107 deletions(-) delete mode 100644 Bugzilla/Send/Sendmail.pm create mode 100644 Bugzilla/Sender/Transport/Sendmail.pm diff --git a/Bugzilla/Mailer.pm b/Bugzilla/Mailer.pm index 5c0d85d33..3c8815306 100644 --- a/Bugzilla/Mailer.pm +++ b/Bugzilla/Mailer.pm @@ -26,7 +26,7 @@ use Encode::MIME::Header; use Email::MIME; use Email::Sender::Simple qw(sendmail); use Email::Sender::Transport::SMTP; -use Email::Sender::Transport::Sendmail; +use Bugzilla::Sender::Transport::Sendmail; sub MessageToMTA { my ($msg, $send_now) = (@_); @@ -105,10 +105,10 @@ sub MessageToMTA { my $transport; if ($method eq "Sendmail") { if (ON_WINDOWS) { - $transport = Email::Sender::Transport::Sendmail->new({ sendmail => SENDMAIL_EXE }); + $transport = Bugzilla::Sender::Transport::Sendmail->new({ sendmail => SENDMAIL_EXE }); } else { - $transport = Email::Sender::Transport::Sendmail->new(); + $transport = Bugzilla::Sender::Transport::Sendmail->new(); } } else { diff --git a/Bugzilla/Send/Sendmail.pm b/Bugzilla/Send/Sendmail.pm deleted file mode 100644 index 48312b2fa..000000000 --- a/Bugzilla/Send/Sendmail.pm +++ /dev/null @@ -1,104 +0,0 @@ -# This Source Code Form is subject to the terms of the Mozilla Public -# License, v. 2.0. If a copy of the MPL was not distributed with this -# file, You can obtain one at http://mozilla.org/MPL/2.0/. -# -# This Source Code Form is "Incompatible With Secondary Licenses", as -# defined by the Mozilla Public License, v. 2.0. - -package Bugzilla::Send::Sendmail; - -use 5.10.1; -use strict; -use warnings; - -use parent qw(Email::Send::Sendmail); - -use Return::Value; -use Symbol qw(gensym); - -sub send { - my ($class, $message, @args) = @_; - my $mailer = $class->_find_sendmail; - - return failure "Couldn't find 'sendmail' executable in your PATH" - ." and Email::Send::Sendmail::SENDMAIL is not set" - unless $mailer; - - return failure "Found $mailer but cannot execute it" - unless -x $mailer; - - local $SIG{'CHLD'} = 'DEFAULT'; - - my $pipe = gensym; - - open($pipe, "| $mailer -t -oi @args") - || return failure "Error executing $mailer: $!"; - print($pipe $message->as_string) - || return failure "Error printing via pipe to $mailer: $!"; - unless (close $pipe) { - return failure "error when closing pipe to $mailer: $!" if $!; - my ($error_message, $is_transient) = _map_exitcode($? >> 8); - if (Bugzilla->params->{'use_mailer_queue'}) { - # Return success for errors which are fatal so Bugzilla knows to - # remove them from the queue - if ($is_transient) { - return failure "error when closing pipe to $mailer: $error_message"; - } else { - warn "error when closing pipe to $mailer: $error_message\n"; - return success; - } - } else { - return failure "error when closing pipe to $mailer: $error_message"; - } - } - return success; -} - -sub _map_exitcode { - # Returns (error message, is_transient) - # from the sendmail source (sendmail/sysexit.h) - my $code = shift; - if ($code == 64) { - return ("Command line usage error (EX_USAGE)", 1); - } elsif ($code == 65) { - return ("Data format error (EX_DATAERR)", 1); - } elsif ($code == 66) { - return ("Cannot open input (EX_NOINPUT)", 1); - } elsif ($code == 67) { - return ("Addressee unknown (EX_NOUSER)", 0); - } elsif ($code == 68) { - return ("Host name unknown (EX_NOHOST)", 0); - } elsif ($code == 69) { - return ("Service unavailable (EX_UNAVAILABLE)", 1); - } elsif ($code == 70) { - return ("Internal software error (EX_SOFTWARE)", 1); - } elsif ($code == 71) { - return ("System error (EX_OSERR)", 1); - } elsif ($code == 72) { - return ("Critical OS file missing (EX_OSFILE)", 1); - } elsif ($code == 73) { - return ("Can't create output file (EX_CANTCREAT)", 1); - } elsif ($code == 74) { - return ("Input/output error (EX_IOERR)", 1); - } elsif ($code == 75) { - return ("Temp failure (EX_TEMPFAIL)", 1); - } elsif ($code == 76) { - return ("Remote error in protocol (EX_PROTOCOL)", 1); - } elsif ($code == 77) { - return ("Permission denied (EX_NOPERM)", 1); - } elsif ($code == 78) { - return ("Configuration error (EX_CONFIG)", 1); - } else { - return ("Unknown Error ($code)", 1); - } -} - -1; - -=head1 B - -=over - -=item send - -=back diff --git a/Bugzilla/Sender/Transport/Sendmail.pm b/Bugzilla/Sender/Transport/Sendmail.pm new file mode 100644 index 000000000..49f00777f --- /dev/null +++ b/Bugzilla/Sender/Transport/Sendmail.pm @@ -0,0 +1,95 @@ +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. +# +# This Source Code Form is "Incompatible With Secondary Licenses", as +# defined by the Mozilla Public License, v. 2.0. + +package Bugzilla::Sender::Transport::Sendmail; + +use 5.10.1; +use strict; +use warnings; + +use parent qw(Email::Sender::Transport::Sendmail); + +use Email::Sender::Failure; + +sub send_email { + my ($self, $email, $envelope) = @_; + + my $pipe = $self->_sendmail_pipe($envelope); + + my $string = $email->as_string; + $string =~ s/\x0D\x0A/\x0A/g unless $^O eq 'MSWin32'; + + print $pipe $string + or Email::Sender::Failure->throw("couldn't send message to sendmail: $!"); + + unless (close $pipe) { + Email::Sender::Failure->throw("error when closing pipe to sendmail: $!") if $!; + my ($error_message, $is_transient) = _map_exitcode($? >> 8); + if (Bugzilla->params->{'use_mailer_queue'}) { + # Return success for errors which are fatal so Bugzilla knows to + # remove them from the queue. + if ($is_transient) { + Email::Sender::Failure->throw("error when closing pipe to sendmail: $error_message"); + } else { + warn "error when closing pipe to sendmail: $error_message\n"; + return $self->success; + } + } else { + Email::Sender::Failure->throw("error when closing pipe to sendmail: $error_message"); + } + } + return $self->success; +} + +sub _map_exitcode { + # Returns (error message, is_transient) + # from the sendmail source (sendmail/sysexits.h) + my $code = shift; + if ($code == 64) { + return ("Command line usage error (EX_USAGE)", 1); + } elsif ($code == 65) { + return ("Data format error (EX_DATAERR)", 1); + } elsif ($code == 66) { + return ("Cannot open input (EX_NOINPUT)", 1); + } elsif ($code == 67) { + return ("Addressee unknown (EX_NOUSER)", 0); + } elsif ($code == 68) { + return ("Host name unknown (EX_NOHOST)", 0); + } elsif ($code == 69) { + return ("Service unavailable (EX_UNAVAILABLE)", 1); + } elsif ($code == 70) { + return ("Internal software error (EX_SOFTWARE)", 1); + } elsif ($code == 71) { + return ("System error (EX_OSERR)", 1); + } elsif ($code == 72) { + return ("Critical OS file missing (EX_OSFILE)", 1); + } elsif ($code == 73) { + return ("Can't create output file (EX_CANTCREAT)", 1); + } elsif ($code == 74) { + return ("Input/output error (EX_IOERR)", 1); + } elsif ($code == 75) { + return ("Temp failure (EX_TEMPFAIL)", 1); + } elsif ($code == 76) { + return ("Remote error in protocol (EX_PROTOCOL)", 1); + } elsif ($code == 77) { + return ("Permission denied (EX_NOPERM)", 1); + } elsif ($code == 78) { + return ("Configuration error (EX_CONFIG)", 1); + } else { + return ("Unknown Error ($code)", 1); + } +} + +1; + +=head1 B + +=over + +=item send_email + +=back -- cgit v1.2.3-24-g4f1b