blib/lib/App/Basis/Email.pm

filter: Statementstmt 77.1% 71 / 92 Branchbran 39.2% 11 / 28 Conditioncond 22.2% 2 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 89.4% 17 / 19 Podpod 50.0% 1 / 2 totaltotal 65.8% 102 / 155 CC 18 SCAR 28.8
CC
18
Cov
65%
send
49.9
BUILD
9.5
__ANON__
2.0
CRAP 17.9
↑ Summary
1 # ABSTRACT: Simple email sending, nothing special
2  
3 =head1 NAME
4  
5 App::Basis::Email
6  
7 =head1 SYNOPSIS
8  
9 use Text::Markdown qw(markdown);
10 use App::Basis::Email ;
11  
12 my $markdown = <<EOD ;
13 # Basic Markdown
14  
15 ![BBC Logo](http://static.bbci.co.uk/frameworks/barlesque/2.60.6/orb/4/img/bbc-blocks-light.png)
16  
17 That was an inlined image
18  
19 ## level2 header
20  
21 * bullet
22 * sub-bullet
23  
24 ### level3 header
25  
26 EOD
27 my $html = markdown($markdown);
28  
29 my $mail = App::Basis::Email->new( hostname => 'my.email.server', port => 25 ) ;
30 my $status = $mail->send(
31 from => 'fred@fred.test.fred',
32 to => 'fred@fred.test.fred',
33 subject => 'test HTML email, with inline images',
34 html => $html
35 );
36  
37 =head1 DESCRIPTION
38
39 Sending email should be simple, sending formatted email should be simple too.
40 I just want to be able to say send this thing via this process.
41  
42 This module provides that, a way to simply send email (plain/html or markdown) via
43 either a SMTP or sendmail target.
44  
45 Obviously I do nothing new, just wrap around existing modules, to make life simple.
46  
47 =head1 AUTHOR
48  
49 kevin mulholland
50  
51 =head1 Notes
52  
53 if you want templating then do it outside of this module and pass the formatted
54 HTML/markdown in
55  
56 =head1 See Also
57  
58 To create email I use L<Email::MIME::CreateHTML>, you may need to force this to
59 install it as there is a slight test bug
60 To send email I use L<Email::Sender::Simple> with L<Email::Sender::Transport::SMTP> and L<Email::Sender::Transport::Sendmail>
61 Markdown processing is done with L<Text::Markdown>
62  
63 =over 4
64  
65 =cut
66  
67 package App::Basis::Email;
68 2 $App::Basis::Email::VERSION = '0.3';
69 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
72 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
73 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
74  
75 2 ▶use Email::Sender::Simple qw(sendmail);
Subroutine BEGIN: called CC=1 SCAR=0.0
76 2 ▶use Email::Sender::Transport::SMTP qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
77 2 ▶use Email::Sender::Transport::Sendmail;
Subroutine BEGIN: called CC=1 SCAR=0.0
78  
79 2 ▶use Email::MIME::CreateHTML;
Subroutine BEGIN: called CC=1 SCAR=0.0
80 2 ▶use Text::Markdown 'markdown';
Subroutine BEGIN: called CC=1 SCAR=0.0
81 2 ▶use HTML::Restrict;
Subroutine BEGIN: called CC=1 SCAR=0.0
82  
83 # ----------------------------------------------------------------------------
84  
85 # what host name are we connecting to, defaults to localhost
86 2 has host => ( is => 'ro', );
87  
88 # on what port
89 has port => (
90 is => 'ro',
91 4 ▶ default => sub { 25 },
Subroutine __ANON__: called CC=1 SCAR=0.0
92 2 );
93  
94 # do we want to use SSL
95 has ssl => (
96 is => 'ro',
97 4 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
98 2 );
99  
100 # SSL needs a user
101 has user => (
102 is => 'ro',
103 4 ▶ default => sub { '' },
Subroutine __ANON__: called CC=1 SCAR=0.0
104 2 );
105 # SSL user needs a password
106 has passwd => (
107 is => 'ro',
108 4 ▶ default => sub { '' },
Subroutine __ANON__: called CC=1 SCAR=0.0
109 2 );
110  
111 # how should we send things, either SMTP or Sendmail (default)
112 2 has transport => (
113 is => 'ro',
114 writer => '_set_transport'
115 );
116  
117 # if using sendmail having the path to the binary would help
118 2 has sendmail_path => ( is => 'ro' );
119  
120 # we want a testing mode that will not send email, so that we can get output
121 # and use it for validation in our test scripts
122 2 has testing => ( is => 'ro' );
123  
124 # ----------------------------------------------------------------------------
125 2 has sender => (
126 is => 'ro',
127 init_arg => undef, # dont allow setting in constructor
128 writer => '_set_sender'
129 );
130  
131 # ----------------------------------------------------------------------------
132  
133 =item new
134  
135 Create a new instance of the email
136  
137 my $mail = App::Basis::Email->new( host => "email.server.fred", port => 25 );
138  
139 B<Parameters>
140 host ip address or hotsname of your SMTP server
141 port optional port number for SMTP, defaults to 25
142 ssl use SSL mode
143 user user for ssl
144 passwd password for ssl
145 testing flag to show testing mode, prevents sending of email
146  
147 =cut
148  
149 4 ▶sub BUILD {
Subroutine BUILD: called CC=8 SCAR=22.5
Pod undocumented
150 4 my $self = shift;
151 4 my $sender;
152 4 my $transport = $self->transport;
153  
154 4 ▶ $transport = 'SMTP' if ( $self->host );
Branch: 1 branch
BranchTrueFalseTotal
if $self->host 2 2 4
155  
156 4 ▶ die "You need either host or transport Sendmail defined" if ( !$transport );
Branch: 1 branch
BranchTrueFalseTotal
unless $transport 0 4 4
157 4 ▶ die "You need username/password for SSL" if ( $self->ssl && !( $self->user && $self->passwd ) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->ssl and not $self->user && $self->passwd 0 4 4
Condition16%1/6
$self->ssl and not $self->user && $self->passwd
!ll&&!rl&&r
400
$self->user && $self->passwd
!ll&&!rl&&r
000
MC/DC0%0/3
$self->ssl and not $self->user && $self->passwd
$self->ssl $self->user $self->passwd
Truth table25%1/4
A and not(B && C)
A= $self->sslB= $self->userC= $self->passwd
ABCresult
0XX0
1110
10X1
1101
158  
159 # its sendmail or SMTP
160 4 ▶ if ( $transport =~ /sendmail/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($transport =~ /sendmail/i) { } 2 2 4
161 2 ▶ die "sendmail_path should be passed to new when using transport => 'sendmail" if( !$self->sendmail_path) ;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->sendmail_path 0 2 2
162 2 $sender = Email::Sender::Transport::Sendmail->new( { sendmail => $self->sendmail_path } );
163 }
164 else {
165 2 $sender = Email::Sender::Transport::SMTP->new(
166 {
167 host => $self->host,
168 port => $self->port,
169 ssl => $self->ssl,
170 sasl_username => $self->user,
171 sasl_password => $self->passwd
172 }
173 );
174 }
175 # make sure we set this, as it can be tested for in our test code
176 4 $self->_set_transport($transport);
177 # this too
178 4 $self->_set_sender($sender);
179 }
180  
181 # ----------------------------------------------------------------------------
182  
183 =item send
184  
185 send the required email
186  
187 my $mail = App::Basis::Email->new( hostname => 'my.email.server', port => 25 ) ;
188 my $status = $mail->send(
189 from => 'fred@fred.test.fred',
190 to => 'fred@fred.test.fred',
191 subject => 'test HTML email, with inline images',
192 html => $html
193 );
194  
195 Any data that is in the hash is passed to the server, though some may be re-interpreted
196 as mentioned in the DESCRIPTION
197  
198 B<Parameters>
199 to email address to (can be array of email addresses)
200 cc email cc list (can be array of email addresses)
201 from email address from
202 subject subject for the email
203 text teset message to send, for email clients that do not do html
204 html html message to send
205 markdown markdown message to send, converted to html and replaces it
206 css_file link to a css file to use for HTML/markdown files
207  
208 B<Returns>
209 status - true = Sent, false = Failed (string of email message if testing flag used)
210  
211 =cut
212  
213 2 ▶sub send {
Subroutine send: called CC=11 SCAR=39.1
Pod documented
214 2 my $self = shift;
215 2 my %params = @_;
216 2 my $status = 0;
217 2 my $to_str;
218  
219 # if we have markdown, then this replaces the HTML
220 2 ▶ if ( $params{markdown} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"markdown"}) 0 2 2
221 0 $params{html} = markdown( $params{markdown} );
222 }
223 # if we do markdown or HTML and the user has not supplied alternative text
224 # then we should strip all the HTML formatting from the html text and
225 # use that
226 2 ▶ if ( $params{html} && !$params{text} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"html"} and not $params{"text"}) 2 0 2
Condition33%1/3
$params{"html"} and not $params{"text"}
!ll&&!rl&&r
002
MC/DC0%0/2
$params{"html"} and not $params{"text"}
$params{"html"} not $params{"text"}
Truth table33%1/3
A and not(B)
A= $params{"html"}B= not $params{"text"}
ABresult
0X0
100
111
227  
228 2 my $hr = HTML::Restrict->new();
229  
230 # use default rules to start with (strip away all HTML)
231 2 $params{text} = $hr->process( $params{html} );
232 }
233 2 ▶ if ( !$params{html} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($params{"html"}) 0 2 2
234 0 $params{html} = "<p>$params{text}</p>";
235 }
236  
237 2 ▶ if ( ref( $params{to} ) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params{'to'} eq 'ARRAY') { } 0 2 2
238 0 $to_str = join( ', ', @{ $params{to} } );
239 }
240 else {
241 2 $to_str = $params{to};
242 }
243  
244 my $email = Email::MIME->create_html(
245 header => [
246 From => $params{from},
247 To => $to_str,
248 Subject => $params{subject},
249 ],
250 body => $params{html},
251 text_body => $params{text},
252 2 inline_css => 1
253 );
254  
255 0 ▶ if ( ref( $params{to} ) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params{"to"} eq "ARRAY") 0 0 0
256 0 $email->header_str_set( To => @{ $params{to} } );
257 }
258  
259 0 ▶ if ( $params{cc} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"cc"}) 0 0 0
260 0 ▶ if ( ref( $params{cc} eq 'ARRAY' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($params{'cc'} eq 'ARRAY')) { } 0 0 0
261 0 $email->header_str_set( CC => @{ $params{cc} } );
262 }
263 else {
264 0 $email->header_str_set( CC => $params{cc} );
265 }
266 }
267  
268 0 my $success;
269 0 ▶ if ( $self->testing ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->testing) { } 0 0 0
270 # when we are testing we will not attempt the send
271 # but we will return what would have been sent
272 # we must assume that the send aspect works as this is built on
273 # other peoples code and considered sound.
274 # We don't do anything particularly fancy with it.
275 0 return $email->as_string;
276 }
277 else {
278 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
279 0 $success = sendmail(
280 $email,
281 {
282 # from => $params{from},
283 transport => $self->sender
284 }
285 );
286 }
287 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
288 0 warn "email sending failed: $_";
289 0 };
290 }
291  
292 0 ▶ if ($success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($success) 0 0 0
293 0 $status = 1;
294 }
295 0 return $status;
296 }
297  
298 # ----------------------------------------------------------------------------
299  
300 =back
301  
302 =cut
303  
304 # ----------------------------------------------------------------------------
305  
306 2 1;
↑ Summary