~ubuntu-branches/debian/sid/bugzilla/sid

« back to all changes in this revision

Viewing changes to Bugzilla/Mailer.pm

  • Committer: Bazaar Package Importer
  • Author(s): Raphael Bossek
  • Date: 2008-06-27 22:34:34 UTC
  • mfrom: (1.1.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20080627223434-0ib57vstn43bb4a3
Tags: 3.0.4.1-1
* Update of French, Russian and German translations. (closes: #488251)
* Added Bulgarian and Belarusian translations.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- Mode: perl; indent-tabs-mode: nil -*-
2
 
#
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/
7
 
#
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.
12
 
#
13
 
# The Original Code is the Bugzilla Bug Tracking System.
14
 
#
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
18
 
# Rights Reserved.
19
 
#
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>
32
 
 
33
 
package Bugzilla::Mailer;
34
 
 
35
 
use strict;
36
 
 
37
 
use base qw(Exporter);
38
 
@Bugzilla::Mailer::EXPORT = qw(MessageToMTA);
39
 
 
40
 
use Bugzilla::Constants;
41
 
use Bugzilla::Error;
42
 
use Bugzilla::Util;
43
 
 
44
 
use Date::Format qw(time2str);
45
 
 
46
 
use Encode qw(encode);
47
 
use Encode::MIME::Header;
48
 
use Email::Address;
49
 
use Email::MIME;
50
 
# Loading this gives us encoding_set.
51
 
use Email::MIME::Modifier;
52
 
use Email::Send;
53
 
 
54
 
sub MessageToMTA {
55
 
    my ($msg) = (@_);
56
 
    my $method = Bugzilla->params->{'mail_delivery_method'};
57
 
    return if $method eq 'None';
58
 
 
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);
63
 
    }
64
 
 
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');
67
 
 
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'};
72
 
 
73
 
            # avoid excessive line wrapping done by Encode.
74
 
            local $Encode::Encoding{'MIME-Q'}->{'bpl'} = 998;
75
 
 
76
 
            my $encoded = encode('MIME-Q', $value);
77
 
            $email->header_set($header, $encoded);
78
 
        }
79
 
    }
80
 
 
81
 
    my $from = $email->header('From');
82
 
 
83
 
    my ($hostname, @args);
84
 
    if ($method eq "Sendmail") {
85
 
        if (ON_WINDOWS) {
86
 
            $Email::Send::Sendmail::SENDMAIL = SENDMAIL_EXE;
87
 
        }
88
 
        push @args, "-i";
89
 
        # We want to make sure that we pass *only* an email address.
90
 
        if ($from) {
91
 
            my ($email_obj) = Email::Address->parse($from);
92
 
            if ($email_obj) {
93
 
                my $from_email = $email_obj->address;
94
 
                push(@args, "-f$from_email") if $from_email;
95
 
            }
96
 
        }
97
 
        push(@args, "-ODeliveryMode=deferred")
98
 
            if !Bugzilla->params->{"sendmailnow"};
99
 
    }
100
 
    else {
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|//([^:/]+)[:/]?|;
105
 
        $hostname = $1;
106
 
        $from .= "\@$hostname" if $from !~ /@/;
107
 
        $email->header_set('From', $from);
108
 
        
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()));
112
 
        }
113
 
    }
114
 
 
115
 
    if ($method eq "SMTP") {
116
 
        push @args, Host  => Bugzilla->params->{"smtpserver"},
117
 
                    Hello => $hostname, 
118
 
                    Debug => Bugzilla->params->{'smtp_debug'};
119
 
    }
120
 
 
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;
126
 
        close TESTFILE;
127
 
    }
128
 
    else {
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 })
135
 
            if !$retval;
136
 
    }
137
 
}
138
 
 
139
 
1;