/usr/share/perl5/Dancer/Plugin/Email.pm is in libdancer-plugin-email-perl 1.0400-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 | package Dancer::Plugin::Email;
our $VERSION = '1.0400'; # VERSION
use Dancer qw(:syntax debug warning);
use Dancer::Plugin;
use Email::Sender::Simple 'sendmail';
use Email::Date::Format 'email_date';
use File::Type;
use MIME::Entity;
use Module::Load 'load';
register email => sub {
my $params = shift || {};
my $multipart = delete $params->{multipart};
my $extra_headers = delete($params->{headers}) || {};
my $conf = plugin_setting;
my $conf_headers = $conf->{headers} || {};
my %headers = ( %$conf_headers, %$params, %$extra_headers );
my $attach = $headers{attach};
my $sender = delete $headers{sender};
if (my $type = $headers{type}) {
$headers{Type} = $type eq 'html' ? 'text/html' : 'text/plain';
}
$headers{Type} ||= 'text/plain';
$headers{Format} ||= 'flowed' if $headers{Type} eq 'text/plain';
$headers{Date} ||= email_date();
delete $headers{$_} for qw(body message attach type);
my $email = MIME::Entity->build(
Charset => 'utf-8',
Encoding => 'quoted-printable',
%headers,
Data => $params->{body} || $params->{message},
);
if ($attach) {
if ($multipart) {
# by default, when you add an attachment,
# C<make_multipart> will be called by MIME::Entity, but
# defaults to 'mixed'. Thunderbird doesn't like this for
# embedded images, so we have a chance to set it to
# 'related' or anything that the user wants
$email->make_multipart($multipart);
}
my @attachments = ref($attach) eq 'ARRAY' ? @$attach : $attach;
for my $attachment (@attachments) {
my %mime;
if (ref($attachment) eq 'HASH') {
%mime = %$attachment;
unless ($mime{Path} || $mime{Data}) {
warning "No Path or Data provided for this attachment!";
next;
};
if ($mime{Path}) {
$mime{Encoding} ||= 'base64';
$mime{Type} ||= File::Type->mime_type($mime{Path});
}
} else {
%mime = (
Path => $attachment,
Type => File::Type->mime_type($attachment),
Encoding => 'base64',
);
}
$email->attach(%mime);
}
}
my $transport;
my $conf_transport = $conf->{transport} || {};
if (my ($transport_name) = keys %$conf_transport) {
my $transport_params = $conf_transport->{$transport_name} || {};
my $transport_class = "Email::Sender::Transport::$transport_name";
my $transport_redirect = $transport_params->{redirect_address};
load $transport_class;
$transport = $transport_class->new($transport_params);
if ($transport_redirect) {
$transport_class = 'Email::Sender::Transport::Redirect';
load $transport_class;
debug "Redirecting email to $transport_redirect.";
$transport = $transport_class->new(
transport => $transport,
redirect_address => $transport_redirect
);
}
}
my %sendmail_arg = ( transport => $transport );
$sendmail_arg{from} = $sender if defined $sender;
if ( $headers{bcc} ) {
sendmail $email, { %sendmail_arg, to => $headers{bcc} };
$email->head->delete('bcc');
}
return sendmail $email, \%sendmail_arg;
};
register_plugin;
# ABSTRACT: Simple email sending for Dancer applications
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Dancer::Plugin::Email - Simple email sending for Dancer applications
=head1 VERSION
version 1.0400
=head1 SYNOPSIS
use Dancer;
use Dancer::Plugin::Email;
post '/contact' => sub {
email {
from => 'bob@foo.com',
to => 'sue@foo.com',
subject => 'allo',
body => 'Dear Sue, ...',
attach => '/path/to/attachment',
};
};
=head1 DESCRIPTION
This plugin tries to make sending emails from L<Dancer> applications as simple
as possible.
It uses L<Email::Sender> under the hood.
In a lot of cases, no configuration is required.
For example, if your app is hosted on a unix-like server with sendmail
installed, calling C<email()> will just do the right thing.
IMPORTANT: Version 1.x of this module is not backwards compatible with the
0.x versions.
This module was originally built on Email::Stuff which was built on
Email::Send which has been deprecated in favor of Email::Sender.
Versions 1.x and on have be refactored to use Email::Sender.
I have tried to keep the interface the same as much as possible.
The main difference is the configuration.
If there are features missing that you were using in older versions,
then please let me know by creating an issue on
L<github|https://github.com/ironcamel/Dancer-Plugin-Email>.
=head1 FUNCTIONS
This module by default exports the single function C<email>.
=head2 email
This function sends an email.
It takes a single argument, a hashref of parameters.
Default values for the parameters may be provided in the headers section of
the L</CONFIGURATION>.
Paramaters provided to this function will override the corresponding
configuration values if there is any overlap.
An exception is thrown if sending the email fails,
so wrapping calls to C<email> with try/catch is recommended.
use Dancer;
use Dancer::Plugin::Email;
use Try::Tiny;
post '/contact' => sub {
try {
email {
sender => 'bounces-here@foo.com', # optional
from => 'bob@foo.com',
to => 'sue@foo.com, jane@foo.com',
bcc => 'sam@foo.com',
subject => 'allo',
body => 'Dear Sue, ...<img src="cid:blabla">',
multipart => 'related', # optional, see below
attach => [
'/path/to/attachment1',
'/path/to/attachment2',
{
Path => "/path/to/attachment3",
# Path is required when passing a hashref.
# See Mime::Entity for other optional values.
Id => "blabla",
}
],
type => 'html', # can be 'html' or 'plain'
# Optional extra headers
headers => {
"X-Mailer" => 'This fine Dancer application',
"X-Accept-Language" => 'en',
}
};
} catch {
error "Could not send email: $_";
};
};
=head1 CONFIGURATION
No configuration is necessarily required.
L<Email::Sender::Simple> tries to make a good guess about how to send the
message.
It will usually try to use the sendmail program on unix-like systems
and SMTP on Windows.
However, you may explicitly configure a transport in your configuration.
Only one transport may be configured.
For documentation for the parameters of the transport, see the corresponding
Email::Sender::Transport::* module.
For example, the parameters available for the SMTP transport are documented
here L<Email::Sender::Transport::SMTP/ATTRIBUTES>.
You may also provide default headers in the configuration:
plugins:
Email:
# Set default headers (OPTIONAL)
headers:
sender: 'bounces-here@foo.com'
from: 'bob@foo.com'
subject: 'default subject'
X-Mailer: 'MyDancer 1.0'
X-Accept-Language: 'en'
# Explicity set a transport (OPTIONAL)
transport:
Sendmail:
sendmail: '/usr/sbin/sendmail'
Example configuration for sending mail via Gmail:
plugins:
Email:
transport:
SMTP:
ssl: 1
host: 'smtp.gmail.com'
port: 465
sasl_username: 'bob@gmail.com'
sasl_password: 'secret'
Use the Sendmail transport using the sendmail program in the system path:
plugins:
Email:
transport:
Sendmail:
Use the Sendmail transport with an explicit path to the sendmail program:
plugins:
Email:
transport:
Sendmail:
sendmail: '/usr/sbin/sendmail'
=head2 Multipart messages
You can embed images in HTML messages this way: first, set the C<type>
to C<html>. Then pass the attachments as hashrefs, setting C<Path> and
C<Id>. In the HTML body, refer to the attachment using the C<Id>,
prepending C<cid:> in the C<src> attribute. This works for popular
webmail clients like Gmail and OE, but is not enough for Thunderbird,
which wants a C<multipart/related> mail, not the default
C<multipart/mixed>. You can fix this adding the C<multipart> parameter
set to C<related>, which set the desired subtype when you pass
attachments.
Example:
email {
from => $from,
to => $to,
subject => $subject,
body => q{<p>Image embedded: <img src="cid:mycid"/></p>},
type => 'html',
attach => [ { Id => 'mycid', Path => '/path/to/file' }],
multipart => 'related'
};
The C<attach> value accepts either a single attachment or an arrayref
of attachment. Each attachment may be a scalar, with the path of the
file to attach, or an hashref, in which case the hashref is passed to
the L<Mime::Entity>'s C<attach> method.
=head1 CONTRIBUTORS
=over
=item *
Marco Pessotto <melmothx@gmail.com>
=item *
Oleg A. Mamontov <oleg@mamontov.net>
=item *
Rusty Conover <https://github.com/rustyconover>
=item *
Stefan Hornburg <racke@linuxia.de>
=back
=head1 SEE ALSO
=over
=item L<Email::Sender>
=item L<MIME::Entity>
=back
=head1 AUTHORS
=over 4
=item *
Naveed Massjouni <naveed@vt.edu>
=item *
Al Newkirk <awncorp@cpan.org>
=back
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2010 by awncorp.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|