Courriel/Builder.pm

filter: Statementstmt 95.8% 46 / 48 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 16 / 16 Podpod n/a totaltotal 96.8% 62 / 64 CC 1 SCAR 0.2
CC
1
Cov
96%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Courriel/Role/Streams.pm ↑ Summary Courriel/Types.pm »
1 package Courriel::Builder;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 our $VERSION = '0.49';
7  
8 4 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Courriel;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Courriel::Header::ContentType;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Courriel::Header::Disposition;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Courriel::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Courriel::Helpers qw( parse_header_with_attributes );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use Courriel::Part::Multipart;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use Courriel::Part::Single;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use Courriel::Types qw( EmailAddressStr HashRef NonEmptyStr Str StringRef );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use DateTime::Format::Mail;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶use Devel::PartialDump;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 ▶use File::Basename qw( basename );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 0 ▶use File::LibMagic;
Subroutine BEGIN: called CC=1 SCAR=2.6
22 use File::Slurper qw( read_binary );
23 use List::AllUtils qw( first );
24 use Params::ValidationCompiler qw( validation_for );
25 use Scalar::Util qw( blessed reftype );
26  
27 our @CARP_NOT = __PACKAGE__;
28  
29 my @exports;
30  
31 BEGIN {
32 @exports = qw(
33 build_email
34 subject
35 from
36 to
37 cc
38 header
39 plain_body
40 html_body
41 attach
42 );
43 }
44  
45 use Sub::Exporter -setup => {
46 exports => \@exports,
47 groups => { default => \@exports },
48 };
49  
50 {
51 my $validator = validation_for(
52 params => [ { type => HashRef } ],
53 slurpy => HashRef,
54 );
55  
56 sub build_email {
57 my @p = $validator->(@_);
58  
59 my @headers;
60 my $plain_body;
61 my $html_body;
62 my @attachments;
63  
64 for my $p (@p) {
65 ## no critic (ControlStructures::ProhibitCascadingIfElse)
66 if ( $p->{header} ) {
67 push @headers, @{ $p->{header} };
68 }
69 elsif ( $p->{plain_body} ) {
70 $plain_body = $p->{plain_body};
71 }
72 elsif ( $p->{html_body} ) {
73 $html_body = $p->{html_body};
74 }
75 elsif ( $p->{attachment} ) {
76 push @attachments, $p->{attachment};
77 }
78 else {
79 _bad_value($p);
80 }
81 }
82  
83 my $body_part;
84 if ( $plain_body && $html_body ) {
85 my $ct = Courriel::Header::ContentType->new(
86 mime_type => 'multipart/alternative',
87 );
88  
89 $body_part = Courriel::Part::Multipart->new(
90 headers => Courriel::Headers->new,
91 content_type => $ct,
92 parts => [ $plain_body, $html_body ],
93 );
94 }
95 else {
96 $body_part = first {defined} $plain_body, $html_body;
97  
98 croak 'Cannot call build_email without a plain or html body'
99 unless $body_part;
100 }
101  
102 if (@attachments) {
103 my $ct = Courriel::Header::ContentType->new(
104 mime_type => 'multipart/mixed' );
105  
106 $body_part = Courriel::Part::Multipart->new(
107 headers => Courriel::Headers->new,
108 content_type => $ct,
109 parts => [
110 $body_part,
111 @attachments,
112 ],
113 );
114 }
115  
116 _add_required_headers( \@headers );
117  
118 # XXX - a little incestuous but I don't really want to make this method
119 # public, and delaying building the body part would make all the code more
120 # complicated than it needs to be.
121 $body_part->_set_headers(
122 Courriel::Headers->new( headers => [@headers] ) );
123  
124 return Courriel->new( part => $body_part );
125 }
126 }
127  
128 sub _bad_value {
129 croak 'A weird value was passed to build_email: '
130 . Devel::PartialDump->new->dump( $_[0] );
131 }
132  
133 sub _add_required_headers {
134 my $headers = shift;
135  
136 my %keys = map {lc} @{$headers};
137  
138 unless ( $keys{date} ) {
139 push @{$headers},
140 ( Date => DateTime::Format::Mail->format_datetime( DateTime->now )
141 );
142 }
143  
144 unless ( $keys{'message-id'} ) {
145 push @{$headers},
146 ( 'Message-Id' => Email::MessageID->new->in_brackets );
147 }
148  
149 unless ( $keys{'mime-version'} ) {
150 push @{$headers}, ( 'MIME-Version' => '1.0' );
151 }
152  
153 return;
154 }
155  
156 {
157 my $validator = validation_for(
158 params => [ { type => Str } ],
159 );
160  
161 sub subject {
162 my ($subject) = $validator->(@_);
163  
164 return { header => [ Subject => $subject ] };
165 }
166 }
167  
168 {
169 my $validator = validation_for(
170 params => [ { type => EmailAddressStr } ],
171 );
172  
173 sub from {
174 my ($from) = $validator->(@_);
175  
176 if ( blessed $from ) {
177 $from = $from->format;
178 }
179  
180 return { header => [ From => $from ] };
181 }
182 }
183  
184 {
185 my $validator = validation_for(
186 params => [ { type => EmailAddressStr } ],
187 slurpy => EmailAddressStr,
188 );
189  
190 sub to {
191 my (@to) = $validator->(@_);
192  
193 @to = map { blessed($_) ? $_->format : $_ } @to;
194  
195 return { header => [ To => join ', ', @to ] };
196 }
197 }
198  
199 {
200 my $validator = validation_for(
201 params => [ { type => EmailAddressStr } ],
202 slurpy => EmailAddressStr,
203 );
204  
205 sub cc {
206 my (@cc) = $validator->(@_);
207  
208 @cc = map { blessed($_) ? $_->format : $_ } @cc;
209  
210 return { header => [ Cc => join ', ', @cc ] };
211 }
212 }
213  
214 {
215 my $validator = validation_for(
216 params => [
217 { type => NonEmptyStr },
218 { type => Str },
219 ],
220 );
221  
222 sub header {
223 my ( $name, $value ) = $validator->(@_);
224  
225 return { header => [ $name => $value ] };
226 }
227 }
228  
229 sub plain_body {
230 my %p
231 = @_ == 1
232 ? ( content => shift )
233 : @_;
234  
235 return {
236 plain_body => _body_part(
237 %p,
238 mime_type => 'text/plain',
239 )
240 };
241 }
242  
243 sub html_body {
244 my @attachments;
245  
246 for my $i ( reverse 0 .. $#_ ) {
247 if ( ref $_[$i]
248 && reftype( $_[$i] ) eq 'HASH'
249 && $_[$i]->{attachment} ) {
250  
251 push @attachments, splice @_, $i, 1;
252 }
253 }
254  
255 my %p
256 = @_ == 1
257 ? ( content => shift )
258 : @_;
259  
260 my $body = _body_part(
261 %p,
262 mime_type => 'text/html',
263 );
264  
265 if (@attachments) {
266 $body = Courriel::Part::Multipart->new(
267 headers => Courriel::Headers->new,
268 content_type => Courriel::Header::ContentType->new(
269 mime_type => 'multipart/related'
270 ),
271 parts => [
272 $body,
273 map { $_->{attachment} } @attachments,
274 ],
275 );
276 }
277  
278 return { html_body => $body };
279 }
280  
281 {
282 my $validator = validation_for(
283 params => [
284 mime_type => { type => NonEmptyStr },
285 charset => {
286 type => NonEmptyStr,
287 default => 'UTF-8',
288 },
289 encoding => {
290 type => NonEmptyStr,
291 default => 'base64',
292 },
293 content => {
294 type => StringRef,
295 },
296 ],
297 named_to_list => 1,
298 );
299  
300 sub _body_part {
301 my ( $mime_type, $charset, $encoding, $content ) = $validator->(@_);
302  
303 my $ct = Courriel::Header::ContentType->new(
304 mime_type => $mime_type,
305 attributes => { charset => $charset },
306 );
307  
308 my $body = Courriel::Part::Single->new(
309 headers => Courriel::Headers->new,
310 content_type => $ct,
311 encoding => $encoding,
312 content => $content,
313 );
314  
315 return $body;
316 }
317 }
318  
319 sub attach {
320 my %p
321 = @_ == 1
322 ? ( file => shift )
323 : @_;
324  
325 return {
326 attachment => $p{file} ? _part_for_file(%p) : _part_for_content(%p) };
327 }
328  
329 my $flm = File::LibMagic->new;
330  
331 {
332 my $validator = validation_for(
333 params => [
334 file => { type => NonEmptyStr },
335 mime_type => { type => NonEmptyStr, optional => 1 },
336 filename => { type => NonEmptyStr, optional => 1 },
337 content_id => { type => NonEmptyStr, optional => 1 },
338 ],
339 named_to_list => 1,
340 );
341  
342 sub _part_for_file {
343 my ( $file, $mime_type, $filename, $content_id ) = $validator->(@_);
344  
345 my $ct
346 = _content_type( $mime_type // $flm->checktype_filename($file) );
347  
348 my $content = read_binary($file);
349  
350 return Courriel::Part::Single->new(
351 headers => _attachment_headers($content_id),
352 content_type => $ct,
353 disposition => _attachment_disposition( $filename // $file ),
354 encoding => 'base64',
355 content => \$content,
356 );
357 }
358 }
359  
360 {
361 my $validator = validation_for(
362 params => [
363 content => { type => StringRef },
364 mime_type => { type => NonEmptyStr, optional => 1 },
365 filename => { type => NonEmptyStr, optional => 1 },
366 content_id => { type => NonEmptyStr, optional => 1 },
367 ],
368 named_to_list => 1,
369 );
370  
371 sub _part_for_content {
372 my ( $content, $mime_type, $filename, $content_id )
373 = $validator->(@_);
374  
375 my $ct = _content_type( $mime_type
376 // $flm->checktype_contents( ${$content} ) );
377  
378 my $disp = Courriel::Header::Disposition->new(
379 disposition => 'attachment',
380 attributes => {
381 defined $filename ? ( filename => basename($filename) ) : ()
382 }
383 );
384  
385 return Courriel::Part::Single->new(
386 headers => _attachment_headers($content_id),
387 content_type => $ct,
388 disposition => _attachment_disposition($filename),
389 encoding => 'base64',
390 content => $content,
391 );
392 }
393 }
394  
395 sub _content_type {
396 my $type = shift;
397  
398 return Courriel::Header::ContentType->new(
399 mime_type => 'application/unknown' )
400 unless defined $type;
401  
402 my ( $mime_type, $attr ) = parse_header_with_attributes($type);
403  
404 return Courriel::Header::ContentType->new(
405 mime_type => 'application/unknown' )
406 unless defined $mime_type && length $mime_type;
407  
408 return Courriel::Header::ContentType->new(
409 mime_type => $mime_type,
410 attributes => $attr,
411 );
412 }
413  
414 sub _attachment_headers {
415 my $content_id = shift;
416  
417 my @headers;
418  
419 if ( defined $content_id ) {
420 $content_id = "<$content_id>"
421 unless $content_id =~ /^<[^>]+>$/;
422  
423 push @headers, ( 'Content-ID' => $content_id );
424 }
425  
426 return Courriel::Headers->new( headers => \@headers );
427 }
428  
429 sub _attachment_disposition {
430 my $file = shift;
431  
432 return Courriel::Header::Disposition->new(
433 disposition => 'attachment',
434 attributes => { defined $file ? ( filename => basename($file) ) : () }
435 );
436 }
437  
438 1;
439  
440 # ABSTRACT: Build emails with sugar
441  
442 __END__
« Courriel/Role/Streams.pm ↑ Summary Courriel/Types.pm »