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