Mailer.pm 4.65 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
# -*- Mode: perl; indent-tabs-mode: nil -*-
#
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is the Bugzilla Bug Tracking System.
#
# The Initial Developer of the Original Code is Netscape Communications
# Corporation. Portions created by Netscape are
# Copyright (C) 1998 Netscape Communications Corporation. All
# Rights Reserved.
#
# Contributor(s): Terry Weissman <terry@mozilla.org>,
#                 Bryce Nesbitt <bryce-mozilla@nextbus.com>
#                 Dan Mosedale <dmose@mozilla.org>
#                 Alan Raetz <al_raetz@yahoo.com>
#                 Jacob Steenhagen <jake@actex.net>
#                 Matthew Tuck <matty@chariot.net.au>
#                 Bradley Baetz <bbaetz@student.usyd.edu.au>
#                 J. Paul Reed <preed@sigkill.com>
#                 Gervase Markham <gerv@gerv.net>
#                 Byron Jones <bugzilla@glob.com.au>
#                 Frédéric Buclin <LpSolit@gmail.com>
31
#                 Max Kanat-Alexander <mkanat@bugzilla.org>
32 33 34 35 36 37 38 39 40

package Bugzilla::Mailer;

use strict;

use base qw(Exporter);
@Bugzilla::Mailer::EXPORT = qw(MessageToMTA);

use Bugzilla::Constants;
41
use Bugzilla::Error;
42 43
use Bugzilla::Util;

44 45
use Date::Format qw(time2str);

46
use Encode qw(encode);
47
use Email::Address;
48 49 50 51
use Email::MIME;
# Loading this gives us encoding_set.
use Email::MIME::Modifier;
use Email::Send;
52 53 54

sub MessageToMTA {
    my ($msg) = (@_);
55 56
    my $method = Bugzilla->params->{'mail_delivery_method'};
    return if $method eq 'None';
57

58 59 60 61
    my $email = ref($msg) ? $msg : Email::MIME->new($msg);
    foreach my $part ($email->parts) {
        $part->charset_set('UTF-8') if Bugzilla->params->{'utf8'};
        $part->encoding_set('quoted-printable') if !is_7bit_clean($part->body);
62 63
    }

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

67 68 69
    # Encode the headers correctly in quoted-printable
    foreach my $header qw(From To Cc Reply-To Sender Errors-To Subject) {
        if (my $value = $email->header($header)) {
70
            $value = Encode::decode("UTF-8", $value) if Bugzilla->params->{'utf8'};
71 72
            my $encoded = encode('MIME-Q', $value);
            $email->header_set($header, $encoded);
73 74 75
        }
    }

76 77 78 79 80 81
    my $from = $email->header('From');

    my ($hostname, @args);
    if ($method eq "Sendmail") {
        if (ON_WINDOWS) {
            $Email::Send::Sendmail::SENDMAIL = SENDMAIL_EXE;
82
        }
83
        push @args, "-i";
84 85 86 87 88 89 90 91
        # 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;
            }
        }
92 93
        push(@args, "-ODeliveryMode=deferred")
            if !Bugzilla->params->{"sendmailnow"};
94
    }
95 96 97 98
    else {
        # Sendmail will automatically append our hostname to the From
        # address, but other mailers won't.
        my $urlbase = Bugzilla->params->{'urlbase'};
99
        $urlbase =~ m|//([^:/]+)[:/]?|;
100 101 102
        $hostname = $1;
        $from .= "\@$hostname" if $from !~ /@/;
        $email->header_set('From', $from);
103 104 105 106 107
        
        # Sendmail adds a Date: header also, but others may not.
        if (!defined $email->header('Date')) {
            $email->header_set('Date', time2str("%a, %e %b %Y %T %z", time()));
        }
108 109
    }

110 111
    if ($method eq "SMTP") {
        push @args, Host  => Bugzilla->params->{"smtpserver"},
112 113
                    Hello => $hostname, 
                    Debug => Bugzilla->params->{'smtp_debug'};
114 115
    }

116 117 118 119 120
    if ($method eq "Test") {
        my $filename = bz_locations()->{'datadir'} . '/mailer.testfile';
        open TESTFILE, '>>', $filename;
        print TESTFILE "\n\n---\n\n" . $email->as_string;
        close TESTFILE;
121 122
    }
    else {
123 124 125 126 127 128 129
        # This is useful for both Sendmail and Qmail, so we put it out here.
        local $ENV{PATH} = SENDMAIL_PATH;
        my $mailer = Email::Send->new({ mailer => $method, 
                                        mailer_args => \@args });
        my $retval = $mailer->send($email);
        ThrowCodeError('mail_send_error', { msg => $retval, mail => $email })
            if !$retval;
130 131 132 133
    }
}

1;