File Coverage

blib/lib/Labyrinth/Mailer.pm
Criterion Covered Total %
statement 37 39 94.8
branch n/a
condition n/a
subroutine 13 13 100.0
pod n/a
total 50 52 96.1


line stmt bran cond sub pod time code
1             package Labyrinth::Mailer;
2              
3 2     2   6681 use warnings;
  2         2  
  2         51  
4 2     2   7 use strict;
  2         2  
  2         37  
5 2     2   8 use utf8;
  2         2  
  2         12  
6              
7 2     2   38 use vars qw($VERSION @ISA %EXPORT_TAGS @EXPORT @EXPORT_OK);
  2         2  
  2         146  
8             $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             require Exporter;
34             @ISA = qw(Exporter);
35             @EXPORT = ( qw( MailSet MailSend MailSent HTMLSend ) );
36              
37             # -------------------------------------
38             # Library Modules
39              
40 2     2   7 use File::Basename;
  2         2  
  2         107  
41 2     2   7 use HTML::Entities;
  2         2  
  2         76  
42 2     2   10 use IO::File;
  2         2  
  2         204  
43 2     2   1336 use MIME::Lite;
  2         22865  
  2         52  
44 2     2   740 use MIME::Lite::TT::HTML;
  2         283538  
  2         54  
45 2     2   15 use MIME::Types;
  2         4  
  2         60  
46 2     2   108423 use Text::Wrap;
  2         4234  
  2         168  
47              
48 2     2   13 use Labyrinth::Audit;
  2         3  
  2         292  
49 2     2   91 use Labyrinth::Writer;
  0            
  0            
