| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package CAM::EmailTemplate::SMTP; |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME |
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
CAM::EmailTemplate::SMTP - Net::SMTP based email message sender |
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 LICENSE |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
Copyright 2005 Clotho Advanced Media, Inc., |
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify it |
|
12
|
|
|
|
|
|
|
under the same terms as Perl itself. |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
use CAM::EmailTemplate::SMTP; |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
CAM::EmailTemplate::SMTP->setHost("mail.foo.com"); |
|
19
|
|
|
|
|
|
|
my $template = new CAM::EmailTemplate::SMTP($filename); |
|
20
|
|
|
|
|
|
|
$template->setParams(recipient => 'user@foo.com', |
|
21
|
|
|
|
|
|
|
bar => "baz", kelp => "green"); |
|
22
|
|
|
|
|
|
|
if ($template->send()) { |
|
23
|
|
|
|
|
|
|
print "Sent."; |
|
24
|
|
|
|
|
|
|
} else { |
|
25
|
|
|
|
|
|
|
print "Doh! " . $template->{sendError}; |
|
26
|
|
|
|
|
|
|
} |
|
27
|
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
29
|
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
This package is exactly like CAM::EmailTemplate except that it uses |
|
31
|
|
|
|
|
|
|
the Perl Net::SMTP package to deliver mail instead of a local sendmail |
|
32
|
|
|
|
|
|
|
executable. |
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
To accomplish this, the programmer must configure the mailhost before |
|
35
|
|
|
|
|
|
|
attempting to send. |
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
See README for a comparison with other CPAN modules. |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
=cut |
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
require 5.005_62; |
|
42
|
1
|
|
|
1
|
|
33972
|
use strict; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
38
|
|
|
43
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
29
|
|
|
44
|
1
|
|
|
1
|
|
909
|
use CAM::EmailTemplate; |
|
|
1
|
|
|
|
|
7255
|
|
|
|
1
|
|
|
|
|
31
|
|
|
45
|
1
|
|
|
1
|
|
1080
|
use Net::SMTP; |
|
|
1
|
|
|
|
|
51687
|
|
|
|
1
|
|
|
|
|
764
|
|
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
our @ISA = qw(CAM::EmailTemplate); |
|
48
|
|
|
|
|
|
|
our $VERSION = '0.91'; |
|
49
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
# Package globals |
|
51
|
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
my $global_mailhost = undef; |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=head1 FUNCTIONS |
|
56
|
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=over 4 |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=cut |
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=item setHost HOST |
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
Create a new template object. The parameters are the same as the |
|
65
|
|
|
|
|
|
|
CAM::Template constructor. |
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
This can be called as a class method or an instance method. If used |
|
68
|
|
|
|
|
|
|
as a class method, all subsequent instances use the specified host. |
|
69
|
|
|
|
|
|
|
If used as an instance method, the host only applies to this one |
|
70
|
|
|
|
|
|
|
instance. |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=cut |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub setHost |
|
75
|
|
|
|
|
|
|
{ |
|
76
|
0
|
|
|
0
|
1
|
|
my $pkg_or_self = shift; |
|
77
|
0
|
|
|
|
|
|
my $mailhost = shift; |
|
78
|
|
|
|
|
|
|
|
|
79
|
0
|
0
|
|
|
|
|
if (ref($pkg_or_self)) |
|
80
|
|
|
|
|
|
|
{ |
|
81
|
0
|
|
|
|
|
|
my $self = $pkg_or_self; |
|
82
|
0
|
|
|
|
|
|
$self->{mailhost} = $mailhost |
|
83
|
|
|
|
|
|
|
} |
|
84
|
|
|
|
|
|
|
else |
|
85
|
|
|
|
|
|
|
{ |
|
86
|
0
|
|
|
|
|
|
$global_mailhost = $mailhost |
|
87
|
|
|
|
|
|
|
} |
|
88
|
0
|
|
|
|
|
|
return $pkg_or_self; |
|
89
|
|
|
|
|
|
|
} |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=item deliver MSG |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
Delivers the message. This function assumes that the message is |
|
95
|
|
|
|
|
|
|
properly formatted. |
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
This method overrides the deliver() method in CAM::EmailTemplate, |
|
98
|
|
|
|
|
|
|
implementing the Net::SMTP functionality. |
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=cut |
|
101
|
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub deliver |
|
103
|
|
|
|
|
|
|
{ |
|
104
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
105
|
0
|
|
|
|
|
|
my $content = shift; |
|
106
|
|
|
|
|
|
|
|
|
107
|
0
|
|
|
|
|
|
my $error = undef; |
|
108
|
0
|
|
0
|
|
|
|
my $mailhost = $self->{mailhost} || $global_mailhost; |
|
109
|
0
|
0
|
|
|
|
|
if (!$mailhost) |
|
110
|
|
|
|
|
|
|
{ |
|
111
|
0
|
|
|
|
|
|
$error = "No mail host specified"; |
|
112
|
|
|
|
|
|
|
} |
|
113
|
|
|
|
|
|
|
else |
|
114
|
|
|
|
|
|
|
{ |
|
115
|
0
|
|
0
|
|
|
|
my $smtp = Net::SMTP->new($mailhost, |
|
116
|
|
|
|
|
|
|
Debug => ($ENV{SMTPTemplate_Debug} || 0)); |
|
117
|
0
|
0
|
|
|
|
|
if (!$smtp) |
|
118
|
|
|
|
|
|
|
{ |
|
119
|
0
|
|
|
|
|
|
$error = "Failed to connect to the mail server"; |
|
120
|
|
|
|
|
|
|
} |
|
121
|
|
|
|
|
|
|
else |
|
122
|
|
|
|
|
|
|
{ |
|
123
|
0
|
|
|
|
|
|
my $header = $content; |
|
124
|
0
|
|
|
|
|
|
$header =~ s/\n\n.*/\n/s; |
|
125
|
|
|
|
|
|
|
|
|
126
|
0
|
|
|
|
|
|
my $headerlength = length($header); |
|
127
|
|
|
|
|
|
|
|
|
128
|
0
|
|
|
|
|
|
my %fields = (); |
|
129
|
0
|
|
|
|
|
|
while ($header) |
|
130
|
|
|
|
|
|
|
{ |
|
131
|
0
|
0
|
|
|
|
|
if ($header =~ s/^([^:\n]+):[ \t]*([^\n]*)\n//) |
|
132
|
|
|
|
|
|
|
{ |
|
133
|
0
|
|
|
|
|
|
my $fieldname = $1; |
|
134
|
0
|
|
|
|
|
|
my $value = $2; |
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
# Special case: Clean up address lines |
|
137
|
0
|
0
|
|
|
|
|
if ($fieldname =~ /^To|From|Cc|Bcc$/) |
|
138
|
|
|
|
|
|
|
{ |
|
139
|
0
|
|
|
|
|
|
foreach my $email (split /\s*,\s*/, $value) |
|
140
|
|
|
|
|
|
|
{ |
|
141
|
0
|
|
|
|
|
|
$email =~ s/^[^<]*<([^>]*)>.*$/$1/s; |
|
142
|
0
|
|
|
|
|
|
push @{$fields{$fieldname}}, $email; |
|
|
0
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
} |
|
144
|
|
|
|
|
|
|
} |
|
145
|
|
|
|
|
|
|
else |
|
146
|
|
|
|
|
|
|
{ |
|
147
|
0
|
|
|
|
|
|
push @{$fields{$fieldname}}, $value; |
|
|
0
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
} |
|
149
|
|
|
|
|
|
|
} |
|
150
|
|
|
|
|
|
|
else |
|
151
|
|
|
|
|
|
|
{ |
|
152
|
0
|
|
|
|
|
|
my $line = substr($header, 0, 40) . "..."; |
|
153
|
0
|
|
|
|
|
|
$line =~ s/\n.*//s; |
|
154
|
0
|
|
|
|
|
|
$error = "Problem parsing header: $line"; |
|
155
|
0
|
|
|
|
|
|
last; |
|
156
|
|
|
|
|
|
|
} |
|
157
|
|
|
|
|
|
|
} |
|
158
|
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
# Remove BCCs |
|
160
|
0
|
|
|
|
|
|
substr($content, 0, $headerlength) =~ s/^Bcc: .*$//gm; |
|
161
|
|
|
|
|
|
|
# Add in the mailer agent field to the header |
|
162
|
0
|
|
|
|
|
|
$content =~ s/\n\n/\nX-Mailer: CAM::EmailTemplate::SMTP[v$VERSION] Net::SMTP[v$Net::SMTP::VERSION]\n\n/s; |
|
163
|
|
|
|
|
|
|
|
|
164
|
0
|
0
|
|
|
|
|
if (!$error) |
|
165
|
|
|
|
|
|
|
{ |
|
166
|
0
|
0
|
|
|
|
|
if (!$smtp->mail($fields{From}->[0])) |
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
167
|
0
|
|
|
|
|
|
{ |
|
168
|
0
|
|
|
|
|
|
$error = "Failed to send the 'From:' field, aborting"; |
|
169
|
0
|
|
|
|
|
|
$smtp->reset(); |
|
170
|
|
|
|
|
|
|
} |
|
171
|
|
|
|
|
|
|
elsif (!$smtp->to(@{$fields{To}})) |
|
172
|
|
|
|
|
|
|
{ |
|
173
|
0
|
|
|
|
|
|
$error = "Failed to send to '@{$fields{To}}', aborting"; |
|
|
0
|
|
|
|
|
|
|
|
174
|
0
|
|
|
|
|
|
$smtp->reset(); |
|
175
|
|
|
|
|
|
|
} |
|
176
|
|
|
|
|
|
|
elsif(!$smtp->data($content)) |
|
177
|
|
|
|
|
|
|
{ |
|
178
|
0
|
|
|
|
|
|
$error = "Failed to send message, aborting"; |
|
179
|
0
|
|
|
|
|
|
$smtp->reset(); |
|
180
|
|
|
|
|
|
|
} |
|
181
|
|
|
|
|
|
|
|
|
182
|
0
|
0
|
|
|
|
|
if (!$smtp->quit()) |
|
183
|
|
|
|
|
|
|
{ |
|
184
|
0
|
|
|
|
|
|
$error = "The mail agent did not complete the message delivery"; |
|
185
|
|
|
|
|
|
|
} |
|
186
|
|
|
|
|
|
|
} |
|
187
|
|
|
|
|
|
|
} |
|
188
|
|
|
|
|
|
|
} |
|
189
|
0
|
0
|
|
|
|
|
return $error ? (undef, $error) : ($self, undef); |
|
190
|
|
|
|
|
|
|
} |
|
191
|
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
1; |
|
193
|
|
|
|
|
|
|
__END__ |