blib/lib/Mail/File.pm

filter: Statementstmt 100% 64 / 64 Branchbran 94.4% 17 / 18 Conditioncond 100% 23 / 23 MC/DCmcdc 100% 11 / 11 Subroutinesub 92.8% 13 / 14 Podpod 100% 3 / 3 totaltotal 98.5% 131 / 133 CC 22 SCAR 17.8
CC
22
Cov
99%
send
10.0
new
9.0
XHeader
3.0
CRAP 5.9
↑ Summary
1 package Mail::File;
2  
3 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 8 ▶use vars qw($VERSION $AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 $VERSION = '0.13';
8  
9 #----------------------------------------------------------------------------
10  
11 =head1 NAME
12  
13 Mail::File - mail module which writes to a flat file.
14  
15 =head1 SYNOPSIS
16  
17 use Mail::File;
18  
19 my $mail = Mail::File->new(template => 'mailXXXX.tmp');
20 $mail->From('me@example.com');
21 $mail->To('you@example.com');
22 $mail->Cc('Them <them@example.com>');
23 $mail->Bcc('Us <us@example.com>; anybody@example.com');
24 $mail->Subject('Blah Blah Blah');
25 $mail->Body('Yadda Yadda Yadda');
26 $mail->XHeader('X-Header' => 'Blah Blah Blah');
27 $mail->send;
28  
29 # Or use a hash
30 my %hash = (
31 From => 'me@example.com',
32 To => 'you@example.com',
33 Cc => 'Them <them@example.com>',
34 Bcc => 'Us <us@example.com>, anybody@example.com',
35 Subject => 'Blah Blah Blah',
36 Body => 'Yadda Yadda Yadda',
37 'X-Header' => 'Blah Blah Blah',
38 template => 'mailXXXX.tmp'
39 );
40  
41 my $mail = Mail::File->new(%hash);
42 $mail->send;
43  
44 =head1 DESCRIPTION
45  
46 This module was written to overcome the problem of sending mail messages,
47 where there is no mail application available.
48  
49 The aim of the module is to write messages to a text file, that will format
50 the contents to include all the key elements of the message, such that the
51 file can be transported to another machine, which is then capable of sending
52 mail messages.
53  
54 Notes that the filename template defaults to 'mail-XXXXXX.eml'.
55  
56 =cut
57  
58 #----------------------------------------------------------------------------
59 # Library Modules
60  
61 8 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 8 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 8 ▶use File::Temp qw(tempfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
64 8 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
65  
66 #----------------------------------------------------------------------------
67 # Variables
68  
69 8 my %autosubs = map {$_ => 1} qw( From To Cc Bcc Subject Body );
70  
71 #----------------------------------------------------------------------------
72 # Interface Functions
73  
74 =head1 METHODS
75  
76 =over 4
77  
78 =item new()
79  
80 Create a new mailer object. Returns the object on success or undef on
81 failure.
82  
83 All the following can be passed as part of an anonymous hash:
84  
85 From
86 To
87 Cc
88 Bcc
89 Subject
90 Body
91 template
92  
93 The template entry is optional, and is only supplied when you call the
94 constructor. The format of the string to template follows the format as
95 for File::Temp. However, a suffix is automatically extracted. An example
96 template would be:
97  
98 mail-XXXX.tmp
99  
100 Where the temnplate to File::Temp would be 'mail-XXXX' and the suffix
101 would be '.tmp'.
102  
103 The default template, if none is supplied is:
104
105 mail-XXXXXX.eml
106  
107 =cut
108  
109 5 ▶sub new {
Subroutine new: called CC=9 SCAR=22.0
Pod documented
110 5 my ($self, %hash) = @_;
111  
112 # create an attributes hash
113 my $atts = {
114 'From' => $hash{From} || '',
115 'To' => $hash{To} || '',
116 'Cc' => $hash{Cc} || '',
117 'Bcc' => $hash{Bcc} || '',
118 'Subject' => $hash{Subject} || '',
119 'Body' => $hash{Body} || '',
120 5 ▶ 'template' => $hash{template} || 'mail-XXXXXX.eml',
Condition100%14/14
$hash{'From'} || ''
l!l
14
$hash{'To'} || ''
l!l
14
$hash{'Cc'} || ''
l!l
14
$hash{'Bcc'} || ''
l!l
14
$hash{'Subject'} || ''
l!l
14
$hash{'Body'} || ''
l!l
14
$hash{'template'} || 'mail-XXXXXX.eml'
l!l
41
MC/DC100%1/1
$hash{'From'} || ''
$hash{'From'}
MC/DC100%1/1
$hash{'To'} || ''
$hash{'To'}
MC/DC100%1/1
$hash{'Cc'} || ''
$hash{'Cc'}
MC/DC100%1/1
$hash{'Bcc'} || ''
$hash{'Bcc'}
MC/DC100%1/1
$hash{'Subject'} || ''
$hash{'Subject'}
MC/DC100%1/1
$hash{'Body'} || ''
$hash{'Body'}
MC/DC100%1/1
$hash{'template'} || 'mail-XXXXXX.eml'
$hash{'template'}
Truth table100%2/2
A
A= $hash{'From'}
Aresult
11
00
Truth table100%2/2
A
A= $hash{'To'}
Aresult
11
00
Truth table100%2/2
A
A= $hash{'Cc'}
Aresult
11
00
Truth table100%2/2
A
A= $hash{'Bcc'}
Aresult
11
00
Truth table100%2/2
A
A= $hash{'Subject'}
Aresult
11
00
Truth table100%2/2
A
A= $hash{'Body'}
Aresult
11
00
Truth table100%2/2
A
A= $hash{'template'}
Aresult
11
00
121 };
122  
123 # store the x-headers
124 5 my @xheaders = grep /^X-/, keys %hash;
125 5 foreach my $xhdr (@xheaders) { $atts->{$xhdr} = $hash{$xhdr} }
126  
127 # create the object
128 5 bless $atts, $self;
129 5 return $atts;
130 }
131  
132 0 ▶sub DESTROY {}
Subroutine DESTROY: not called CC=1 SCAR=0.0
133  
134 #----------------------------------------------------------------------------
135 # The Get & Set Methods Interface Subs
136  
137 =item Accessor Methods
138  
139 The following accessor methods are available:
140  
141 From
142 To
143 Cc
144 Bcc
145 Subject
146 Body
147  
148 All functions can be called to return the current value of the associated
149 object variable, or be called with a parameter to set a new value for the
150 object variable.
151  
152 =cut
153  
154 17 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=2 SCAR=6.9
155 8 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
156 17 my $name = $AUTOLOAD;
157 17 $name =~ s/^.*:://;
158 17 ▶ die "Unknown sub $AUTOLOAD\n" unless($autosubs{$name});
Branch: 1 branch
BranchTrueFalseTotal
unless $autosubs{$name} 1 16 17
159
160 16 ▶ *$name = sub { @_==2 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}; };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 ? : 18 12 30
161 16 goto &$name;
162 }
163  
164 =item XHeader($xheader,$value)
165  
166 Adds a header in the style of 'X-Header' to the headers of the message.
167 Returns undef if header cannot be added.
168  
169 =cut
170  
171 6 ▶sub XHeader {
Subroutine XHeader: called CC=3 SCAR=11.0
Pod documented
172 6 my ($self,$xheader,$value) = @_;
173 6 ▶ return unless($xheader =~ /^X-/);
Branch: 1 branch
BranchTrueFalseTotal
unless $xheader =~ /^X-/ 1 5 6
174 5 ▶ $value ? $self->{$xheader} = $value : $self->{$xheader};
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 2 3 5
175 }
176  
177 =item send()
178  
179 Sends the message. Returns the filename on success, 0 on failure.
180  
181 Really just writes to a file.
182  
183 =cut
184  
185 9 ▶sub send {
Subroutine send: called CC=10 SCAR=23.0
Pod documented
186 9 my ($self) = @_;
187  
188 # create output directory if necessary
189 9 ▶ if((my $path = dirname($self->{template})) ne '.') {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $path = dirname($self->{"template"})) ne ".") 7 2 9
190 7 eval { mkpath($path) };
191 7 ▶ return if($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 7 7
192 }
193  
194 # we need a basic message fields
195 return unless( $self->{From} &&
196 $self->{To} &&
197 $self->{Subject} &&
198 9 ▶ $self->{Body});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"From"} and $self->{"To"} and $self->{"Subject"} and $self->{"Body"} 4 5 9
Condition100%9/9
$self->{"From"} and $self->{"To"} and $self->{"Subject"} and $self->{"Body"}
!ll&&!rl&&r
315
$self->{"From"} and $self->{"To"} and $self->{"Subject"}
!ll&&!rl&&r
216
$self->{"From"} and $self->{"To"}
!ll&&!rl&&r
117
MC/DC100%4/4
$self->{"From"} and $self->{"To"} and $self->{"Subject"} and $self->{"Body"}
$self->{"From"} $self->{"To"} $self->{"Subject"} $self->{"Body"}
Truth table100%5/5
A and B and C and D
A= $self->{"From"}B= $self->{"To"}C= $self->{"Subject"}D= $self->{"Body"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
199  
200 # use the date we write the file
201 5 my $t = localtime;
202 5 my $date = $t->strftime();
203  
204 # Build the message
205 5 my $msg = "From: $self->{From}\n" .
206 "To: $self->{To}\n" .
207 "Subject: $self->{Subject}\n".
208 "Date: $date\n";
209 5 ▶ $msg .= "Cc: $self->{Cc}\n" if($self->{Cc});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Cc"} 2 3 5
210 5 ▶ $msg .= "Bcc: $self->{Bcc}\n" if($self->{Bcc});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Bcc"} 2 3 5
211  
212 # store the x-headers
213 5 my @xheaders = grep /^X-/, keys %$self;
214 5 foreach my $xhdr (@xheaders) { $msg .= "$xhdr: $self->{$xhdr}\n"; }
215  
216 5 $msg .= "\n$self->{Body}\n";
217  
218 5 my ($template,$suffix) = ($self->{template} =~ /(.*)(\.\w+)$/);
219 5 my ($fh, $filename) = tempfile( $template, SUFFIX => $suffix, UNLINK => 0 );
220 5 print $fh $msg;
221 5 undef $fh;
222  
223 5 return $filename;
224 }
225  
226 8 1;
227  
228 __END__
↑ Summary