| 1 | package Email::Send; | ||||||||||||
| 2 | 14 | ▶ | use v5.14.0; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | |||||||||||||
| 4 | 14 | our $VERSION = '2.202'; | |||||||||||
| 5 | 14 | $VERSION = eval $VERSION; | |||||||||||
| 6 | |||||||||||||
| 7 | 14 | ▶ | use Email::Simple 1.92; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 14 | ▶ | use Module::Pluggable 2.97 | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 14 | search_path => 'Email::Send', | |||||||||||
| 10 | 14 | except => $Email::Send::__plugin_exclusion; | |||||||||||
| 11 | |||||||||||||
| 12 | 14 | ▶ | BEGIN { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | 14 | local $Return::Value::NO_CLUCK = 1; | |||||||||||
| 14 | 14 | require Return::Value; | |||||||||||
| 15 | 14 | Return::Value->import; | |||||||||||
| 16 | } | ||||||||||||
| 17 | 14 | ▶ | use Scalar::Util 1.02 (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 18 | |||||||||||||
| 19 | =head1 NAME | ||||||||||||
| 20 | |||||||||||||
| 21 | Email::Send - Simply Sending Email | ||||||||||||
| 22 | |||||||||||||
| 23 | =head1 WAIT! ACHTUNG! | ||||||||||||
| 24 | |||||||||||||
| 25 | Email::Send is going away... well, not really going away, but it's being | ||||||||||||
| 26 | officially marked "out of favor." It has API design problems that make it hard | ||||||||||||
| 27 | to usefully extend and rather than try to deprecate features and slowly ease in | ||||||||||||
| 28 | a new interface, we've released Email::SendB<er> which fixes these problems and | ||||||||||||
| 29 | others. As of today, 2008-12-19, Email::Sender is young, but it's fairly | ||||||||||||
| 30 | well-tested. Please consider using it instead for any new work. | ||||||||||||
| 31 | |||||||||||||
| 32 | =head1 SYNOPSIS | ||||||||||||
| 33 | |||||||||||||
| 34 | use Email::Send; | ||||||||||||
| 35 | |||||||||||||
| 36 | my $message = <<'__MESSAGE__'; | ||||||||||||
| 37 | To: recipient@example.com | ||||||||||||
| 38 | From: sender@example.com | ||||||||||||
| 39 | Subject: Hello there folks | ||||||||||||
| 40 | |||||||||||||
| 41 | How are you? Enjoy! | ||||||||||||
| 42 | __MESSAGE__ | ||||||||||||
| 43 | |||||||||||||
| 44 | my $sender = Email::Send->new({mailer => 'SMTP'}); | ||||||||||||
| 45 | $sender->mailer_args([Host => 'smtp.example.com']); | ||||||||||||
| 46 | $sender->send($message); | ||||||||||||
| 47 | |||||||||||||
| 48 | # more complex | ||||||||||||
| 49 | my $bulk = Email::Send->new; | ||||||||||||
| 50 | for ( qw[SMTP Sendmail Qmail] ) { | ||||||||||||
| 51 | $bulk->mailer($_) and last if $bulk->mailer_available($_); | ||||||||||||
| 52 | } | ||||||||||||
| 53 | |||||||||||||
| 54 | $bulk->message_modifier(sub { | ||||||||||||
| 55 | my ($sender, $message, $to) = @_; | ||||||||||||
| 56 | $message->header_set(To => qq[$to\@geeknest.com]) | ||||||||||||
| 57 | }); | ||||||||||||
| 58 | |||||||||||||
| 59 | my @to = qw[casey chastity evelina casey_jr marshall]; | ||||||||||||
| 60 | my $rv = $bulk->send($message, $_) for @to; | ||||||||||||
| 61 | |||||||||||||
| 62 | =head1 DESCRIPTION | ||||||||||||
| 63 | |||||||||||||
| 64 | This module provides a very simple, very clean, very specific interface | ||||||||||||
| 65 | to multiple Email mailers. The goal of this software is to be small | ||||||||||||
| 66 | and simple, easy to use, and easy to extend. | ||||||||||||
| 67 | |||||||||||||
| 68 | =head2 Constructors | ||||||||||||
| 69 | |||||||||||||
| 70 | =over 4 | ||||||||||||
| 71 | |||||||||||||
| 72 | =item new | ||||||||||||
| 73 | |||||||||||||
| 74 | my $sender = Email::Send->new({ | ||||||||||||
| 75 | mailer => 'NNTP', | ||||||||||||
| 76 | mailer_args => [ Host => 'nntp.example.com' ], | ||||||||||||
| 77 | }); | ||||||||||||
| 78 | |||||||||||||
| 79 | Create a new mailer object. This method can take parameters for any of the data | ||||||||||||
| 80 | properties of this module. Those data properties, which have their own accessors, | ||||||||||||
| 81 | are listed under L<"Properties">. | ||||||||||||
| 82 | |||||||||||||
| 83 | =back | ||||||||||||
| 84 | |||||||||||||
| 85 | =head2 Properties | ||||||||||||
| 86 | |||||||||||||
| 87 | =over 4 | ||||||||||||
| 88 | |||||||||||||
| 89 | =item mailer | ||||||||||||
| 90 | |||||||||||||
| 91 | The mailing system you'd like to use for sending messages with this object. | ||||||||||||
| 92 | This is not defined by default. If you don't specify a mailer, all available | ||||||||||||
| 93 | plugins will be tried when the C<send> method is called until one succeeds. | ||||||||||||
| 94 | |||||||||||||
| 95 | =item mailer_args | ||||||||||||
| 96 | |||||||||||||
| 97 | Arguments passed into the mailing system you're using. | ||||||||||||
| 98 | |||||||||||||
| 99 | =item message_modifier | ||||||||||||
| 100 | |||||||||||||
| 101 | If defined, this callback is invoked every time the C<send> method is called | ||||||||||||
| 102 | on an object. The mailer object will be passed as the first argument. Second, | ||||||||||||
| 103 | the actual C<Email::Simple> object for a message will be passed. Finally, any | ||||||||||||
| 104 | additional arguments passed to C<send> will be passed to this method in the | ||||||||||||
| 105 | order they were received. | ||||||||||||
| 106 | |||||||||||||
| 107 | This is useful if you are sending in bulk. | ||||||||||||
| 108 | |||||||||||||
| 109 | =back | ||||||||||||
| 110 | |||||||||||||
| 111 | =cut | ||||||||||||
| 112 | |||||||||||||
| 113 | 19 | ▶ | sub new { | ||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 114 | 19 | my ($class, $args) = @_; | |||||||||||
| 115 | 19 | ▶ | $args->{mailer_args} ||= []; | ||||||||||
|
Condition100%2/2
$args->{"mailer_args"} ||= []
MC/DC100%1/1
$args->{"mailer_args"} ||= []
$args->{"mailer_args"}
Truth table100%2/2
A
A= $args->{"mailer_args"}
| |||||||||||||
| 116 | my %plugins = map { | ||||||||||||
| 117 | 19 | my ($short_name) = /^Email::Send::(.+)/; | |||||||||||
| 118 | 96 | ($short_name, $_); | |||||||||||
| 119 | } $class->plugins; | ||||||||||||
| 120 | 19 | $args->{_plugin_list} = \%plugins; | |||||||||||
| 121 | 19 | return bless $args => $class; | |||||||||||
| 122 | } | ||||||||||||
| 123 | |||||||||||||
| 124 | 14 | ▶ | BEGIN { | ||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=6.9
| |||||||||||||
| 125 | 14 | for my $field (qw(mailer mailer_args message_modifier _plugin_list)) { | |||||||||||
| 126 | 148 | ▶ | my $code = sub { | ||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
| |||||||||||||
| 127 | 148 | ▶ | return $_[0]->{$field} unless @_ > 1; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 128 | 4 | my $self = shift; | |||||||||||
| 129 | 4 | ▶ | $self->{$field} = (@_ == 1 ? $_[0] : [@_]); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 130 | 56 | }; | |||||||||||
| 131 | |||||||||||||
| 132 | 14 | ▶ | no strict 'refs'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 133 | 56 | *$field = $code; | |||||||||||
| 134 | } | ||||||||||||
| 135 | } | ||||||||||||
| 136 | |||||||||||||
| 137 | =head2 METHODS | ||||||||||||
| 138 | |||||||||||||
| 139 | =over 4 | ||||||||||||
| 140 | |||||||||||||
| 141 | =item send | ||||||||||||
| 142 | |||||||||||||
| 143 | my $result = $sender->send($message, @modifier_args); | ||||||||||||
| 144 | |||||||||||||
| 145 | Send a message using the predetermined mailer and mailer arguments. If you | ||||||||||||
| 146 | have defined a C<message_modifier> it will be called prior to sending. | ||||||||||||
| 147 | |||||||||||||
| 148 | The first argument you pass to send is an email message. It must be in some | ||||||||||||
| 149 | format that C<Email::Abstract> can understand. If you don't have | ||||||||||||
| 150 | C<Email::Abstract> installed then sending as plain text or an C<Email::Simple> | ||||||||||||
| 151 | object will do. | ||||||||||||
| 152 | |||||||||||||
| 153 | Any remaining arguments will be passed directly into your defined | ||||||||||||
| 154 | C<message_modifier>. | ||||||||||||
| 155 | |||||||||||||
| 156 | =cut | ||||||||||||
| 157 | |||||||||||||
| 158 | 19 | ▶ | sub send { | ||||||||||
|
Subroutine
send: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||
| 159 | 19 | ▶ | goto &_send_function unless eval { $_[0]->isa('Email::Send') }; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 160 | 18 | my ($self, $message, @args) = @_; | |||||||||||
| 161 | |||||||||||||
| 162 | 18 | my $simple = $self->_objectify_message($message); | |||||||||||
| 163 | 18 | ▶ | return failure "No message found." unless $simple; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 164 | |||||||||||||
| 165 | 16 | ▶ | $self->message_modifier->( | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 166 | $self, $simple, | ||||||||||||
| 167 | @args, | ||||||||||||
| 168 | ) if $self->message_modifier; | ||||||||||||
| 169 | |||||||||||||
| 170 | 16 | ▶ | if ( $self->mailer ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 171 | 14 | return $self->_send_it($self->mailer, $simple); | |||||||||||
| 172 | } | ||||||||||||
| 173 | |||||||||||||
| 174 | 2 | return $self->_try_all($simple); | |||||||||||
| 175 | } | ||||||||||||
| 176 | |||||||||||||
| 177 | =item all_mailers | ||||||||||||
| 178 | |||||||||||||
| 179 | my @available = $sender->all_mailers; | ||||||||||||
| 180 | |||||||||||||
| 181 | Returns a list of available mailers. These are mailers that are | ||||||||||||
| 182 | installed on your computer and register themselves as available. | ||||||||||||
| 183 | |||||||||||||
| 184 | =cut | ||||||||||||
| 185 | |||||||||||||
| 186 | 3 | ▶ | sub all_mailers { | ||||||||||
|
Subroutine
all_mailers: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 187 | 3 | my ($self) = @_; | |||||||||||
| 188 | 3 | my @mailers; | |||||||||||
| 189 | 3 | for ( keys %{$self->_plugin_list} ) { | |||||||||||
| 190 | 7 | ▶ | push @mailers, $_ if $self->mailer_available($_); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 191 | } | ||||||||||||
| 192 | 3 | return @mailers; | |||||||||||
| 193 | } | ||||||||||||
| 194 | |||||||||||||
| 195 | =item mailer_available | ||||||||||||
| 196 | |||||||||||||
| 197 | # is SMTP over SSL avaialble? | ||||||||||||
| 198 | $sender->mailer('SMTP') | ||||||||||||
| 199 | if $sender->mailer_available('SMTP', ssl => 1); | ||||||||||||
| 200 | |||||||||||||
| 201 | Given the name of a mailer, such as C<SMTP>, determine if it is | ||||||||||||
| 202 | available. Any additional arguments passed to this method are passed | ||||||||||||
| 203 | directly to the C<is_available> method of the mailer being queried. | ||||||||||||
| 204 | |||||||||||||
| 205 | =back | ||||||||||||
| 206 | |||||||||||||
| 207 | =cut | ||||||||||||
| 208 | |||||||||||||
| 209 | 25 | ▶ | sub mailer_available { | ||||||||||
|
Subroutine
mailer_available: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||
| 210 | 25 | my ($self, $mailer, @args) = @_; | |||||||||||
| 211 | |||||||||||||
| 212 | 25 | my $invocant = $self->_mailer_invocant($mailer); | |||||||||||
| 213 | |||||||||||||
| 214 | 25 | ▶ | return $invocant unless $invocant; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 215 | |||||||||||||
| 216 | 22 | ▶ | $invocant->can('is_available') | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 217 | or return failure "Mailer $mailer doesn't report availability."; | ||||||||||||
| 218 | |||||||||||||
| 219 | 21 | my $test = $invocant->is_available(@args); | |||||||||||
| 220 | 21 | ▶ | return $test unless $test; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 221 | 20 | return success; | |||||||||||
| 222 | } | ||||||||||||
| 223 | |||||||||||||
| 224 | 18 | ▶ | sub _objectify_message { | ||||||||||
|
Subroutine
_objectify_message: called CC=5 SCAR=16.1
| |||||||||||||
| 225 | 18 | my ($self, $message) = @_; | |||||||||||
| 226 | |||||||||||||
| 227 | 18 | ▶ | return undef unless defined $message; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 228 | 17 | ▶ | return $message if UNIVERSAL::isa($message, 'Email::Simple'); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 229 | 15 | ▶ | return Email::Simple->new($message) unless ref($message); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 230 | return Email::Abstract->cast($message => 'Email::Simple') | ||||||||||||
| 231 | 2 | ▶ | if eval { require Email::Abstract }; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 232 | 1 | return undef; | |||||||||||
| 233 | } | ||||||||||||
| 234 | |||||||||||||
| 235 | 43 | ▶ | sub _mailer_invocant { | ||||||||||
|
Subroutine
_mailer_invocant: called CC=4 SCAR=13.9
| |||||||||||||
| 236 | 43 | my ($self, $mailer) = @_; | |||||||||||
| 237 | |||||||||||||
| 238 | 43 | ▶ | return $mailer if Scalar::Util::blessed($mailer); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 239 | |||||||||||||
| 240 | # is the mailer a plugin given by short name? | ||||||||||||
| 241 | my $package = exists $self->_plugin_list->{$mailer} | ||||||||||||
| 242 | 39 | ▶ | ? $self->_plugin_list->{$mailer} | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 243 | : $mailer; | ||||||||||||
| 244 | |||||||||||||
| 245 | 39 | ▶ | eval "require $package" or return failure "$@"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 246 | |||||||||||||
| 247 | 36 | return $package; | |||||||||||
| 248 | } | ||||||||||||
| 249 | |||||||||||||
| 250 | 16 | ▶ | sub _send_it { | ||||||||||
|
Subroutine
_send_it: called CC=2 SCAR=6.9
| |||||||||||||
| 251 | 16 | my ($self, $mailer, $message) = @_; | |||||||||||
| 252 | 16 | my $test = $self->mailer_available($mailer); | |||||||||||
| 253 | 16 | ▶ | return $test unless $test; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 254 | |||||||||||||
| 255 | 14 | my $invocant = $self->_mailer_invocant($mailer); | |||||||||||
| 256 | |||||||||||||
| 257 | 14 | return $invocant->send($message, @{$self->mailer_args}); | |||||||||||
| 258 | } | ||||||||||||
| 259 | |||||||||||||
| 260 | 2 | ▶ | sub _try_all { | ||||||||||
|
Subroutine
_try_all: called CC=4 SCAR=13.9
| |||||||||||||
| 261 | 2 | my ($self, $simple) = @_; | |||||||||||
| 262 | 2 | foreach ( $self->all_mailers ) { | |||||||||||
| 263 | 2 | ▶ | next if $_ eq 'Email::Send::Test'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 264 | 2 | my $sent = $self->_send_it($_, $simple); | |||||||||||
| 265 | 2 | ▶ | return $sent if $sent; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 266 | } | ||||||||||||
| 267 | 1 | return failure "Unable to send message."; | |||||||||||
| 268 | } | ||||||||||||
| 269 | |||||||||||||
| 270 | # Classic Interface. | ||||||||||||
| 271 | |||||||||||||
| 272 | 13 | ▶ | sub import { | ||||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||
| 273 | 14 | ▶ | no strict 'refs'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 274 | 13 | *{(caller)[0] . '::send'} = __PACKAGE__->can('_send_function'); | |||||||||||
| 275 | } | ||||||||||||
| 276 | |||||||||||||
| 277 | 6 | ▶ | sub _send_function { | ||||||||||
|
Subroutine
_send_function: called CC=1 SCAR=0.0
| |||||||||||||
| 278 | 6 | my ($mailer, $message, @args) = @_; | |||||||||||
| 279 | 6 | __PACKAGE__->new({ | |||||||||||
| 280 | mailer => $mailer, | ||||||||||||
| 281 | mailer_args => \@args, | ||||||||||||
| 282 | })->send($message); | ||||||||||||
| 283 | } | ||||||||||||
| 284 | |||||||||||||
| 285 | 14 | 1; | |||||||||||
| 286 | |||||||||||||
| 287 | __END__ | ||||||||||||