File Coverage

blib/lib/Email/Send.pm
Criterion Covered Total %
statement 86 86 100.0
branch 37 38 97.3
condition 2 2 100.0
subroutine 20 20 100.0
pod 4 4 100.0
total 149 150 99.3


line stmt bran cond sub pod time code
1             package Email::Send;
2 13     13   363340 use strict;
  13         31  
  13         482  
3              
4 13     13   71 use vars qw[$VERSION];
  13         27  
  13         919  
5             $VERSION = '2.199';
6              
7 13     13   12315 use Email::Simple 1.92;
  13         86352  
  13         606  
8             use Module::Pluggable 2.97
9 13         109 search_path => 'Email::Send',
10 13     13   12905 except => $Email::Send::__plugin_exclusion;
  13         156372  
11             BEGIN {
12 13     13   1734 local $Return::Value::NO_CLUCK = 1;
13 13         13065 require Return::Value;
14 13         55295 Return::Value->import;
15             }
16 13     13   120 use Scalar::Util 1.02 ();
  13         1115  
  13         2966  
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 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 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 method is called
101             on an object. The mailer object will be passed as the first argument. Second,
102             the actual C object for a message will be passed. Finally, any
103             additional arguments passed to C 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     19 1 142938 my ($class, $args) = @_;
114 19   100     164 $args->{mailer_args} ||= [];
115 96         76610 my %plugins = map {
116 19         152 my ($short_name) = /^Email::Send::(.+)/;
117 96         305 ($short_name, $_);
118             } $class->plugins;
119 19         88 $args->{_plugin_list} = \%plugins;
120 19         106 return bless $args => $class;
121             }
122              
123             BEGIN {
124 13     13   40 for my $field (qw(mailer mailer_args message_modifier _plugin_list)) {
125             my $code = sub {
126 148 100   148   2225 return $_[0]->{$field} unless @_ > 1;
127 4         8 my $self = shift;
128 4 50       26 $self->{$field} = (@_ == 1 ? $_[0] : [@_]);
129 52         180 };
130              
131 13     13   76 no strict 'refs';
  13         25  
  13         530  
132 52         10345 *$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 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 can understand. If you don't have
149             C installed then sending as plain text or an C
150             object will do.
151              
152             Any remaining arguments will be passed directly into your defined
153             C.
154              
155             =cut
156              
157             sub send {
158 19 100   19 1 2872 goto &_send_function unless eval { $_[0]->isa('Email::Send') };
  19         215  
159 18         56 my ($self, $message, @args) = @_;
160              
161 18         73 my $simple = $self->_objectify_message($message);
162 18 100       2847 return failure "No message found." unless $simple;
163              
164 16 100       60 $self->message_modifier->(
165             $self, $simple,
166             @args,
167             ) if $self->message_modifier;
168              
169 16 100       166 if ( $self->mailer ) {
170 14         49 return $self->_send_it($self->mailer, $simple);
171             }
172              
173 2         6 return $self->_try_all($simple);
174             }
175              
176             =item all_mailers
177              
178             my @available = $sender->all_mailers;
179              
180             Returns a list of availabe 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     3 1 8 my ($self) = @_;
187 3         3 my @mailers;
188 3         5 for ( keys %{$self->_plugin_list} ) {
  3         8  
189 7 100       247 push @mailers, $_ if $self->mailer_available($_);
190             }
191 3         224 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, determine if it is
201             available. Any additional arguments passed to this method are passed
202             directly to the C method of the mailer being queried.
203              
204             =back
205              
206             =cut
207              
208             sub mailer_available {
209 25     25 1 61 my ($self, $mailer, @args) = @_;
210              
211 25         79 my $invocant = $self->_mailer_invocant($mailer);
212              
213 25 100       226 return $invocant unless $invocant;
214              
215 22 100       285 $invocant->can('is_available')
216             or return failure "Mailer $mailer doesn't report availability.";
217              
218 21         120 my $test = $invocant->is_available(@args);
219 21 100       339 return $test unless $test;
220 20         258 return success;
221             }
222              
223             sub _objectify_message {
224 18     18   40 my ($self, $message) = @_;
225              
226 18 100       104 return undef unless defined $message;
227 17 100       144 return $message if UNIVERSAL::isa($message, 'Email::Simple');
228 15 100       144 return Email::Simple->new($message) unless ref($message);
229             return Email::Abstract->cast($message => 'Email::Simple')
230 2 100       5 if eval { require Email::Abstract };
  2         394  
231 1         23 return undef;
232             }
233              
234             sub _mailer_invocant {
235 43     43   556 my ($self, $mailer) = @_;
236              
237 43 100       206 return $mailer if Scalar::Util::blessed($mailer);
238              
239             # is the mailer a plugin given by short name?
240 39 100       121 my $package = exists $self->_plugin_list->{$mailer}
241             ? $self->_plugin_list->{$mailer}
242             : $mailer;
243              
244 39 100       2992 eval "require $package" or return failure "$@";
245              
246 36         1397 return $package;
247             }
248              
249             sub _send_it {
250 16     16   38 my ($self, $mailer, $message) = @_;
251 16         59 my $test = $self->mailer_available($mailer);
252 16 100       791 return $test unless $test;
253              
254 14         481 my $invocant = $self->_mailer_invocant($mailer);
255              
256 14         35 return $invocant->send($message, @{$self->mailer_args});
  14         55  
257             }
258              
259             sub _try_all {
260 2     2   3 my ($self, $simple) = @_;
261 2         6 foreach ( $self->all_mailers ) {
262 2         11 my $sent = $self->_send_it($_, $simple);
263 2 100       58 return $sent if $sent;
264             }
265 1         61 return failure "Unable to send message.";
266             }
267              
268             # Classic Interface.
269              
270             sub import {
271 13     13   81 no strict 'refs';
  13         31  
  13         1552  
272 13     13   1491 *{(caller)[0] . '::send'} = __PACKAGE__->can('_send_function');
  13         2424  
273             }
274              
275             sub _send_function {
276 6     6   155169 my ($mailer, $message, @args) = @_;
277 6         72 __PACKAGE__->new({
278             mailer => $mailer,
279             mailer_args => \@args,
280             })->send($message);
281             }
282              
283             1;
284              
285             __END__