| 1 | # | ||
| 2 | # $Id$ | ||
| 3 | # | ||
| 4 | # email::send Brik | ||
| 5 | # | ||
| 6 | package Metabrik::Email::Send; | ||
| 7 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 1 | ▶ | use base qw(Metabrik::Network::Smtp); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | sub brik_properties { | ||
| 13 | return { | ||
| 14 | revision => '$Revision$', | ||
| 15 | tags => [ qw(unstable smtp) ], | ||
| 16 | author => 'GomoR <GomoR[at]metabrik.org>', | ||
| 17 | license => 'http://opensource.org/licenses/BSD-3-Clause', | ||
| 18 | attributes => { | ||
| 19 | from => [ qw(from) ], | ||
| 20 | to => [ qw(to) ], | ||
| 21 | cc => [ qw(cc) ], | ||
| 22 | bcc => [ qw(bcc) ], | ||
| 23 | subject => [ qw(subject) ], | ||
| 24 | server => [ qw(server) ], | ||
| 25 | port => [ qw(port) ], | ||
| 26 | hello => [ qw(hello) ], | ||
| 27 | auth_mechanism => [ qw(none|GSSAPI) ], | ||
| 28 | use_starttls => [ qw(0|1) ], | ||
| 29 | username => [ qw(username) ], | ||
| 30 | password => [ qw(password) ], | ||
| 31 | }, | ||
| 32 | attributes_default => { | ||
| 33 | from => 'from@example.com', | ||
| 34 | to => 'to@example.com', | ||
| 35 | subject => 'My subject', | ||
| 36 | server => 'localhost', | ||
| 37 | port => 25, | ||
| 38 | use_starttls => 0, | ||
| 39 | }, | ||
| 40 | commands => { | ||
| 41 | send => [ qw(email from|OPTIONAL to|OPTIONAL subject|OPTIONAL) ], | ||
| 42 | }, | ||
| 43 | require_modules => { | ||
| 44 | 'DateTime' => [ ], | ||
| 45 | 'DateTime::Format::Mail' => [ ], | ||
| 46 | }, | ||
| 47 | }; | ||
| 48 | } | ||
| 49 | |||
| 50 | sub send { | ||
| 51 | my $self = shift; | ||
| 52 | my ($email, $from, $to, $subject) = @_; | ||
| 53 | |||
| 54 | $from ||= $self->from; | ||
| 55 | $to ||= $self->to; | ||
| 56 | $subject ||= $self->subject; | ||
| 57 | $self->brik_help_run_undef_arg('send', $email) or return; | ||
| 58 | $self->brik_help_run_invalid_arg('send', $email, 'Email::Simple') or return; | ||
| 59 | $self->brik_help_run_undef_arg('send', $from) or return; | ||
| 60 | $self->brik_help_run_undef_arg('send', $to) or return; | ||
| 61 | $self->brik_help_run_undef_arg('send', $subject) or return; | ||
| 62 | |||
| 63 | my $cc = $self->cc; | ||
| 64 | my $bcc = $self->bcc; | ||
| 65 | |||
| 66 | my $ct = $email->header('Content-Type'); | ||
| 67 | my $cl = $email->header('Content-Length'); | ||
| 68 | my $ce = $email->header('Content-Transfer-Encoding'); | ||
| 69 | my $lc = $email->header('Lines'); | ||
| 70 | |||
| 71 | my $dt = DateTime->now; | ||
| 72 | my $date = DateTime::Format::Mail->format_datetime($dt); | ||
| 73 | |||
| 74 | $self->log->verbose("send: From [$from]"); | ||
| 75 | $self->log->verbose("send: To [$to]"); | ||
| 76 | $self->log->verbose("send: Date [$date]"); | ||
| 77 | $self->log->verbose("send: Subject [$subject]"); | ||
| 78 | #print "Content-Type [$ct]\n\n"; | ||
| 79 | #print $email->body,"\n"; | ||
| 80 | |||
| 81 | my $smtp = $self->open or return; | ||
| 82 | |||
| 83 | $smtp->mail($from); | ||
| 84 | $smtp->to($to); | ||
| 85 | |||
| 86 | if (defined($cc)) { | ||
| 87 | $smtp->cc($cc); | ||
| 88 | } | ||
| 89 | |||
| 90 | if (defined($bcc)) { | ||
| 91 | $smtp->bcc($bcc); | ||
| 92 | } | ||
| 93 | |||
| 94 | $smtp->data; | ||
| 95 | $smtp->datasend("Content-Type: $ct\r\n") if defined($ct); | ||
| 96 | $smtp->datasend("Content-Length: $cl\r\n") if defined($cl); | ||
| 97 | $smtp->datasend("Content-Transfer-Encoding: $ce\r\n") if defined($ce); | ||
| 98 | $smtp->datasend("Lines: $lc\r\n") if defined($lc); | ||
| 99 | $smtp->datasend("Date: $date\r\n"); | ||
| 100 | $smtp->datasend("From: $from\r\n"); | ||
| 101 | $smtp->datasend("To: $to\r\n"); | ||
| 102 | if (defined($cc)) { | ||
| 103 | $smtp->datasend("Cc: $cc\r\n"); | ||
| 104 | } | ||
| 105 | $smtp->datasend("Subject: $subject\r\n\r\n"); | ||
| 106 | $smtp->datasend($email->body); | ||
| 107 | $smtp->dataend; | ||
| 108 | |||
| 109 | $self->log->verbose("send: message sent"); | ||
| 110 | |||
| 111 | $self->close; | ||
| 112 | |||
| 113 | return 1; | ||
| 114 | } | ||
| 115 | |||
| 116 | 1; | ||
| 117 | |||
| 118 | __END__ | ||