| 1 | package Email::Send; | ||||||||||||
| 2 | # $Id: Send.pm,v 1.13 2005/05/11 03:14:14 cwest Exp $ | ||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | |||||||||||||
| 5 | 2 | ▶ | use vars qw[$VERSION]; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 2 | $VERSION = '1.99_01'; | |||||||||||
| 7 | |||||||||||||
| 8 | 2 | ▶ | use base qw[Class::Accessor::Fast]; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 2 | ▶ | use Email::Simple; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 2 | ▶ | use Module::Pluggable search_path => 'Email::Send'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 2 | ▶ | use UNIVERSAL::require; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 2 | ▶ | use Return::Value; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | |||||||||||||
| 14 | =head1 NAME | ||||||||||||
| 15 | |||||||||||||
| 16 | Email::Send - Simply Sending Email | ||||||||||||
| 17 | |||||||||||||
| 18 | =head1 SYNOPSIS | ||||||||||||
| 19 | |||||||||||||
| 20 | use Email::Send; | ||||||||||||
| 21 | |||||||||||||
| 22 | my $message = <<'__MESSAGE__'; | ||||||||||||
| 23 | To: recipient@example.com | ||||||||||||
| 24 | From: sender@example.com | ||||||||||||
| 25 | Subject: Hello there folks | ||||||||||||
| 26 | |||||||||||||
| 27 | How are you? Enjoy! | ||||||||||||
| 28 | __MESSAGE__ | ||||||||||||
| 29 | |||||||||||||
| 30 | my $sender = Email::Send->new({mailer => 'SMTP'}); | ||||||||||||
| 31 | $sender->mailer_args([Host => 'smtp.example.com']); | ||||||||||||
| 32 | $sender->send($message); | ||||||||||||
| 33 | |||||||||||||
| 34 | # more complex | ||||||||||||
| 35 | my $bulk = Email::Send->new; | ||||||||||||
| 36 | for ( qw[SMTP Sendmail Qmail] ) { | ||||||||||||
| 37 | $bulk->mailer($_) and last if $bulk->mailer_available($_); | ||||||||||||
| 38 | } | ||||||||||||
| 39 | |||||||||||||
| 40 | $bulk->message_modifier(sub { | ||||||||||||
| 41 | my ($sender, $message, $to) = @_; | ||||||||||||
| 42 | $message->header_set(To => qq[$to\@geeknest.com]) | ||||||||||||
| 43 | }); | ||||||||||||
| 44 | |||||||||||||
| 45 | my @to = qw[casey chastity evelina casey_jr marshall]; | ||||||||||||
| 46 | my $rv = $bulk->send($message, $_) for @to; | ||||||||||||
| 47 | |||||||||||||
| 48 | =head1 DESCRIPTION | ||||||||||||
| 49 | |||||||||||||
| 50 | This module provides a very simple, very clean, very specific interface | ||||||||||||
| 51 | to multiple Email mailers. The goal if this software is to be small | ||||||||||||
| 52 | and simple, easy to use, and easy to extend. | ||||||||||||
| 53 | |||||||||||||
| 54 | =head2 Constructors | ||||||||||||
| 55 | |||||||||||||
| 56 | =over 4 | ||||||||||||
| 57 | |||||||||||||
| 58 | =item new() | ||||||||||||
| 59 | |||||||||||||
| 60 | my $mailer = Email::Send->new({ | ||||||||||||
| 61 | mailer => 'NNTP', | ||||||||||||
| 62 | mailer_args => [ Host => 'nntp.example.com' ], | ||||||||||||
| 63 | }); | ||||||||||||
| 64 | |||||||||||||
| 65 | Create a new mailer object. This method can take parameters for any of the data | ||||||||||||
| 66 | properties of this module. Those data properties, which have their own accessors, | ||||||||||||
| 67 | are listed under L<"Properties">. | ||||||||||||
| 68 | |||||||||||||
| 69 | =back | ||||||||||||
| 70 | |||||||||||||
| 71 | =head2 Properties | ||||||||||||
| 72 | |||||||||||||
| 73 | =over 4 | ||||||||||||
| 74 | |||||||||||||
| 75 | =item mailer | ||||||||||||
| 76 | |||||||||||||
| 77 | The mailing system you'd like to use for sending messages with this object. | ||||||||||||
| 78 | This is not defined by default. If you don't specify a mailer, all available | ||||||||||||
| 79 | plugins will be tried when the C<send()> method is called until one succeeds. | ||||||||||||
| 80 | |||||||||||||
| 81 | =item mailer_args | ||||||||||||
| 82 | |||||||||||||
| 83 | Arguments passed into the mailing system you're using. | ||||||||||||
| 84 | |||||||||||||
| 85 | =item message_modifier | ||||||||||||
| 86 | |||||||||||||
| 87 | If defined, this callback is invoked every time the C<send()> method is called | ||||||||||||
| 88 | on an object. The mailer object will be passed as the first argument. Second, | ||||||||||||
| 89 | the actual C<Email::Simple> object for a message will be passed. Finally, any | ||||||||||||
| 90 | additional arguments passed to C<send()> will be passed to this method in the | ||||||||||||
| 91 | order they were recieved. | ||||||||||||
| 92 | |||||||||||||
| 93 | This is useful if you are sending in bulk. | ||||||||||||
| 94 | |||||||||||||
| 95 | =back | ||||||||||||
| 96 | |||||||||||||
| 97 | =cut | ||||||||||||
| 98 | |||||||||||||
| 99 | sub new { | ||||||||||||
| 100 | 1 | ▶ | my ($class, $args) = @_; | ||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||
| 101 | 1 | ▶ | $args->{mailer_args} ||= []; | ||||||||||
|
Condition50%1/2
$args->{"mailer_args"} ||= []
MC/DC0%0/1
$args->{"mailer_args"} ||= []
$args->{"mailer_args"}
Truth table50%1/2
A
A= $args->{"mailer_args"}
| |||||||||||||
| 102 | my %plugins = map { | ||||||||||||
| 103 | 1 | my ($short_name) = /^Email::Send::(.+)/; | |||||||||||
| 104 | 5 | ($short_name, $_); | |||||||||||
| 105 | } $class->plugins; | ||||||||||||
| 106 | 1 | $args->{_plugin_list} = \%plugins; | |||||||||||
| 107 | 1 | return $class->SUPER::new($args); | |||||||||||
| 108 | } | ||||||||||||
| 109 | 2 | __PACKAGE__->mk_accessors(qw[mailer mailer_args message_modifier _plugin_list]); | |||||||||||
| 110 | |||||||||||||
| 111 | =head2 Methods | ||||||||||||
| 112 | |||||||||||||
| 113 | =over 4 | ||||||||||||
| 114 | |||||||||||||
| 115 | =item send() | ||||||||||||
| 116 | |||||||||||||
| 117 | my $result = $mailer->send($message, @modifier_args); | ||||||||||||
| 118 | |||||||||||||
| 119 | Send a message using the predetermined mailer and mailer arguments. If you | ||||||||||||
| 120 | have defined a C<message_modifier> it will be called prior to sending. | ||||||||||||
| 121 | |||||||||||||
| 122 | The first argument you pass to send is an email message. It must be in some | ||||||||||||
| 123 | format that C<Email::Abstract> can understand. If you don't have C<Email::Abstract> | ||||||||||||
| 124 | installed then sending as plain text or an C<Email::Simple> object will do. | ||||||||||||
| 125 | |||||||||||||
| 126 | Any remaining arguments will be passed directly into your defined | ||||||||||||
| 127 | C<message_modifier>. | ||||||||||||
| 128 | |||||||||||||
| 129 | =cut | ||||||||||||
| 130 | |||||||||||||
| 131 | sub send { | ||||||||||||
| 132 | 0 | ▶ | my ($self, $message, @args) = @_; | ||||||||||
|
Subroutine
send: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||
| 133 | 0 | my $simple = $self->_objectify_message($message); | |||||||||||
| 134 | 0 | ▶ | return failure "No message found." unless $simple; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 135 | |||||||||||||
| 136 | 0 | ▶ | $self->message_modifier( | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 137 | $self, $simple, | ||||||||||||
| 138 | @args, | ||||||||||||
| 139 | ) if $self->message_modifier; | ||||||||||||
| 140 | |||||||||||||
| 141 | 0 | ▶ | if ( $self->mailer ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 142 | 0 | return $self->_send_it($self->mailer, $simple); | |||||||||||
| 143 | } | ||||||||||||
| 144 | |||||||||||||
| 145 | 0 | return $self->_try_all($simple); | |||||||||||
| 146 | } | ||||||||||||
| 147 | |||||||||||||
| 148 | =item all_mailers() | ||||||||||||
| 149 | |||||||||||||
| 150 | my @available = $mailer->all_mailers; | ||||||||||||
| 151 | |||||||||||||
| 152 | Returns a list of availabe mailers. These are mailers that are | ||||||||||||
| 153 | installed on your computer and register themselves as available. | ||||||||||||
| 154 | |||||||||||||
| 155 | =cut | ||||||||||||
| 156 | |||||||||||||
| 157 | sub all_mailers { | ||||||||||||
| 158 | 0 | ▶ | my ($self) = @_; | ||||||||||
|
Subroutine
all_mailers: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||
| 159 | 0 | my @mailers; | |||||||||||
| 160 | 0 | for ( keys %{$self->_plugin_list} ) { | |||||||||||
| 161 | 0 | ▶ | push @mailers, $_ if $self->mailer_available($_); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 162 | } | ||||||||||||
| 163 | 0 | return @mailers; | |||||||||||
| 164 | } | ||||||||||||
| 165 | |||||||||||||
| 166 | =item mailer_available() | ||||||||||||
| 167 | |||||||||||||
| 168 | # is SMTP over SSL avaialble? | ||||||||||||
| 169 | $mailer->mailer('SMTP') | ||||||||||||
| 170 | if $mailer->mailer_available('SMTP', ssl => 1); | ||||||||||||
| 171 | |||||||||||||
| 172 | Given the name of a mailer, such as C<SMTP>, determine if it is | ||||||||||||
| 173 | available. Any additional arguments passed to this method are passed | ||||||||||||
| 174 | directly to the C<is_available()> method of the mailer being queried. | ||||||||||||
| 175 | |||||||||||||
| 176 | =back | ||||||||||||
| 177 | |||||||||||||
| 178 | =cut | ||||||||||||
| 179 | |||||||||||||
| 180 | sub mailer_available { | ||||||||||||
| 181 | 0 | ▶ | my ($self, $mailer, @args) = @_; | ||||||||||
|
Subroutine
mailer_available: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||
| 182 | 0 | ▶ | if ( my $package = $self->_plugin_list->{$mailer} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 183 | 0 | ▶ | $package->require or return failure; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 184 | 0 | ▶ | $package->can('is_available') | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 185 | or return failure "Mailer $mailer doesn't report availability."; | ||||||||||||
| 186 | 0 | my $test = $package->is_available(@args); | |||||||||||
| 187 | 0 | ▶ | return $test unless $test; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 188 | 0 | return success; | |||||||||||
| 189 | } | ||||||||||||
| 190 | 0 | return failure "Mailer $mailer not found."; | |||||||||||
| 191 | } | ||||||||||||
| 192 | |||||||||||||
| 193 | sub _objectify_message { | ||||||||||||
| 194 | 0 | ▶ | my ($self, $message) = @_; | ||||||||||
|
Subroutine
_objectify_message: not called CC=4 SCAR=30.0
| |||||||||||||
| 195 | |||||||||||||
| 196 | 0 | ▶ | return $message if UNIVERSAL::isa($message, 'Email::Simple'); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 197 | 0 | ▶ | return Email::Simple->new($message) unless ref($message); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 198 | 0 | ▶ | return Email::Abstract->cast($message => 'Email::Simple') | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 199 | if Email::Abstract->require; | ||||||||||||
| 200 | 0 | return undef; | |||||||||||
| 201 | } | ||||||||||||
| 202 | |||||||||||||
| 203 | sub _send_it { | ||||||||||||
| 204 | 0 | ▶ | my ($self, $mailer, $message) = @_; | ||||||||||
|
Subroutine
_send_it: not called CC=3 SCAR=24.8
| |||||||||||||
| 205 | 0 | my $test = $self->mailer_available($mailer); | |||||||||||
| 206 | 0 | ▶ | return $test unless $test; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 207 | |||||||||||||
| 208 | 0 | my $package = $self->_plugin_list->{$mailer}; | |||||||||||
| 209 | 0 | ▶ | $package->require or return failure; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 210 | 0 | return $package->send($message, @{$self->mailer_args}); | |||||||||||
| 211 | } | ||||||||||||
| 212 | |||||||||||||
| 213 | sub _try_all { | ||||||||||||
| 214 | 0 | ▶ | my ($self, $simple) = @_; | ||||||||||
|
Subroutine
_try_all: not called CC=3 SCAR=24.8
| |||||||||||||
| 215 | 0 | foreach ( $self->all_mailers ) { | |||||||||||
| 216 | 0 | my $sent = $self->_send_it($_, $simple); | |||||||||||
| 217 | 0 | ▶ | return $sent if $sent; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 218 | } | ||||||||||||
| 219 | 0 | return failure "Unable to send message."; | |||||||||||
| 220 | } | ||||||||||||
| 221 | |||||||||||||
| 222 | 2 | 1; | |||||||||||
| 223 | |||||||||||||
| 224 | __END__ | ||||||||||||