Email-Send
view release on metacpan or search on metacpan
lib/Email/Send.pm view on Meta::CPAN
return $invocant unless $invocant;
$invocant->can('is_available')
or return failure "Mailer $mailer doesn't report availability.";
my $test = $invocant->is_available(@args);
return $test unless $test;
return success;
}
sub _objectify_message {
my ($self, $message) = @_;
return undef unless defined $message;
return $message if UNIVERSAL::isa($message, 'Email::Simple');
return Email::Simple->new($message) unless ref($message);
return Email::Abstract->cast($message => 'Email::Simple')
if eval { require Email::Abstract };
return undef;
}
sub _mailer_invocant {
my ($self, $mailer) = @_;
return $mailer if Scalar::Util::blessed($mailer);
# is the mailer a plugin given by short name?
my $package = exists $self->_plugin_list->{$mailer}
? $self->_plugin_list->{$mailer}
: $mailer;
eval "require $package" or return failure "$@";
return $package;
}
sub _send_it {
my ($self, $mailer, $message) = @_;
my $test = $self->mailer_available($mailer);
return $test unless $test;
my $invocant = $self->_mailer_invocant($mailer);
return $invocant->send($message, @{$self->mailer_args});
}
sub _try_all {
my ($self, $simple) = @_;
foreach ( $self->all_mailers ) {
next if $_ eq 'Email::Send::Test';
my $sent = $self->_send_it($_, $simple);
return $sent if $sent;
}
return failure "Unable to send message.";
}
# Classic Interface.
sub import {
no strict 'refs';
*{(caller)[0] . '::send'} = __PACKAGE__->can('_send_function');
}
sub _send_function {
my ($mailer, $message, @args) = @_;
__PACKAGE__->new({
mailer => $mailer,
mailer_args => \@args,
})->send($message);
}
1;
__END__
=head2 Writing Mailers
package Email::Send::Example;
sub is_available {
eval { use Net::Example }
}
sub send {
my ($class, $message, @args) = @_;
use Net::Example;
Net::Example->do_it($message) or return;
}
1;
Writing new mailers is very simple. If you want to use a short name
when calling C<send>, name your mailer under the C<Email::Send> namespace.
If you don't, the full name will have to be used. A mailer only needs
to implement a single function, C<send>. It will be called from
C<Email::Send> exactly like this.
Your::Sending::Package->send($message, @args);
C<$message> is an Email::Simple object, C<@args> are the extra
arguments passed into C<Email::Send::send>.
Here's an example of a mailer that sends email to a URL.
package Email::Send::HTTP::Post;
use strict;
use vars qw[$AGENT $URL $FIELD];
use Return::Value;
sub is_available {
eval { use LWP::UserAgent }
}
sub send {
my ($class, $message, @args);
require LWP::UserAgent;
if ( @args ) {
my ($URL, $FIELD) = @args;
( run in 0.597 second using v1.01-cache-2.11-cpan-5511b514fd6 )