50             use Labyrinth::Variables;
51              
52             # -------------------------------------
53             # Variables
54              
55             my $mtypes = MIME::Types->new;
56              
57             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             sub MailSet {
84             my %hash = @_;
85             for(qw(mailsend logdir)) {
86             $mailer{$_} = $hash{$_} if($hash{$_});
87             }
88             }
89              
90             sub MailSend {
91             my %hash = @_;
92             my $errno = 0;
93              
94             $mailer{mailsend} or return LogError("MailSend: mailsend not set");
95             $mailer{logdir} or return LogError("MailSend: logdir not set");
96              
97             my $template = $hash{template} or return LogError("MailSend: template not set");
98             my $email = $hash{recipient_email} or return LogError("MailSend: recipient_email not set");
99             my $body;
100              
101             #use Data::Dumper;
102             #LogDebug("MailSend: template=$template, email=$email, hash=".Dumper(\%hash));
103              
104             eval { $body = Transform($template,\%hash); };
105             return LogError("MailSend: error=$@") if($@);
106             eval { $body = decode_entities($body) };
107             #LogDebug("MailSend: body=$body");
108              
109             unless($hash{nowrap}) {
110             $Text::Wrap::columns = 72;
111             $body = wrap('', '', $body);
112             }
113              
114             if($hash{output}) {
115             my $fh = IO::File->new($hash{output},'a+') or die "Cannot write to file [$hash{output}]: $!";
116             $fh->binmode(':utf8');
117             print $fh $body;
118             print $fh "\n\n#-----\n";
119             $fh->close;
120             $mailer{result} = 1;
121             $tvars{mailer}{result} = 1;
122             } else {
123             #my $cmd = qq!|:utf8 $mailer{mailsend} $email!;
124             my $cmd = qq!| $mailer{mailsend} $email!;
125              
126             if(my $fh = IO::File->new($cmd)) {
127             $fh->binmode(':utf8');
128             print $fh $body;
129             $fh->close;
130             $mailer{result} = 1;
131             $tvars{mailer}{result} = 1;
132             } else {
133             $mailer{result} = 0;
134             $tvars{mailer}{result} = 0;
135             $tvars{mailer}{error} = $!;
136             }
137              
138             unless($mailer{result}) {
139             my @files = sort glob("$mailer{logdir}/mail*.eml");
140             my $num = 0;
141             ($num) = ($files[-1] =~ /mail(\d+).eml/) if(@files);
142             $num++;
143             my $file = sprintf "%s/mail%06d.eml", $mailer{logdir}, $num;
144             LogDebug("MailSend - $file");
145             my $fh = IO::File->new(">$file") or die "Cannot write to file [$file]: $!";
146             binmode($fh,':utf8');
147             print $fh $body;
148             print $fh "\n\nCommand: $cmd\n";
149             print $fh "Error: $tvars{mailer}{error}\n";
150             $fh->close;
151             $mailer{file} = $file;
152             }
153             }
154             }
155              
156             sub MailSent {
157             return $mailer{result};
158             }
159              
160             sub HTMLSend {
161             my %hash = @_;
162              
163             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             Type =>'multipart/related'
171             );
172              
173             unless($mail) {
174             LogError("HTMLSend: Error!");
175             return;
176             }
177              
178             if($hash{text}) {
179             my $ref = Transform($hash{text},$hash{vars});
180             my $text = $ref;
181              
182             $mail->attach(
183             Type => 'text/text',
184             Data => $text
185             ) if($text);
186             }
187              
188             if($hash{html}) {
189             my $ref = Transform($hash{html},$hash{vars});
190             my $html = $ref;
191              
192              
193             for my $path ($html =~ m!href="([^"]+)"!g) {
194             next if($path =~ m!$settings{protregex}!);
195             my $newpath = "$settings{docroot}/$settings{webpath}/$path";
196             $newpath =~ s!//+!/!g;
197             $path =~ s!href="$path"!href="$newpath"!g;
198             }
199              
200             $mail->attach(
201             Type => 'text/html',
202             Data => $html
203             ) if($html);
204             }
205              
206             for(@{$hash{attach}}) {
207             if(/\.pdf$/i) {
208             $mail->attach(Type => 'application/pdf ', Encoding => 'base64', Path => $_, Filename => basename($_));
209             } else {
210             my ($type,$enc) = _mtype($_);
211             $mail->attach(Type => $type, Encoding => $enc, Path => $_, Filename => basename($_));
212             }
213             }
214              
215             LogDebug("Mail=".$mail->as_string());
216             eval {$mail->send;};
217             if($@) {
218             LogError("MailError: eval=[$@]") ;
219             $mailer{result} = 0;
220             $tvars{mailer}{result} = 0;
221             $tvars{mailer}{error} = $@;
222             } else {
223             $mailer{result} = 1;
224             $tvars{mailer}{result} = 1;
225             }
226             }
227              
228             sub HTMLSendX {
229             my %hash = @_;
230             my $path = $settings{'templates'};
231              
232             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             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             TmplOptions => \%config,
256             TmplParams => \%tvars,
257             );
258              
259             unless($mail) {
260             LogError("HTMLSend: Error!");
261             return;
262             }
263              
264             for(@{$hash{attach}}) {
265             if(/\.pdf$/i) {
266             $mail->attach(Type => 'application/pdf ', Encoding => 'base64', Path => $_, Filename => basename($_));
267             } else {
268             my ($type,$enc) = _mtype($_);
269             $mail->attach(Type => $type, Encoding => $enc, Path => $_, Filename => basename($_));
270             }
271             }
272              
273             LogDebug("Mail=".$mail->as_string());
274             eval { $mail->send };
275             if($@) {
276             LogError("MailError: eval=[$@]") ;
277             $mailer{result} = 0;
278             $tvars{mailer}{result} = 0;
279             $tvars{mailer}{error} = $@;
280             } else {
281             $mailer{result} = 1;
282             $tvars{mailer}{result} = 1;
283             }
284             }
285              
286             sub _mtype {
287             my $file = shift;
288             my $data = $mtypes->by_suffix($file);
289             return @$data;
290             }
291              
292             1;
293              
294             __END__