You need to sign in or sign up before continuing.
Mailer.pm 6.98 KB
Newer Older
1 2 3
# 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/.
4
#
5 6
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.
7 8 9 10 11 12

package Bugzilla::Mailer;

use strict;

use base qw(Exporter);
13
@Bugzilla::Mailer::EXPORT = qw(MessageToMTA build_thread_marker);
14 15

use Bugzilla::Constants;
16
use Bugzilla::Error;
17
use Bugzilla::Hook;
18 19
use Bugzilla::Util;

20 21
use Date::Format qw(time2str);

22
use Email::Address;
23
use Email::MIME;
24 25 26 27 28 29
# Return::Value 1.666002 pollutes the error log with warnings about this
# deprecated module. We have to set NO_CLUCK = 1 before loading Email::Send
# to disable these warnings.
BEGIN {
    $Return::Value::NO_CLUCK = 1;
}
30
use Email::Send;
31 32

sub MessageToMTA {
33
    my ($msg, $send_now) = (@_);
34 35
    my $method = Bugzilla->params->{'mail_delivery_method'};
    return if $method eq 'None';
36

37 38 39 40 41
    if (Bugzilla->params->{'use_mailer_queue'} and !$send_now) {
        Bugzilla->job_queue->insert('send_mail', { msg => $msg });
        return;
    }

42 43 44 45 46 47 48 49 50 51 52 53 54 55
    my $email;
    if (ref $msg) {
        $email = $msg;
    }
    else {
        # RFC 2822 requires us to have CRLF for our line endings and
        # Email::MIME doesn't do this for us. We use \015 (CR) and \012 (LF)
        # directly because Perl translates "\n" depending on what platform
        # you're running on. See http://perldoc.perl.org/perlport.html#Newlines
        # We check for multiple CRs because of this Template-Toolkit bug:
        # https://rt.cpan.org/Ticket/Display.html?id=43345
        $msg =~ s/(?:\015+)?\012/\015\012/msg;
        $email = new Email::MIME($msg);
    }
56 57 58 59 60 61

    # We add this header to uniquely identify all email that we
    # send as coming from this Bugzilla installation.
    #
    # We don't use correct_urlbase, because we want this URL to
    # *always* be the same for this Bugzilla, in every email,
62
    # even if the admin changes the "ssl_redirect" parameter some day.
63
    $email->header_set('X-Bugzilla-URL', Bugzilla->params->{'urlbase'});
64 65 66 67
    
    # We add this header to mark the mail as "auto-generated" and
    # thus to hopefully avoid auto replies.
    $email->header_set('Auto-Submitted', 'auto-generated');
68

69 70 71
    # MIME-Version must be set otherwise some mailsystems ignore the charset
    $email->header_set('MIME-Version', '1.0') if !$email->header('MIME-Version');

72
    # Encode the headers correctly.
73 74
    foreach my $header ($email->header_names) {
        my @values = $email->header($header);
75
        map { utf8::decode($_) if defined($_) && !utf8::is_utf8($_) } @values;
76

77
        $email->header_str_set($header, @values);
78 79
    }

80 81 82
    my $from = $email->header('From');

    my ($hostname, @args);
83
    my $mailer_class = $method;
84
    if ($method eq "Sendmail") {
85
        $mailer_class = 'Bugzilla::Send::Sendmail';
86 87
        if (ON_WINDOWS) {
            $Email::Send::Sendmail::SENDMAIL = SENDMAIL_EXE;
88
        }
89
        push @args, "-i";
90 91 92 93 94 95 96 97
        # We want to make sure that we pass *only* an email address.
        if ($from) {
            my ($email_obj) = Email::Address->parse($from);
            if ($email_obj) {
                my $from_email = $email_obj->address;
                push(@args, "-f$from_email") if $from_email;
            }
        }
98
    }
99 100 101 102
    else {
        # Sendmail will automatically append our hostname to the From
        # address, but other mailers won't.
        my $urlbase = Bugzilla->params->{'urlbase'};
103
        $urlbase =~ m|//([^:/]+)[:/]?|;
104 105 106
        $hostname = $1;
        $from .= "\@$hostname" if $from !~ /@/;
        $email->header_set('From', $from);
107 108 109
        
        # Sendmail adds a Date: header also, but others may not.
        if (!defined $email->header('Date')) {
110
            $email->header_set('Date', time2str("%a, %d %b %Y %T %z", time()));
111
        }
112 113
    }

114 115
    if ($method eq "SMTP") {
        push @args, Host  => Bugzilla->params->{"smtpserver"},
116 117
                    username => Bugzilla->params->{"smtp_username"},
                    password => Bugzilla->params->{"smtp_password"},
118
                    Hello => $hostname, 
119
                    ssl => Bugzilla->params->{'smtp_ssl'},
120
                    Debug => Bugzilla->params->{'smtp_debug'};
121 122
    }

123 124
    Bugzilla::Hook::process('mailer_before_send', 
                            { email => $email, mailer_args => \@args });
125

Byron Jones's avatar
Byron Jones committed
126 127 128 129
    $email->walk_parts(sub {
        my ($part) = @_;
        return if $part->parts > 1; # Top-level
        my $content_type = $part->content_type || '';
130 131 132 133 134
        $content_type =~ /charset=['"](.+)['"]/;
        # If no charset is defined or is the default us-ascii,
        # then we encode the email to UTF-8 if Bugzilla has utf8 enabled.
        # XXX - This is a hack to workaround bug 723944.
        if (!$1 || $1 eq 'us-ascii') {
Byron Jones's avatar
Byron Jones committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148
            my $body = $part->body;
            if (Bugzilla->params->{'utf8'}) {
                $part->charset_set('UTF-8');
                # encoding_set works only with bytes, not with utf8 strings.
                my $raw = $part->body_raw;
                if (utf8::is_utf8($raw)) {
                    utf8::encode($raw);
                    $part->body_set($raw);
                }
            }
            $part->encoding_set('quoted-printable') if !is_7bit_clean($body);
        }
    });

149 150 151
    if ($method eq "Test") {
        my $filename = bz_locations()->{'datadir'} . '/mailer.testfile';
        open TESTFILE, '>>', $filename;
152 153
        # From - <date> is required to be a valid mbox file.
        print TESTFILE "\n\nFrom - " . $email->header('Date') . "\n" . $email->as_string;
154
        close TESTFILE;
155 156
    }
    else {
157 158
        # This is useful for both Sendmail and Qmail, so we put it out here.
        local $ENV{PATH} = SENDMAIL_PATH;
159
        my $mailer = Email::Send->new({ mailer => $mailer_class, 
160 161 162 163
                                        mailer_args => \@args });
        my $retval = $mailer->send($email);
        ThrowCodeError('mail_send_error', { msg => $retval, mail => $email })
            if !$retval;
164 165 166
    }
}

167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
# Builds header suitable for use as a threading marker in email notifications
sub build_thread_marker {
    my ($bug_id, $user_id, $is_new) = @_;

    if (!defined $user_id) {
        $user_id = Bugzilla->user->id;
    }

    my $sitespec = '@' . Bugzilla->params->{'urlbase'};
    $sitespec =~ s/:\/\//\./; # Make the protocol look like part of the domain
    $sitespec =~ s/^([^:\/]+):(\d+)/$1/; # Remove a port number, to relocate
    if ($2) {
        $sitespec = "-$2$sitespec"; # Put the port number back in, before the '@'
    }

    my $threadingmarker;
    if ($is_new) {
        $threadingmarker = "Message-ID: <bug-$bug_id-$user_id$sitespec>";
    }
    else {
187 188 189
        my $rand_bits = generate_random_password(10);
        $threadingmarker = "Message-ID: <bug-$bug_id-$user_id-$rand_bits$sitespec>" .
                           "\nIn-Reply-To: <bug-$bug_id-$user_id$sitespec>" .
190 191 192 193 194 195
                           "\nReferences: <bug-$bug_id-$user_id$sitespec>";
    }

    return $threadingmarker;
}

196
1;