-
Notifications
You must be signed in to change notification settings - Fork 0
/
README
179 lines (133 loc) · 5.63 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
NAME
Email::Send - Simply Sending Email
VERSION
2.198
SYNOPSIS
use Email::Send;
my $message = <<'__MESSAGE__';
To: recipient@example.com
From: sender@example.com
Subject: Hello there folks
How are you? Enjoy!
__MESSAGE__
my $sender = Email::Send->new({mailer => 'SMTP'});
$sender->mailer_args([Host => 'smtp.example.com']);
$sender->send($message);
# more complex
my $bulk = Email::Send->new;
for ( qw[SMTP Sendmail Qmail] ) {
$bulk->mailer($_) and last if $bulk->mailer_available($_);
}
$bulk->message_modifier(sub {
my ($sender, $message, $to) = @_;
$message->header_set(To => qq[$to\@geeknest.com])
});
my @to = qw[casey chastity evelina casey_jr marshall];
my $rv = $bulk->send($message, $_) for @to;
DESCRIPTION
This module provides a very simple, very clean, very specific interface
to multiple Email mailers. The goal of this software is to be small and
simple, easy to use, and easy to extend.
Constructors
new()
my $mailer = Email::Send->new({
mailer => 'NNTP',
mailer_args => [ Host => 'nntp.example.com' ],
});
Create a new mailer object. This method can take parameters for any
of the data properties of this module. Those data properties, which
have their own accessors, are listed under "Properties".
Properties
mailer
The mailing system you'd like to use for sending messages with this
object. This is not defined by default. If you don't specify a
mailer, all available plugins will be tried when the "send()" method
is called until one succeeds.
mailer_args
Arguments passed into the mailing system you're using.
message_modifier
If defined, this callback is invoked every time the "send()" method
is called on an object. The mailer object will be passed as the
first argument. Second, the actual "Email::Simple" object for a
message will be passed. Finally, any additional arguments passed to
"send()" will be passed to this method in the order they were
recieved.
This is useful if you are sending in bulk.
Methods
send()
my $result = $mailer->send($message, @modifier_args);
Send a message using the predetermined mailer and mailer arguments.
If you have defined a "message_modifier" it will be called prior to
sending.
The first argument you pass to send is an email message. It must be
in some format that "Email::Abstract" can understand. If you don't
have "Email::Abstract" installed then sending as plain text or an
"Email::Simple" object will do.
Any remaining arguments will be passed directly into your defined
"message_modifier".
all_mailers()
my @available = $mailer->all_mailers;
Returns a list of availabe mailers. These are mailers that are
installed on your computer and register themselves as available.
mailer_available()
# is SMTP over SSL avaialble?
$mailer->mailer('SMTP')
if $mailer->mailer_available('SMTP', ssl => 1);
Given the name of a mailer, such as "SMTP", determine if it is
available. Any additional arguments passed to this method are passed
directly to the "is_available()" method of the mailer being queried.
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 "send", name your mailer under the "Email::Send" namespace. If
you don't, the full name will have to be used. A mailer only needs to
implement a single function, "send". It will be called from
"Email::Send" exactly like this.
Your::Sending::Package::send($message, @args);
$message is an Email::Simple object, @args are the extra arguments
passed into "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 ($message, @args);
use LWP::UserAgent;
if ( @args ) {
my ($URL, $FIELD) = @args;
$AGENT = LWP::UserAgent->new;
}
return failure "Can't send to URL if no URL and field are named"
unless $URL && $FIELD;
$AGENT->post($URL => { $FIELD => $message->as_string });
return success;
}
1;
This example will keep a UserAgent singleton unless new arguments are
passed to "send". It is used by calling "Email::Send::send".
my $mailer = Email::Send->new({ mailer => 'HTTP::Post' });
$mailer->mailer_args([ 'http://example.com/incoming', 'message' ]);
$mailer->send($message);
$mailer->send($message2); # uses saved $URL and $FIELD
SEE ALSO
Email::Simple, Email::Abstract, Email::Send::IO, Email::Send::NNTP,
Email::Send::Qmail, Email::Send::SMTP, Email::Send::Sendmail, perl.
AUTHOR
Casey West, <casey@geeknest.com>.
COPYRIGHT
Copyright (c) 2005 Casey West. All rights reserved.
This module is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.