| 1 | # Copyright Infomation | ||
| 2 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 3 | # Author : Dr. Ahmed Amin Elsheshtawy, Ph.D. | ||
| 4 | # Website: https://github.com/mewsoft/Nile, http://www.mewsoft.com | ||
| 5 | # Email : mewsoft@cpan.org, support@mewsoft.com | ||
| 6 | # Copyrights (c) 2014-2015 Mewsoft Corp. All rights reserved. | ||
| 7 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 8 | package Nile::Plugin::Email; | ||
| 9 | |||
| 10 | our $VERSION = '0.55'; | ||
| 11 | our $AUTHORITY = 'cpan:MEWSOFT'; | ||
| 12 | |||
| 13 | =pod | ||
| 14 | |||
| 15 | =encoding utf8 | ||
| 16 | |||
| 17 | =head1 NAME | ||
| 18 | |||
| 19 | Nile::Plugin::Email - Email plugin for the Nile framework. | ||
| 20 | |||
| 21 | =head1 SYNOPSIS | ||
| 22 | |||
| 23 | $email = $app->plugin->email; | ||
| 24 | # or create new object | ||
| 25 | #$email = $app->plugin->email->new; | ||
| 26 | |||
| 27 | $email->email({ | ||
| 28 | from => 'ahmed@mewsoft.com', | ||
| 29 | to => 'ramy@domain.com', | ||
| 30 | subject => 'Happy birthday Ramy', | ||
| 31 | body => 'Hello Ramy, Happy new year Ramy', | ||
| 32 | type => 'html', # 'html' or 'text' message type | ||
| 33 | attach => '/path/to/attachment', # optional attachment file | ||
| 34 | }); | ||
| 35 | |||
| 36 | =head1 DESCRIPTION | ||
| 37 | |||
| 38 | Nile::Plugin::Email - Email plugin for the Nile framework. | ||
| 39 | |||
| 40 | This plugin build on the top of L<Email::Sender::Simple> module which supports any transporter like L<Sendmail|Email::Sender::Transport::Sendmail>, L<SMTP|Email::Sender::Transport::SMTP> etc. | ||
| 41 | |||
| 42 | Plugin configuration can be set in the config file under the plugin section. | ||
| 43 | |||
| 44 | Example plugin configuration for C<SMTP> transporter: | ||
| 45 | |||
| 46 | <plugin> | ||
| 47 | |||
| 48 | <email> | ||
| 49 | <transport>SMTP</transport> | ||
| 50 | <host>localhost</host> | ||
| 51 | <ssl>0</ssl> | ||
| 52 | <port>25</port> | ||
| 53 | <timeout>120</timeout> | ||
| 54 | <sasl_username></sasl_username> | ||
| 55 | <sasl_password>0</sasl_password> | ||
| 56 | <allow_partial_success>0</allow_partial_success> | ||
| 57 | <helo>Hello from loclahost</helo> | ||
| 58 | <localaddr>localhost</localaddr> | ||
| 59 | <localport>1234</localport> | ||
| 60 | </email> | ||
| 61 | |||
| 62 | </plugin> | ||
| 63 | |||
| 64 | Example plugin configuration for C<Sendmail> transporter: | ||
| 65 | |||
| 66 | <plugin> | ||
| 67 | |||
| 68 | <email> | ||
| 69 | <transport>Sendmail</transport> | ||
| 70 | <sendmail>/usr/sbin/sendmail</sendmail> | ||
| 71 | </email> | ||
| 72 | |||
| 73 | </plugin> | ||
| 74 | |||
| 75 | If no configuration found, the module will search for Sendmail program and use it, if not found it will | ||
| 76 | try to connect to local SMTP server. | ||
| 77 | |||
| 78 | You also can set the transporter programmatically using the method L</transport> below. | ||
| 79 | |||
| 80 | =cut | ||
| 81 | |||
| 82 | use Nile::Plugin; # also extends Nile::Plugin | ||
| 83 | |||
| 84 | use Try::Tiny; | ||
| 85 | use Module::Load; | ||
| 86 | #use Net::SMTP::SSL; | ||
| 87 | use MIME::Entity; | ||
| 88 | use Email::Simple; | ||
| 89 | use Email::Sender::Simple qw(sendmail); # try_to_sendmail | ||
| 90 | use Email::Date::Format qw(email_date); | ||
| 91 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 92 | sub main { # our sub new {} | ||
| 93 | |||
| 94 | my ($self, $arg) = @_; | ||
| 95 | |||
| 96 | $self->type('html'); | ||
| 97 | $self->mailer("Nile"); | ||
| 98 | |||
| 99 | my $setting = $self->setting(); | ||
| 100 | |||
| 101 | my $transport = delete $setting->{transport}; | ||
| 102 | |||
| 103 | |||
| 104 | if ($transport) { | ||
| 105 | $self->transport($transport, $setting); | ||
| 106 | } | ||
| 107 | } | ||
| 108 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 109 | =head2 email() | ||
| 110 | |||
| 111 | Send email with optional attachments quickly: | ||
| 112 | |||
| 113 | $email->email({ | ||
| 114 | from => 'ahmed@mewsoft.com', | ||
| 115 | #from => '"Ahmed Elsheshtawy" <ahmed@mewsoft.com>', | ||
| 116 | to => 'ramy@domain.com', | ||
| 117 | subject => 'Happy birthday Ramy', | ||
| 118 | body => 'Hello Ramy, Happy new year Ramy', | ||
| 119 | type => 'html', # 'html' or 'text' message type, default 'html' | ||
| 120 | attach => '/path/to/attachment', # optional attachment file | ||
| 121 | }); | ||
| 122 | |||
| 123 | Send email with many options: | ||
| 124 | |||
| 125 | $email->email({ | ||
| 126 | |||
| 127 | from => 'ahmed@mewsoft.com', | ||
| 128 | to => 'ramy@ramys.net', | ||
| 129 | |||
| 130 | cc => ['x@y.com', 'a@b.com', 'b@d.com'], | ||
| 131 | bcc => ['c@x.com', 'e@z.com', 'r@j.com'], | ||
| 132 | |||
| 133 | subject => 'Happy birthday Ramy', | ||
| 134 | |||
| 135 | # optional headers | ||
| 136 | sender => 'support@mewsoft.com', # set to the 'from' if not set | ||
| 137 | #'Return-Path' => '', # set to the 'from' if not set | ||
| 138 | #'Reply-To' => '', # set to the 'from' if not set | ||
| 139 | #'X-Mailer' => 'Nile', | ||
| 140 | #'X-Priority' => 3, # highest => 1, high => 2, normal => 3, low => 4, lowest => 5 | ||
| 141 | #Date => email_date(), # automatically set to current date and time | ||
| 142 | #Comments => '', | ||
| 143 | #Encrypted => '', | ||
| 144 | #References => '', | ||
| 145 | #'Message-ID' => '', | ||
| 146 | #'MIME-Version' => '', | ||
| 147 | #Organization => 'Mewsoft', | ||
| 148 | |||
| 149 | #multipart => 'related', | ||
| 150 | |||
| 151 | type => 'html', # 'html' or 'text' message type, default 'html' | ||
| 152 | |||
| 153 | # optional raw headers overried above and used as is | ||
| 154 | header => { | ||
| 155 | "X-Mailer" => '', | ||
| 156 | "X-Accept-Language" => 'en', | ||
| 157 | }, | ||
| 158 | |||
| 159 | # attachments | ||
| 160 | attach => [ | ||
| 161 | '/path/to/attachment1', | ||
| 162 | '/path/to/attachment2', | ||
| 163 | '/path/to/attachment3', | ||
| 164 | { | ||
| 165 | Path => "/path/to/attachment4", | ||
| 166 | # Mime::Entity options. | ||
| 167 | Id => "...", | ||
| 168 | } | ||
| 169 | ], | ||
| 170 | }); | ||
| 171 | |||
| 172 | Set the email components one by one for more control: | ||
| 173 | |||
| 174 | $email->from('ahmed@mewsoft.com'); | ||
| 175 | $email->to('x@domain.com'); | ||
| 176 | $email->cc(['a@b.com', 'b@c.com']); | ||
| 177 | $email->bcc(['d@e.com', 'e@f.com']); | ||
| 178 | $email->sender('company@mewsoft.com'); | ||
| 179 | $email->type('text'); | ||
| 180 | $email->mailer('Nile'); | ||
| 181 | $email->subject('Happy birthday'); | ||
| 182 | $email->body('Hello Ramy, Happy new year'); | ||
| 183 | |||
| 184 | # send the email | ||
| 185 | $email->send(); | ||
| 186 | |||
| 187 | =cut | ||
| 188 | |||
| 189 | sub email { | ||
| 190 | |||
| 191 | my ($self, $args) = @_; | ||
| 192 | |||
| 193 | # X-Priority: highest => 1, high => 2, normal => 3, low => 4, lowest => 5 | ||
| 194 | |||
| 195 | # Bcc Encrypted Received Sender | ||
| 196 | # Cc From References Subject | ||
| 197 | # Comments Keywords Reply-To To | ||
| 198 | # Content-* Message-ID Resent-* X-* | ||
| 199 | # Date MIME-Version Return-Path | ||
| 200 | # Organization | ||
| 201 | |||
| 202 | foreach my $header (qw(from to cc bcc sender multipart Return-Path Reply-To X-Mailer X-Priority Subject | ||
| 203 | Date Comments Encrypted References Message-ID MIME-Version Organization Content-Type | ||
| 204 | )) { | ||
| 205 | if (exists $args->{$header}) { | ||
| 206 | $self->header->{$header} = $args->{$header}; #decode_utf8($args->{$header}); | ||
| 207 | } | ||
| 208 | } | ||
| 209 | |||
| 210 | $args->{header}->{'Return-Path'} ||= $args->{header}->{From}; | ||
| 211 | $args->{header}->{'Reply-To'} ||= $args->{header}->{From}; | ||
| 212 | |||
| 213 | if ($args->{type}) { | ||
| 214 | $self->header->{'Content-Type'} = lc($args->{type}) eq "html" ? | ||
| 215 | 'text/html; charset=' . $self->charset : 'text/plain; charset=' . $self->charset; | ||
| 216 | } | ||
| 217 | |||
| 218 | if (exists $args->{mailer}) { | ||
| 219 | $self->header->{'X-Mailer'} = $args->{mailer}; | ||
| 220 | } | ||
| 221 | |||
| 222 | # optional headers | ||
| 223 | if (exists $args->{header}) { | ||
| 224 | while (my ($k, $v) = each %{ $args->{header} }) { | ||
| 225 | $self->header->{$k} = $v; #decode_utf8($v}); | ||
| 226 | } | ||
| 227 | } | ||
| 228 | |||
| 229 | #Content-Transfer-Encoding: 8bit | ||
| 230 | #Content-type: text/html; charset=UTF-8 | ||
| 231 | |||
| 232 | if ($self->header->{type} =~ /text\/plain/i && !$self->format) { | ||
| 233 | $self->format('flowed'); | ||
| 234 | } | ||
| 235 | |||
| 236 | if ($args->{attach}) { | ||
| 237 | $self->attach(ref($args->{attach}) eq 'ARRAY' ? @{$args->{attach}} : $args->{attach}); | ||
| 238 | } | ||
| 239 | |||
| 240 | $self->subject($args->{subject}) if ($args->{subject}); | ||
| 241 | |||
| 242 | $self->body($args->{body} || $args->{message}) if ($args->{body} || $args->{message}); | ||
| 243 | |||
| 244 | $self->send; | ||
| 245 | } | ||
| 246 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 247 | =head2 from() | ||
| 248 | |||
| 249 | $email->from('ahmed@mewsoft.com'); | ||
| 250 | $from = $email->from; | ||
| 251 | |||
| 252 | Sets or returns the C<from> email address. | ||
| 253 | |||
| 254 | =cut | ||
| 255 | |||
| 256 | has 'from' => ( | ||
| 257 | is => 'rw', | ||
| 258 | ); | ||
| 259 | |||
| 260 | =head2 to() | ||
| 261 | |||
| 262 | $email->to('ahmed@mewsoft.com'); | ||
| 263 | $to = $email->to; | ||
| 264 | |||
| 265 | Sets or returns the C<to> email address. | ||
| 266 | |||
| 267 | =cut | ||
| 268 | |||
| 269 | sub to { | ||
| 270 | my ($self, @to) = @_; | ||
| 271 | if (@to) { | ||
| 272 | foreach my $to (@to) { | ||
| 273 | push @{$self->to_list}, $to; | ||
| 274 | } | ||
| 275 | } | ||
| 276 | else { | ||
| 277 | @{$self->to_list}; | ||
| 278 | } | ||
| 279 | } | ||
| 280 | |||
| 281 | has 'to_list' => ( | ||
| 282 | is => 'rw', | ||
| 283 | isa => 'ArrayRef', | ||
| 284 | default => sub { [] } | ||
| 285 | ); | ||
| 286 | |||
| 287 | =head2 sender() | ||
| 288 | |||
| 289 | # if not set, the from address will be used | ||
| 290 | $email->sender('support@mewsoft.com'); | ||
| 291 | $sender = $email->sender; | ||
| 292 | |||
| 293 | Sets or returns the C<sender> email address. | ||
| 294 | |||
| 295 | =cut | ||
| 296 | |||
| 297 | has 'sender' => ( | ||
| 298 | is => 'rw', | ||
| 299 | ); | ||
| 300 | |||
| 301 | =head2 cc() | ||
| 302 | |||
| 303 | $email->cc('x@mewsoft.com', 'y@mewsoft.com'); | ||
| 304 | @cc = $email->cc; | ||
| 305 | |||
| 306 | Sets or returns the C<cc> email addresses. | ||
| 307 | |||
| 308 | =cut | ||
| 309 | |||
| 310 | sub cc { | ||
| 311 | my ($self, @cc) = @_; | ||
| 312 | if (@cc) { | ||
| 313 | foreach my $cc (@cc) { | ||
| 314 | push @{$self->cc_list}, $cc; | ||
| 315 | } | ||
| 316 | } | ||
| 317 | else { | ||
| 318 | @{$self->cc_list}; | ||
| 319 | } | ||
| 320 | } | ||
| 321 | |||
| 322 | has 'cc_list' => ( | ||
| 323 | is => 'rw', | ||
| 324 | isa => 'ArrayRef', | ||
| 325 | default => sub { [] } | ||
| 326 | ); | ||
| 327 | |||
| 328 | =head2 bcc() | ||
| 329 | |||
| 330 | $email->bcc('x@mewsoft.com', 'y@mewsoft.com'); | ||
| 331 | @bcc= $email->bcc; | ||
| 332 | |||
| 333 | Sets or returns the C<bcc> email addresses. | ||
| 334 | |||
| 335 | =cut | ||
| 336 | |||
| 337 | sub bcc { | ||
| 338 | my ($self, @bcc) = @_; | ||
| 339 | if (@bcc) { | ||
| 340 | foreach my $bcc (@bcc) { | ||
| 341 | push @{$self->bcc_list}, $bcc; | ||
| 342 | } | ||
| 343 | } | ||
| 344 | else { | ||
| 345 | @{$self->bcc_list} | ||
| 346 | } | ||
| 347 | } | ||
| 348 | |||
| 349 | has 'bcc_list' => ( | ||
| 350 | is => 'rw', | ||
| 351 | isa => 'ArrayRef', | ||
| 352 | default => sub { [] } | ||
| 353 | ); | ||
| 354 | |||
| 355 | =head2 subject() | ||
| 356 | |||
| 357 | $email->subject($subject); | ||
| 358 | $subject = $email->subject; | ||
| 359 | |||
| 360 | Sets or returns the C<subject>. | ||
| 361 | |||
| 362 | =cut | ||
| 363 | |||
| 364 | has 'subject' => ( | ||
| 365 | is => 'rw', | ||
| 366 | ); | ||
| 367 | |||
| 368 | =head2 body() | ||
| 369 | |||
| 370 | $email->body($body); | ||
| 371 | $body = $email->body; | ||
| 372 | |||
| 373 | Sets or returns the C<body>. | ||
| 374 | |||
| 375 | =cut | ||
| 376 | |||
| 377 | has 'body' => ( | ||
| 378 | is => 'rw', | ||
| 379 | ); | ||
| 380 | |||
| 381 | =head2 encoding() | ||
| 382 | |||
| 383 | # default is 'quoted-printable' | ||
| 384 | $email->encoding($encoding); | ||
| 385 | $encoding = $email->encoding; | ||
| 386 | |||
| 387 | Sets or returns the C<encoding>. Standard encodings are '7bit', '8bit', 'base64', 'binary', 'quoted-printable'. | ||
| 388 | Non-standard encodings are 'binhex', 'binhex40', 'mac-binhex40', 'mac-binhex', 'x-uu', 'x-uuencode'. | ||
| 389 | |||
| 390 | Encoding auto detected if not set. | ||
| 391 | |||
| 392 | =cut | ||
| 393 | |||
| 394 | has 'encoding' => ( | ||
| 395 | is => 'rw', | ||
| 396 | #default => 'quoted-printable', # auto detected | ||
| 397 | ); | ||
| 398 | |||
| 399 | =head2 format() | ||
| 400 | |||
| 401 | # $format = 'flowed'; | ||
| 402 | $email->format($format); | ||
| 403 | $format = $email->format; | ||
| 404 | |||
| 405 | Sets or returns the C<format>. | ||
| 406 | |||
| 407 | =cut | ||
| 408 | |||
| 409 | has 'format' => ( | ||
| 410 | is => 'rw', | ||
| 411 | ); | ||
| 412 | |||
| 413 | =head2 charset() | ||
| 414 | |||
| 415 | # default $charset = "UTF-8"; | ||
| 416 | $email->charset($charset); | ||
| 417 | $charset = $email->charset; | ||
| 418 | |||
| 419 | Sets or returns the C<charset>. | ||
| 420 | |||
| 421 | =cut | ||
| 422 | |||
| 423 | has 'charset' => ( | ||
| 424 | is => 'rw', | ||
| 425 | default => "UTF-8", | ||
| 426 | ); | ||
| 427 | |||
| 428 | =head2 multipart() | ||
| 429 | |||
| 430 | # default is "mixed" for "multipart/mixed" | ||
| 431 | $email->multipart("mixed"); | ||
| 432 | $multipart = $email->multipart; | ||
| 433 | |||
| 434 | Sets or returns the C<multipart>. | ||
| 435 | |||
| 436 | =cut | ||
| 437 | |||
| 438 | has 'multipart' => ( | ||
| 439 | is => 'rw', | ||
| 440 | ); | ||
| 441 | |||
| 442 | =head2 header() | ||
| 443 | |||
| 444 | $email->header->{$name} = $value; | ||
| 445 | $value = $email->header->{$name}; | ||
| 446 | |||
| 447 | Sets or returns the C<header> entries. | ||
| 448 | |||
| 449 | =cut | ||
| 450 | |||
| 451 | has 'header' => ( | ||
| 452 | is => 'rw', | ||
| 453 | isa => 'HashRef', | ||
| 454 | default => sub { +{} } | ||
| 455 | ); | ||
| 456 | |||
| 457 | has 'attachments' => ( | ||
| 458 | is => 'rw', | ||
| 459 | isa => 'ArrayRef', | ||
| 460 | default => sub { [] }, | ||
| 461 | ); | ||
| 462 | |||
| 463 | has 'transporter' => ( | ||
| 464 | is => 'rw', | ||
| 465 | ); | ||
| 466 | |||
| 467 | =head2 type() | ||
| 468 | |||
| 469 | # html email message | ||
| 470 | $email->type("html"); | ||
| 471 | |||
| 472 | # text/plain email message | ||
| 473 | $email->type("text"); | ||
| 474 | |||
| 475 | Sets or returns the message content type. | ||
| 476 | |||
| 477 | =cut | ||
| 478 | |||
| 479 | sub type { | ||
| 480 | my ($self, $type) = @_; | ||
| 481 | if ($type) { | ||
| 482 | $self->header->{'Content-Type'} = lc($type) eq "html" ? 'text/html; ' : 'text/plain; '; | ||
| 483 | $self->header->{'Content-Type'} .= "charset=" . $self->charset; | ||
| 484 | } | ||
| 485 | else { | ||
| 486 | return $self->header->{'Content-Type'}; | ||
| 487 | } | ||
| 488 | } | ||
| 489 | |||
| 490 | =head2 mailer() | ||
| 491 | |||
| 492 | $email->mailer("Nile"); | ||
| 493 | |||
| 494 | Sets or returns the header C<X-Mailer>. | ||
| 495 | |||
| 496 | =cut | ||
| 497 | |||
| 498 | sub mailer { | ||
| 499 | my ($self, $mailer) = @_; | ||
| 500 | if ($mailer) { | ||
| 501 | $self->header->{'X-Mailer'} = $mailer; | ||
| 502 | } | ||
| 503 | else { | ||
| 504 | return $self->header->{'X-Mailer'}; | ||
| 505 | } | ||
| 506 | } | ||
| 507 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 508 | =head2 attach() | ||
| 509 | |||
| 510 | $email->attach('/path/to/attachment1'); | ||
| 511 | $email->attach('/path/to/attachment2'); | ||
| 512 | |||
| 513 | $email->attach( | ||
| 514 | '/path/to/attachment1', | ||
| 515 | '/path/to/attachment2', | ||
| 516 | '/path/to/attachment3', | ||
| 517 | { | ||
| 518 | Path => "/path/to/attachment4", | ||
| 519 | # Mime::Entity options | ||
| 520 | Id => "...", | ||
| 521 | } | ||
| 522 | ); | ||
| 523 | |||
| 524 | Attach files to the email message. | ||
| 525 | |||
| 526 | =cut | ||
| 527 | |||
| 528 | sub attach { | ||
| 529 | |||
| 530 | my ($self, @attach) = @_; | ||
| 531 | |||
| 532 | my ($attach, %attach); | ||
| 533 | |||
| 534 | foreach $attach (@attach) { | ||
| 535 | if (ref($attach) eq 'HASH') { | ||
| 536 | $attach->{Encoding} ||= 'base64'; | ||
| 537 | $attach->{Type} ||= $self->app->mime->for_file($attach->{Path}); | ||
| 538 | } | ||
| 539 | else { | ||
| 540 | $attach = { | ||
| 541 | Path => $attach, | ||
| 542 | Encoding => 'base64', | ||
| 543 | Type => $self->app->mime->for_file($attach), | ||
| 544 | }; | ||
| 545 | } | ||
| 546 | push @{$self->attachments}, $attach; | ||
| 547 | } | ||
| 548 | |||
| 549 | $self; | ||
| 550 | } | ||
| 551 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 552 | =head2 transport() | ||
| 553 | |||
| 554 | $email->transport($transport, %options); | ||
| 555 | |||
| 556 | # set transport to Sendmail program, path auto detected | ||
| 557 | $email->transport('Sendmail'); | ||
| 558 | |||
| 559 | # set transport to Sendmail program, specify path to it | ||
| 560 | $email->transport('Sendmail', { sendmail => '/usr/sbin/sendmail' }); | ||
| 561 | |||
| 562 | # set transport to SMTP with options | ||
| 563 | $email->transport("SMTP", { | ||
| 564 | host => 'mail.mewsoft.com', # the name of the host to connect to; defaults to localhost | ||
| 565 | ssl => 0, # if true, connect via SSL; defaults to false | ||
| 566 | port => 25, # port to connect to; defaults to 25 for non-SSL, 465 for SSL | ||
| 567 | timeout => 180, # maximum time in secs to wait for server; default is 120 | ||
| 568 | sasl_username => '', # the username to use for auth; optional | ||
| 569 | sasl_password => '', # the password to use for auth; required if username is provided | ||
| 570 | allow_partial_success => 0, # if true, will send data even if some recipients were rejected; defaults to false | ||
| 571 | helo => 'mail.mewsoft.com', # what to say when saying HELO; no default | ||
| 572 | #localaddr => 'localhost', # local address from which to connect | ||
| 573 | #localport =>'1234', # local port from which to connect | ||
| 574 | }); | ||
| 575 | |||
| 576 | # custom transport | ||
| 577 | #$transport = MyTransport->new(); | ||
| 578 | $email->transport($transport, %options); | ||
| 579 | |||
| 580 | Set the email transporter class. Can be C<Email::Sender::Transport> subclass | ||
| 581 | name like C<Sendmail>, C<SMTP>, etc or C<Email::Sender::Transport> type object. | ||
| 582 | |||
| 583 | To find available transporters, search cpan for L<Email::Sender::Transport|http://search.cpan.org/search?query=+Email%3A%3ASender%3A%3ATransport%3A&mode=all> | ||
| 584 | |||
| 585 | =cut | ||
| 586 | |||
| 587 | sub transport { | ||
| 588 | |||
| 589 | my ($self, $transport, @arg) = @_; | ||
| 590 | |||
| 591 | if ( $self->app->instance_isa($transport, 'Email::Sender::Transport') ) { | ||
| 592 | $self->transporter($transport); | ||
| 593 | } | ||
| 594 | else { | ||
| 595 | my $class = "Email::Sender::Transport::$transport"; | ||
| 596 | load $class; | ||
| 597 | $self->transporter($class->new(@arg)); | ||
| 598 | } | ||
| 599 | } | ||
| 600 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 601 | =head2 send() | ||
| 602 | |||
| 603 | $email->send(); | ||
| 604 | |||
| 605 | Send the email with current settings. This method is called automatically by the method C<email> to send the email. | ||
| 606 | |||
| 607 | =cut | ||
| 608 | |||
| 609 | sub send { | ||
| 610 | my ($self) = @_; | ||
| 611 | |||
| 612 | # get plugin setting from config files. same as $self->setting("email"); | ||
| 613 | # setting method is inherited from Nile::Plugin the base class | ||
| 614 | my $setting = $self->setting(); | ||
| 615 | |||
| 616 | #$self->app->dump($setting); | ||
| 617 | |||
| 618 | $self->header->{Date} ||= email_date(); | ||
| 619 | |||
| 620 | my $email = MIME::Entity->build( | ||
| 621 | Charset => $self->charset, | ||
| 622 | Encoding => $self->encoding || '', | ||
| 623 | From => $self->from, | ||
| 624 | Sender => ($self->sender || $self->from), | ||
| 625 | To => $self->to_list, | ||
| 626 | Cc => $self->cc_list, | ||
| 627 | Bcc => $self->bcc_list, | ||
| 628 | Subject => $self->subject, | ||
| 629 | %{$self->header}, | ||
| 630 | Data => $self->body, | ||
| 631 | ); | ||
| 632 | |||
| 633 | #$email->attach(Path => $gif_path, Type => "image/gif", Encoding => "base64"); | ||
| 634 | if (@{$self->attachments}) { | ||
| 635 | if ($self->multipart) { | ||
| 636 | #make_multipart [SUBTYPE], OPTSHASH... | ||
| 637 | $email->make_multipart($self->multipart); | ||
| 638 | } | ||
| 639 | foreach my $attach (@{$self->attachments}) { | ||
| 640 | $email->attach(%{$attach}); | ||
| 641 | } | ||
| 642 | } | ||
| 643 | |||
| 644 | # text/plain | ||
| 645 | # content_type => 'text/plain', charset => 'utf-8', encoding => 'quoted-printable', format => 'flowed', | ||
| 646 | # text/html | ||
| 647 | # content_type => 'text/html', charset => 'utf-8', encoding => 'quoted-printable', | ||
| 648 | |||
| 649 | # return sendmail $email, $options; # dies on error | ||
| 650 | try { | ||
| 651 | sendmail($email, { | ||
| 652 | ($self->transporter ? (transport => $self->transporter) : ()), | ||
| 653 | }); | ||
| 654 | return; | ||
| 655 | } | ||
| 656 | catch { | ||
| 657 | my $error = $_ || 'unknown error'; | ||
| 658 | #try { | ||
| 659 | # if ($error->isa('Email::Sender::Failure')) { | ||
| 660 | # $error = Email::Sender::Failure->message; | ||
| 661 | # } | ||
| 662 | #}; | ||
| 663 | #say "Email status: ". $error; | ||
| 664 | return $error; | ||
| 665 | } | ||
| 666 | |||
| 667 | } | ||
| 668 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 669 | |||
| 670 | =pod | ||
| 671 | |||
| 672 | =head1 Bugs | ||
| 673 | |||
| 674 | This project is available on github at L<https://github.com/mewsoft/Nile>. | ||
| 675 | |||
| 676 | =head1 HOMEPAGE | ||
| 677 | |||
| 678 | Please visit the project's homepage at L<https://metacpan.org/release/Nile>. | ||
| 679 | |||
| 680 | =head1 SOURCE | ||
| 681 | |||
| 682 | Source repository is at L<https://github.com/mewsoft/Nile>. | ||
| 683 | |||
| 684 | =head1 SEE ALSO | ||
| 685 | |||
| 686 | See L<Nile> for details about the complete framework. | ||
| 687 | |||
| 688 | =head1 AUTHOR | ||
| 689 | |||
| 690 | Ahmed Amin Elsheshtawy, احمد امين الششتاوى <mewsoft@cpan.org> | ||
| 691 | Website: http://www.mewsoft.com | ||
| 692 | |||
| 693 | =head1 COPYRIGHT AND LICENSE | ||
| 694 | |||
| 695 | Copyright (C) 2014-2015 by Dr. Ahmed Amin Elsheshtawy احمد امين الششتاوى mewsoft@cpan.org, support@mewsoft.com, | ||
| 696 | L<https://github.com/mewsoft/Nile>, L<http://www.mewsoft.com> | ||
| 697 | |||
| 698 | This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 699 | |||
| 700 | =cut | ||
| 701 | |||
| 702 | 1; |