| 1 | package Dancer::Plugin::Email; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 1 | our $VERSION = '1.0300'; # VERSION | |||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 1 | ▶ | use Dancer qw(:syntax debug warning); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 1 | ▶ | use Dancer::Plugin; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 1 | ▶ | use Email::Sender::Simple 'sendmail'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 1 | ▶ | use Email::Date::Format 'email_date'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 1 | ▶ | use File::Type; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 1 | ▶ | use MIME::Entity; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 1 | ▶ | use Module::Load 'load'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | register email => sub { | ||||||||||||||||||||
| 14 | 0 | ▶ | my $params = shift || {}; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=26 SCAR=65.5
Condition0%0/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||
| 15 | 0 | my $multipart = delete $params->{multipart}; | |||||||||||||||||||
| 16 | 0 | ▶ | my $extra_headers = delete($params->{headers}) || {}; | ||||||||||||||||||
|
Condition0%0/2
delete $params->{'headers'} || {}
MC/DC0%0/1
delete $params->{'headers'} || {}
delete $params->{'headers'}
Truth table0%0/2
A
A= delete $params->{'headers'}
| |||||||||||||||||||||
| 17 | 0 | my $conf = plugin_setting; | |||||||||||||||||||
| 18 | 0 | ▶ | my $conf_headers = $conf->{headers} || {}; | ||||||||||||||||||
|
Condition0%0/2
$conf->{'headers'} || {}
MC/DC0%0/1
$conf->{'headers'} || {}
$conf->{'headers'}
Truth table0%0/2
A
A= $conf->{'headers'}
| |||||||||||||||||||||
| 19 | 0 | my %headers = ( %$conf_headers, %$params, %$extra_headers ); | |||||||||||||||||||
| 20 | 0 | my $attach = $headers{attach}; | |||||||||||||||||||
| 21 | 0 | my $sender = delete $headers{sender}; | |||||||||||||||||||
| 22 | 0 | ▶ | if (my $type = $headers{type}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 23 | 0 | ▶ | $headers{Type} = $type eq 'html' ? 'text/html' : 'text/plain'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 24 | } | ||||||||||||||||||||
| 25 | 0 | ▶ | $headers{Type} ||= 'text/plain'; | ||||||||||||||||||
|
Condition0%0/2
$headers{"Type"} ||= "text/plain"
MC/DC0%0/1
$headers{"Type"} ||= "text/plain"
$headers{"Type"}
Truth table0%0/2
A
A= $headers{"Type"}
| |||||||||||||||||||||
| 26 | 0 | ▶ | $headers{Format} ||= 'flowed' if $headers{Type} eq 'text/plain'; | ||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$headers{"Format"} ||= "flowed"
MC/DC0%0/1
$headers{"Format"} ||= "flowed"
$headers{"Format"}
Truth table0%0/2
A
A= $headers{"Format"}
| |||||||||||||||||||||
| 27 | 0 | ▶ | $headers{Date} ||= email_date(); | ||||||||||||||||||
|
Condition0%0/3
$headers{"Date"} ||= email_date()
MC/DC0%0/2
$headers{"Date"} ||= email_date()
$headers{"Date"}
email_date()
Truth table0%0/3
A ||= B
A= $headers{"Date"}B= email_date()
| |||||||||||||||||||||
| 28 | 0 | delete $headers{$_} for qw(body message attach type); | |||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | my $email = MIME::Entity->build( | ||||||||||||||||||||
| 31 | Charset => 'utf-8', | ||||||||||||||||||||
| 32 | Encoding => 'quoted-printable', | ||||||||||||||||||||
| 33 | %headers, | ||||||||||||||||||||
| 34 | Data => $params->{body} || $params->{message}, | ||||||||||||||||||||
| 35 | 0 | ▶ | ); | ||||||||||||||||||
|
Condition0%0/3
$params->{'body'} || $params->{'message'}
MC/DC0%0/2
$params->{'body'} || $params->{'message'}
$params->{'body'}
$params->{'message'}
Truth table0%0/3
A || B
A= $params->{'body'}B= $params->{'message'}
| |||||||||||||||||||||
| 36 | 0 | ▶ | if ($attach) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 37 | 0 | ▶ | if ($multipart) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 38 | # by default, when you add an attachment, | ||||||||||||||||||||
| 39 | # C<make_multipart> will be called by MIME::Entity, but | ||||||||||||||||||||
| 40 | # defaults to 'mixed'. Thunderbird doesn't like this for | ||||||||||||||||||||
| 41 | # embedded images, so we have a chance to set it to | ||||||||||||||||||||
| 42 | # 'related' or anything that the user wants | ||||||||||||||||||||
| 43 | 0 | $email->make_multipart($multipart); | |||||||||||||||||||
| 44 | } | ||||||||||||||||||||
| 45 | 0 | ▶ | my @attachments = ref($attach) eq 'ARRAY' ? @$attach : $attach; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 46 | 0 | for my $attachment (@attachments) { | |||||||||||||||||||
| 47 | 0 | my %mime; | |||||||||||||||||||
| 48 | 0 | ▶ | if (ref($attachment) eq 'HASH') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 49 | 0 | %mime = %$attachment; | |||||||||||||||||||
| 50 | 0 | ▶ | unless ($mime{Path}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 51 | 0 | warning "No Path provided for this attachment!"; | |||||||||||||||||||
| 52 | 0 | next; | |||||||||||||||||||
| 53 | }; | ||||||||||||||||||||
| 54 | 0 | ▶ | $mime{Encoding} ||= 'base64'; | ||||||||||||||||||
|
Condition0%0/2
$mime{"Encoding"} ||= "base64"
MC/DC0%0/1
$mime{"Encoding"} ||= "base64"
$mime{"Encoding"}
Truth table0%0/2
A
A= $mime{"Encoding"}
| |||||||||||||||||||||
| 55 | 0 | ▶ | $mime{Type} ||= File::Type->mime_type($mime{Path}), | ||||||||||||||||||
|
Condition0%0/3
$mime{"Type"} ||= "File::Type"->mime_type($mime{"Path"})
MC/DC0%0/2
$mime{"Type"} ||= "File::Type"->mime_type($mime{"Path"})
$mime{"Type"}
"File::Type"->mime_type($mime{"Path"})
Truth table0%0/3
A ||= B
A= $mime{"Type"}B= "File::Type"->mime_type($mime{"Path"})
| |||||||||||||||||||||
| 56 | } else { | ||||||||||||||||||||
| 57 | 0 | %mime = ( | |||||||||||||||||||
| 58 | Path => $attachment, | ||||||||||||||||||||
| 59 | Type => File::Type->mime_type($attachment), | ||||||||||||||||||||
| 60 | Encoding => 'base64', | ||||||||||||||||||||
| 61 | ); | ||||||||||||||||||||
| 62 | } | ||||||||||||||||||||
| 63 | 0 | $email->attach(%mime); | |||||||||||||||||||
| 64 | } | ||||||||||||||||||||
| 65 | } | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 0 | my $transport; | |||||||||||||||||||
| 68 | 0 | ▶ | my $conf_transport = $conf->{transport} || {}; | ||||||||||||||||||
|
Condition0%0/2
$conf->{'transport'} || {}
MC/DC0%0/1
$conf->{'transport'} || {}
$conf->{'transport'}
Truth table0%0/2
A
A= $conf->{'transport'}
| |||||||||||||||||||||
| 69 | 0 | ▶ | if (my ($transport_name) = keys %$conf_transport) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 70 | 0 | ▶ | my $transport_params = $conf_transport->{$transport_name} || {}; | ||||||||||||||||||
|
Condition0%0/2
$conf_transport->{$transport_name} || {}
MC/DC0%0/1
$conf_transport->{$transport_name} || {}
$conf_transport->{$transport_name}
Truth table0%0/2
A
A= $conf_transport->{$transport_name}
| |||||||||||||||||||||
| 71 | 0 | my $transport_class = "Email::Sender::Transport::$transport_name"; | |||||||||||||||||||
| 72 | 0 | my $transport_redirect = $transport_params->{redirect_address}; | |||||||||||||||||||
| 73 | 0 | load $transport_class; | |||||||||||||||||||
| 74 | 0 | $transport = $transport_class->new($transport_params); | |||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 0 | ▶ | if ($transport_redirect) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 77 | 0 | $transport_class = 'Email::Sender::Transport::Redirect'; | |||||||||||||||||||
| 78 | 0 | load $transport_class; | |||||||||||||||||||
| 79 | 0 | debug "Redirecting email to $transport_redirect."; | |||||||||||||||||||
| 80 | 0 | $transport = $transport_class->new( | |||||||||||||||||||
| 81 | transport => $transport, | ||||||||||||||||||||
| 82 | redirect_address => $transport_redirect | ||||||||||||||||||||
| 83 | ); | ||||||||||||||||||||
| 84 | } | ||||||||||||||||||||
| 85 | } | ||||||||||||||||||||
| 86 | 0 | my %sendmail_arg = ( transport => $transport ); | |||||||||||||||||||
| 87 | 0 | ▶ | $sendmail_arg{from} = $sender if defined $sender; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 88 | 0 | ▶ | if ( $headers{bcc} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 89 | 0 | sendmail $email, { %sendmail_arg, to => $headers{bcc} }; | |||||||||||||||||||
| 90 | 0 | $email->head->delete('bcc'); | |||||||||||||||||||
| 91 | } | ||||||||||||||||||||
| 92 | 0 | return sendmail $email, \%sendmail_arg; | |||||||||||||||||||
| 93 | 1 | }; | |||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 1 | register_plugin; | |||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | # ABSTRACT: Simple email sending for Dancer applications | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | 1 | 1; | |||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | __END__ | ||||||||||||||||||||