1
# -*- Mode: perl; indent-tabs-mode: nil -*-
3
# The contents of this file are subject to the Mozilla Public
4
# License Version 1.1 (the "License"); you may not use this file
5
# except in compliance with the License. You may obtain a copy of
6
# the License at http://www.mozilla.org/MPL/
8
# Software distributed under the License is distributed on an "AS
9
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
10
# implied. See the License for the specific language governing
11
# rights and limitations under the License.
13
# The Original Code is the Bugzilla Bug Tracking System.
15
# The Initial Developer of the Original Code is Netscape Communications
16
# Corporation. Portions created by Netscape are
17
# Copyright (C) 1998 Netscape Communications Corporation. All
20
# Contributor(s): Terry Weissman <terry@mozilla.org>,
21
# Bryce Nesbitt <bryce-mozilla@nextbus.com>
22
# Dan Mosedale <dmose@mozilla.org>
23
# Alan Raetz <al_raetz@yahoo.com>
24
# Jacob Steenhagen <jake@actex.net>
25
# Matthew Tuck <matty@chariot.net.au>
26
# Bradley Baetz <bbaetz@student.usyd.edu.au>
27
# J. Paul Reed <preed@sigkill.com>
28
# Gervase Markham <gerv@gerv.net>
29
# Byron Jones <bugzilla@glob.com.au>
30
# Frédéric Buclin <LpSolit@gmail.com>
31
# Max Kanat-Alexander <mkanat@bugzilla.org>
33
package Bugzilla::Mailer;
37
use base qw(Exporter);
38
@Bugzilla::Mailer::EXPORT = qw(MessageToMTA);
40
use Bugzilla::Constants;
44
use Date::Format qw(time2str);
46
use Encode qw(encode);
47
use Encode::MIME::Header;
50
# Loading this gives us encoding_set.
51
use Email::MIME::Modifier;
56
my $method = Bugzilla->params->{'mail_delivery_method'};
57
return if $method eq 'None';
59
my $email = ref($msg) ? $msg : Email::MIME->new($msg);
60
foreach my $part ($email->parts) {
61
$part->charset_set('UTF-8') if Bugzilla->params->{'utf8'};
62
$part->encoding_set('quoted-printable') if !is_7bit_clean($part->body);
65
# MIME-Version must be set otherwise some mailsystems ignore the charset
66
$email->header_set('MIME-Version', '1.0') if !$email->header('MIME-Version');
68
# Encode the headers correctly in quoted-printable
69
foreach my $header qw(From To Cc Reply-To Sender Errors-To Subject) {
70
if (my $value = $email->header($header)) {
71
$value = Encode::decode("UTF-8", $value) if Bugzilla->params->{'utf8'};
73
# avoid excessive line wrapping done by Encode.
74
local $Encode::Encoding{'MIME-Q'}->{'bpl'} = 998;
76
my $encoded = encode('MIME-Q', $value);
77
$email->header_set($header, $encoded);
81
my $from = $email->header('From');
83
my ($hostname, @args);
84
if ($method eq "Sendmail") {
86
$Email::Send::Sendmail::SENDMAIL = SENDMAIL_EXE;
89
# We want to make sure that we pass *only* an email address.
91
my ($email_obj) = Email::Address->parse($from);
93
my $from_email = $email_obj->address;
94
push(@args, "-f$from_email") if $from_email;
97
push(@args, "-ODeliveryMode=deferred")
98
if !Bugzilla->params->{"sendmailnow"};
101
# Sendmail will automatically append our hostname to the From
102
# address, but other mailers won't.
103
my $urlbase = Bugzilla->params->{'urlbase'};
104
$urlbase =~ m|//([^:/]+)[:/]?|;
106
$from .= "\@$hostname" if $from !~ /@/;
107
$email->header_set('From', $from);
109
# Sendmail adds a Date: header also, but others may not.
110
if (!defined $email->header('Date')) {
111
$email->header_set('Date', time2str("%a, %e %b %Y %T %z", time()));
115
if ($method eq "SMTP") {
116
push @args, Host => Bugzilla->params->{"smtpserver"},
118
Debug => Bugzilla->params->{'smtp_debug'};
121
if ($method eq "Test") {
122
my $filename = bz_locations()->{'datadir'} . '/mailer.testfile';
123
open TESTFILE, '>>', $filename;
124
# From - <date> is required to be a valid mbox file.
125
print TESTFILE "\n\nFrom - " . $email->header('Date') . "\n" . $email->as_string;
129
# This is useful for both Sendmail and Qmail, so we put it out here.
130
local $ENV{PATH} = SENDMAIL_PATH;
131
my $mailer = Email::Send->new({ mailer => $method,
132
mailer_args => \@args });
133
my $retval = $mailer->send($email);
134
ThrowCodeError('mail_send_error', { msg => $retval, mail => $email })