| 1 | package Dancer::Plugin::Email; | ||
| 2 | { | ||
| 3 | $Dancer::Plugin::Email::VERSION = '0.1300_02'; | ||
| 4 | } | ||
| 5 | |||
| 6 | use Dancer ':syntax'; | ||
| 7 | use Dancer::Plugin; | ||
| 8 | use Email::Sender::Simple 'sendmail'; | ||
| 9 | use File::Type; | ||
| 10 | use MIME::Entity; | ||
| 11 | use Module::Load 'load'; | ||
| 12 | use Try::Tiny; | ||
| 13 | |||
| 14 | register email => sub { | ||
| 15 | my $params = shift || {}; | ||
| 16 | my $conf = plugin_setting; | ||
| 17 | my $conf_headers = $conf->{headers} || {}; | ||
| 18 | my %headers = ( %$conf_headers, %$params ); | ||
| 19 | my $attach = $headers{attach}; | ||
| 20 | delete $headers{$_} for qw(body message attach); | ||
| 21 | |||
| 22 | my $email = MIME::Entity->build( | ||
| 23 | %headers, | ||
| 24 | Data => $params->{body} || $params->{message}, | ||
| 25 | ); | ||
| 26 | if ($attach) { | ||
| 27 | my @attachments = ref($attach) eq 'ARRAY' ? @$attach : $attach; | ||
| 28 | for my $attachment (@attachments) { | ||
| 29 | $email->attach( | ||
| 30 | Path => $attachment, | ||
| 31 | Type => File::Type->mime_type($attachment), | ||
| 32 | Encoding => 'base64', | ||
| 33 | ); | ||
| 34 | } | ||
| 35 | } | ||
| 36 | |||
| 37 | my $transport; | ||
| 38 | my $conf_transport = $conf->{transport} || {}; | ||
| 39 | if (my ($transport_name) = keys %$conf_transport) { | ||
| 40 | my $transport_params = $conf_transport->{$transport_name} || {}; | ||
| 41 | my $transport_class = "Email::Sender::Transport::$transport_name"; | ||
| 42 | load $transport_class; | ||
| 43 | $transport = $transport_class->new($transport_params); | ||
| 44 | } | ||
| 45 | return sendmail $email, { transport => $transport }; | ||
| 46 | }; | ||
| 47 | |||
| 48 | |||
| 49 | register_plugin; | ||
| 50 | |||
| 51 | # ABSTRACT: Simple email sending for Dancer applications | ||
| 52 | |||
| 53 | |||
| 54 | 1; | ||
| 55 | |||
| 56 | __END__ |