Labyrinth/Mailer.pm

filter: Statementstmt 30.2% 49 / 162 Branchbran 0.0% 0 / 48 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 70.0% 14 / 20 Podpod 100% 5 / 5 totaltotal 28.9% 68 / 235 CC 29 SCAR 44.3
CC
29
Cov
23%
MailSend
182.0
HTMLSend
132.0
HTMLSendX
30.0
CRAP 83.8
« Labyrinth/DBUtils.pm ↑ Summary Labyrinth/Support.pm »
1 package Labyrinth::Mailer;
2  
3 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 12 ▶use vars qw($VERSION @ISA %EXPORT_TAGS @EXPORT @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 $VERSION = '5.32';
9  
10 =head1 NAME
11  
12 Labyrinth::Mailer - Mail Manager for Labyrinth
13  
14 =head1 SYNOPSIS
15  
16 use Labyrinth::Mailer;
17  
18 MailSend($template,%hash);
19  
20 =head1 DESCRIPTION
21  
22 The Mailer package contains generic functions used for sending mail messages.
23  
24 =head1 EXPORT
25  
26 MailSend
27  
28 =cut
29  
30 # -------------------------------------
31 # Export Details
32  
33 12 require Exporter;
34 12 @ISA = qw(Exporter);
35 12 @EXPORT = ( qw( MailSet MailSend MailSent HTMLSend ) );
36  
37 # -------------------------------------
38 # Library Modules
39  
40 12 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 12 ▶use HTML::Entities;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 12 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 12 ▶use MIME::Lite;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 12 ▶use MIME::Lite::TT::HTML;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 12 ▶use MIME::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 12 ▶use Text::Wrap;
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 12 ▶use Labyrinth::Audit;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 12 ▶use Labyrinth::Writer;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 12 ▶use Labyrinth::Variables;
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 # -------------------------------------
53 # Variables
54  
55 12 my $mtypes = MIME::Types->new;
56  
57 12 my %mailer;
58  
59 # -------------------------------------
60 # The Subs
61  
62 =head1 FUNCTIONS
63  
64 =over 4
65  
66 =item MailSet(%hash)
67  
68 =item MailSend(%hash)
69  
70 Hash table entries should contain TT variables used by the template. An email
71 address and template to use must be included.
72  
73 =item MailSent
74  
75 =item HTMLSend
76  
77 =item HTMLSendX
78  
79 =back
80  
81 =cut
82  
83 0 ▶sub MailSet {
Subroutine MailSet: not called CC=3 SCAR=24.8
Pod documented
84 0 my %hash = @_;
85 0 for(qw(mailsend logdir)) {
86 0 ▶ $mailer{$_} = $hash{$_} if($hash{$_});
Branch: 1 branch
BranchTrueFalseTotal
if $hash{$_} 0 0 0
87 }
88 }
89  
90 0 ▶sub MailSend {
Subroutine MailSend: not called CC=13 SCAR=52.0
Pod documented
91 0 my %hash = @_;
92 0 my $errno = 0;
93  
94 0 ▶ $mailer{mailsend} or return LogError("MailSend: mailsend not set");
Branch: 1 branch
BranchTrueFalseTotal
$mailer{"mailsend"} or return LogError("MailSend: mailsend not set") 0 0 0
95 0 ▶ $mailer{logdir} or return LogError("MailSend: logdir not set");
Branch: 1 branch
BranchTrueFalseTotal
$mailer{"logdir"} or return LogError("MailSend: logdir not set") 0 0 0
96  
97 0 ▶ my $template = $hash{template} or return LogError("MailSend: template not set");
Branch: 1 branch
BranchTrueFalseTotal
my $template = $hash{"template"} or return LogError("MailSend: template not set") 0 0 0
98 0 ▶ my $email = $hash{recipient_email} or return LogError("MailSend: recipient_email not set");
Branch: 1 branch
BranchTrueFalseTotal
my $email = $hash{"recipient_email"} or return LogError("MailSend: recipient_email not set") 0 0 0
99 0 my $body;
100  
101 #use Data::Dumper;
102 #LogDebug("MailSend: template=$template, email=$email, hash=".Dumper(\%hash));
103  
104 0 eval { $body = Transform($template,\%hash); };
105 0 ▶ return LogError("MailSend: error=$@") if($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
106 0 eval { $body = decode_entities($body) };
107 #LogDebug("MailSend: body=$body");
108  
109 0 ▶ unless($hash{nowrap}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hash{"nowrap"}) 0 0 0
110 0 $Text::Wrap::columns = 72;
111 0 $body = wrap('', '', $body);
112 }
113  
114 0 ▶ if($hash{output}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hash{'output'}) { } 0 0 0
115 0 ▶ my $fh = IO::File->new($hash{output},'a+') or die "Cannot write to file [$hash{output}]: $!";
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new($hash{"output"}, "a+") or die qq[Cannot write to file [$hash{"output"}]: $!] 0 0 0
116 0 $fh->binmode(':utf8');
117 0 print $fh $body;
118 0 print $fh "\n\n#-----\n";
119 0 $fh->close;
120 0 $mailer{result} = 1;
121 0 $tvars{mailer}{result} = 1;
122 } else {
123 #my $cmd = qq!|:utf8 $mailer{mailsend} $email!;
124 0 my $cmd = qq!| $mailer{mailsend} $email!;
125  
126 0 ▶ if(my $fh = IO::File->new($cmd)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fh = 'IO::File'->new($cmd)) { } 0 0 0
127 0 $fh->binmode(':utf8');
128 0 print $fh $body;
129 0 $fh->close;
130 0 $mailer{result} = 1;
131 0 $tvars{mailer}{result} = 1;
132 } else {
133 0 $mailer{result} = 0;
134 0 $tvars{mailer}{result} = 0;
135 0 $tvars{mailer}{error} = $!;
136 }
137  
138 0 ▶ unless($mailer{result}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mailer{"result"}) 0 0 0
139 0 my @files = sort glob("$mailer{logdir}/mail*.eml");
140 0 my $num = 0;
141 0 ▶ ($num) = ($files[-1] =~ /mail(\d+).eml/) if(@files);
Branch: 1 branch
BranchTrueFalseTotal
if @files 0 0 0
142 0 $num++;
143 0 my $file = sprintf "%s/mail%06d.eml", $mailer{logdir}, $num;
144 0 LogDebug("MailSend - $file");
145 0 ▶ my $fh = IO::File->new(">$file") or die "Cannot write to file [$file]: $!";
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new(">$file") or die "Cannot write to file [$file]: $!" 0 0 0
146 0 binmode($fh,':utf8');
147 0 print $fh $body;
148 0 print $fh "\n\nCommand: $cmd\n";
149 0 print $fh "Error: $tvars{mailer}{error}\n";
150 0 $fh->close;
151 0 $mailer{file} = $file;
152 }
153 }
154 }
155  
156 0 ▶sub MailSent {
Subroutine MailSent: not called CC=1 SCAR=6.9
Pod documented
157 0 return $mailer{result};
158 }
159  
160 0 ▶sub HTMLSend {
Subroutine HTMLSend: not called CC=11 SCAR=48.8
Pod documented
161 0 my %hash = @_;
162  
163 0 MIME::Lite->send('smtp', $settings{smtp}, Timeout=>60);
164 # MIME::Lite->send('sendmail', "$settings{mailsend} $hash{to}", Timeout=>60);
165  
166 my $mail = MIME::Lite->new(
167 From => $hash{from},
168 To => $hash{to},
169 Subject => $hash{subject},
170 0 Type =>'multipart/related'
171 );
172  
173 0 ▶ unless($mail) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mail) 0 0 0
174 0 LogError("HTMLSend: Error!");
175 0 return;
176 }
177  
178 0 ▶ if($hash{text}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hash{"text"}) 0 0 0
179 0 my $ref = Transform($hash{text},$hash{vars});
180 0 my $text = $ref;
181  
182 0 ▶ $mail->attach(
Branch: 1 branch
BranchTrueFalseTotal
if $text 0 0 0
183 Type => 'text/text',
184 Data => $text
185 ) if($text);
186 }
187  
188 0 ▶ if($hash{html}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hash{"html"}) 0 0 0
189 0 my $ref = Transform($hash{html},$hash{vars});
190 0 my $html = $ref;
191  
192  
193 0 for my $path ($html =~ m!href="([^"]+)"!g) {
194 0 ▶ next if($path =~ m!$settings{protregex}!);
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ /$Labyrinth::Mailer::settings{"protregex"}/ 0 0 0
195 0 my $newpath = "$settings{docroot}/$settings{webpath}/$path";
196 0 $newpath =~ s!//+!/!g;
197 0 $path =~ s!href="$path"!href="$newpath"!g;
198 }
199  
200 $mail->attach(
201 0 ▶ Type => 'text/html',
Branch: 1 branch
BranchTrueFalseTotal
if $html 0 0 0
202 Data => $html
203 ) if($html);
204 }
205  
206 0 for(@{$hash{attach}}) {
207 0 ▶ if(/\.pdf$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\.pdf$/i) { } 0 0 0
208 0 $mail->attach(Type => 'application/pdf ', Encoding => 'base64', Path => $_, Filename => basename($_));
209 } else {
210 0 my ($type,$enc) = _mtype($_);
211 0 $mail->attach(Type => $type, Encoding => $enc, Path => $_, Filename => basename($_));
212 }
213 }
214  
215 0 LogDebug("Mail=".$mail->as_string());
216 0 eval {$mail->send;};
217 0 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
218 0 LogError("MailError: eval=[$@]") ;
219 0 $mailer{result} = 0;
220 0 $tvars{mailer}{result} = 0;
221 0 $tvars{mailer}{error} = $@;
222 } else {
223 0 $mailer{result} = 1;
224 0 $tvars{mailer}{result} = 1;
225 }
226 }
227  
228 0 ▶sub HTMLSendX {
Subroutine HTMLSendX: not called CC=5 SCAR=34.0
Pod documented
229 0 my %hash = @_;
230 0 my $path = $settings{'templates'};
231  
232 0 my %config = ( # provide config info
233 RELATIVE => 1,
234 ABSOLUTE => 1,
235 INCLUDE_PATH => $path,
236 INTERPOLATE => 0,
237 POST_CHOMP => 1,
238 TRIM => 1,
239 );
240  
241 0 MIME::Lite->send('smtp', $settings{smtp}, Timeout=>60);
242 # MIME::Lite->send('sendmail', "$settings{mailsend} $hash{to}", Timeout=>60);
243  
244 my $mail = MIME::Lite::TT::HTML->new(
245 From => $hash{from},
246 To => $hash{to},
247 Subject => $hash{subject},
248 # Encoding =>'base64',
249 Encoding =>'quoted-printable',
250 Template => {
251 html => $hash{html},
252 text => $hash{text},
253 },
254 # Charset => 'utf8',
255 0 TmplOptions => \%config,
256 TmplParams => \%tvars,
257 );
258  
259 0 ▶ unless($mail) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mail) 0 0 0
260 0 LogError("HTMLSend: Error!");
261 0 return;
262 }
263  
264 0 for(@{$hash{attach}}) {
265 0 ▶ if(/\.pdf$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\.pdf$/i) { } 0 0 0
266 0 $mail->attach(Type => 'application/pdf ', Encoding => 'base64', Path => $_, Filename => basename($_));
267 } else {
268 0 my ($type,$enc) = _mtype($_);
269 0 $mail->attach(Type => $type, Encoding => $enc, Path => $_, Filename => basename($_));
270 }
271 }
272  
273 0 LogDebug("Mail=".$mail->as_string());
274 0 eval { $mail->send };
275 0 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
276 0 LogError("MailError: eval=[$@]") ;
277 0 $mailer{result} = 0;
278 0 $tvars{mailer}{result} = 0;
279 0 $tvars{mailer}{error} = $@;
280 } else {
281 0 $mailer{result} = 1;
282 0 $tvars{mailer}{result} = 1;
283 }
284 }
285  
286 0 ▶sub _mtype {
Subroutine _mtype: not called CC=1 SCAR=6.9
287 0 my $file = shift;
288 0 my $data = $mtypes->by_suffix($file);
289 0 return @$data;
290 }
291  
292 12 1;
293  
294 __END__
« Labyrinth/DBUtils.pm ↑ Summary Labyrinth/Support.pm »