| 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., <cpan@clotho.com> | ||||||||||||||||||||
| 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 | 1 | require 5.005_62; | |||||||||||||||||||
| 42 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 43 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 44 | 1 | ▶ | use CAM::EmailTemplate; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 45 | 1 | ▶ | use Net::SMTP; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | 1 | our @ISA = qw(CAM::EmailTemplate); | |||||||||||||||||||
| 48 | 1 | our $VERSION = '0.91'; | |||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | # Package globals | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 1 | 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 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
setHost: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 76 | 0 | my $pkg_or_self = shift; | |||||||||||||||||||
| 77 | 0 | my $mailhost = shift; | |||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | 0 | ▶ | if (ref($pkg_or_self)) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
deliver: not called CC=15 SCAR=54.8
Pod
documented
| |||||||||||||||||||||
| 104 | 0 | my $self = shift; | |||||||||||||||||||
| 105 | 0 | my $content = shift; | |||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | 0 | my $error = undef; | |||||||||||||||||||
| 108 | 0 | ▶ | my $mailhost = $self->{mailhost} || $global_mailhost; | ||||||||||||||||||
|
Condition0%0/3
$self->{'mailhost'} || $global_mailhost
MC/DC0%0/2
$self->{'mailhost'} || $global_mailhost
$self->{'mailhost'}
$global_mailhost
Truth table0%0/3
A || B
A= $self->{'mailhost'}B= $global_mailhost
| |||||||||||||||||||||
| 109 | 0 | ▶ | if (!$mailhost) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 110 | { | ||||||||||||||||||||
| 111 | 0 | $error = "No mail host specified"; | |||||||||||||||||||
| 112 | } | ||||||||||||||||||||
| 113 | else | ||||||||||||||||||||
| 114 | { | ||||||||||||||||||||
| 115 | my $smtp = Net::SMTP->new($mailhost, | ||||||||||||||||||||
| 116 | 0 | ▶ | Debug => ($ENV{SMTPTemplate_Debug} || 0)); | ||||||||||||||||||
|
Condition0%0/2
$ENV{'SMTPTemplate_Debug'} || 0
MC/DC0%0/1
$ENV{'SMTPTemplate_Debug'} || 0
$ENV{'SMTPTemplate_Debug'}
Truth table0%0/2
A
A= $ENV{'SMTPTemplate_Debug'}
| |||||||||||||||||||||
| 117 | 0 | ▶ | if (!$smtp) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 130 | { | ||||||||||||||||||||
| 131 | 0 | ▶ | if ($header =~ s/^([^:\n]+):[ \t]*([^\n]*)\n//) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 132 | { | ||||||||||||||||||||
| 133 | 0 | my $fieldname = $1; | |||||||||||||||||||
| 134 | 0 | my $value = $2; | |||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | # Special case: Clean up address lines | ||||||||||||||||||||
| 137 | 0 | ▶ | if ($fieldname =~ /^To|From|Cc|Bcc$/) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 138 | { | ||||||||||||||||||||
| 139 | 0 | foreach my $email (split /\s*,\s*/, $value) | |||||||||||||||||||
| 140 | { | ||||||||||||||||||||
| 141 | 0 | $email =~ s/^[^<]*<([^>]*)>.*$/$1/s; | |||||||||||||||||||
| 142 | 0 | push @{$fields{$fieldname}}, $email; | |||||||||||||||||||
| 143 | } | ||||||||||||||||||||
| 144 | } | ||||||||||||||||||||
| 145 | else | ||||||||||||||||||||
| 146 | { | ||||||||||||||||||||
| 147 | 0 | push @{$fields{$fieldname}}, $value; | |||||||||||||||||||
| 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 | ▶ | if (!$error) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 165 | { | ||||||||||||||||||||
| 166 | 0 | ▶ | if (!$smtp->mail($fields{From}->[0])) | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 167 | { | ||||||||||||||||||||
| 168 | 0 | $error = "Failed to send the 'From:' field, aborting"; | |||||||||||||||||||
| 169 | 0 | $smtp->reset(); | |||||||||||||||||||
| 170 | } | ||||||||||||||||||||
| 171 | 0 | elsif (!$smtp->to(@{$fields{To}})) | |||||||||||||||||||
| 172 | { | ||||||||||||||||||||
| 173 | 0 | $error = "Failed to send to '@{$fields{To}}', aborting"; | |||||||||||||||||||
| 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 | ▶ | if (!$smtp->quit()) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 183 | { | ||||||||||||||||||||
| 184 | 0 | $error = "The mail agent did not complete the message delivery"; | |||||||||||||||||||
| 185 | } | ||||||||||||||||||||
| 186 | } | ||||||||||||||||||||
| 187 | } | ||||||||||||||||||||
| 188 | } | ||||||||||||||||||||
| 189 | 0 | ▶ | return $error ? (undef, $error) : ($self, undef); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 190 | } | ||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | 1 | 1; | |||||||||||||||||||
| 193 | __END__ | ||||||||||||||||||||