1Email::Sender::Manual::UQsueirckCSotnatrrti(b3u)ted PerlEmDaoiclu:m:eSnetnadteiro:n:Manual::QuickStart(3)
2
3
4
6 Email::Sender::Manual::QuickStart - how to start using Email::Sender
7 right now
8
10 version 0.101760
11
13 Let's Send Some Mail!
14 No messing around, let's just send some mail.
15
16 use strict;
17 use Email::Sender::Simple qw(sendmail);
18 use Email::Simple;
19 use Email::Simple::Creator;
20
21 my $email = Email::Simple->create(
22 header => [
23 To => '"Xavier Q. Ample" <x.ample@example.com>',
24 From => '"Bob Fishman" <orz@example.mil>',
25 Subject => "don't forget to *enjoy the sauce*",
26 ],
27 body => "This message is short, but at least it's cheap.\n",
28 );
29
30 sendmail($email);
31
32 That's it. Your message goes out into the internet and tries to get
33 delivered to "x.ample@example.com".
34
35 In the example above, $email could be an Email::Simple object, a
36 MIME::Entity, a string containing an email message, or one of several
37 other types of input. If "Email::Abstract" can understand a value, it
38 can be passed to Email::Sender::Simple. Email::Sender::Simple tries to
39 make a good guess about how to send the message. It will usually try
40 to use the sendmail program on unix-like systems and to use SMTP on
41 Windows. You can specify a transport, if you need to, but normally
42 that shouldn't be an issue. (See "Picking a Transport", though, for
43 more information.)
44
45 Also note that we imported and used a "sendmail" routine in the example
46 above. This is exactly the same as saying:
47
48 Email::Sender::Simple->send($email);
49
50 ...but it's a lot easier to type. You can use either one.
51
52 envelope information
53
54 We didn't have to tell Email::Sender::Simple where to send the message.
55 If you don't specify recipients, it will use all the email addresses it
56 can find in the To and Cc headers by default. It will use
57 Email::Address to parse those fields. Similarly, if no sender is
58 specified, it will use the first address found in the From header.
59
60 In most email transmission systems, though, the headers are not by
61 necessity tied to the addresses used as the sender and recipients. For
62 example, your message header might say "From: mailing-list@example.com"
63 while your SMTP client says "MAIL FROM:<verp-1234@lists.example.com>".
64 This is a powerful feature, and is necessary for many email
65 application. Being able to set those distinctly is important, and
66 Email::Sender::Simple lets you do this:
67
68 sendmail($email, { to => [ $to_1, $to_2 ], from => $sender });
69
70 in case of error
71
72 When the message is sent successfully (at least on to its next hop),
73 "sendmail" will return a true value -- specifically, an
74 Email::Sender::Success object. This object only rarely has much use.
75 What's more useful is what happens if the message can't be sent.
76
77 If there is an error sending the message, an exception will be thrown.
78 It will be an object belonging to the class Email::Sender::Failure.
79 This object will have a "message" attribute describing the nature of
80 the failure. There are several specialized forms of failure, like
81 Email::Sender::Failure::Multi, which is thrown when more than one error
82 is encountered when trying to send. You don't need to know about these
83 to use Email::Sender::Simple, though. All you need to know is that
84 "sendmail" returns true on success and dies on failure.
85
86 If you'd rather not have to catch exceptions for failure to send mail,
87 you can use the "try_to_send" method, which can be imported as
88 "try_to_sendmail". This method will return just false on failure to
89 send mail.
90
91 For example:
92
93 Email::Sender::Simple->try_to_send($email, { ... });
94
95 use Email::Sender::Simple qw(try_to_sendmail);
96 try_to_sendmail($email, { ... });
97
98 Some Email::Sender transports can signal success if some, but not all,
99 recipients could be reached. Email::Sender::Simple does its best to
100 ensure that this never happens. When you are using
101 Email::Sender::Simple, mail should either be sent or not. Partial
102 success should never occur.
103
104 Picking a Transport
105 passing in your own transport
106
107 If Email::Sender::Simple doesn't pick the transport you want, or if you
108 have more specific needs, you can specify a transport in several ways.
109 The simplest is to build a transport object and pass it in. You can
110 read more about transports elsewhere. For now, we'll just assume that
111 you need to send mail via SMTP on an unusual port. You can send mail
112 like this:
113
114 my $transport = Email::Sender::Transport::SMTP->new({
115 host => 'smtp.example.com',
116 port => 2525,
117 });
118
119 sendmail($email, { transport => $transport });
120
121 Now, instead of guessing at what transport to use,
122 Email::Sender::Simple will use the one you provided. This transport
123 will have to be specified for each call to "sendmail", so you might
124 want to look at other options, which follow.
125
126 specifying transport in the environment
127
128 If you have a program that makes several calls to
129 Email::Sender::Simple, and you need to run this program using a
130 different mailserver, you can set environment variables to change the
131 default. For example:
132
133 $ export EMAIL_SENDER_TRANSPORT=SMTP
134 $ export EMAIL_SENDER_TRANSPORT_host=smtp.example.com
135 $ export EMAIL_SENDER_TRANSPORT_port=2525
136
137 $ perl your-program
138
139 It is important to note that if you have set the default transport by
140 using the environment, no subsequent "transport" args to "sendmail"
141 will be respected. If you set the default transport via the
142 environment, that's it. Everything will use that transport. (Also,
143 note that while we gave the host and port arguments above in lower
144 case, the casing of arguments in the environment is flattened to
145 support systems where environment variables are of a fixed case. So,
146 "EMAIL_SENDER_TRANSPORT_PORT" woudl also work.
147
148 This is extremely valuable behavior, as it allows you to audit every
149 message that would be sent by a program by running something like this:
150
151 $ export EMAIL_SENDER_TRANSPORT=Maildir
152 $ perl your-program
153
154 In that example, any message sent via Email::Sender::Simple would be
155 delivered to a maildir in the current directory.
156
157 subclassing to change the default transport
158
159 If you want to use a library that will behave like
160 Email::Sender::Simple but with a different default transport, you can
161 subclass Email::Sender::Simple and replace the
162 "build_default_transport" method.
163
164 Testing
165 Email::Sender::Simple makes it very, very easy to test code that sends
166 email. The simplest way is to do something like this:
167
168 use Test::More;
169 BEGIN { $ENV{EMAIL_SENDER_TRANSPORT} = 'Test' }
170 use YourCode;
171
172 YourCode->run;
173
174 my @deliveries = Email::Sender::Simple->default_transport->deliveries;
175
176 Now you've got an array containing every delivery performed through
177 Email::Sender::Simple, in order. Because you set the transport via the
178 environment, no other code will be able to force a different transport.
179
180 When testing code that forks, Email::Sender::Transport::SQLite can be
181 used to allow every child process to deliver to a single, easy to
182 inspect destination database.
183
185 This is awesome! Where can I learn more?
186 Have a look at Email::Sender::Manual, where all the manual's documents
187 are listed. You can also look at the documentation for
188 Email::Sender::Simple and the various Email::Sender::Transport classes.
189
191 Ricardo Signes <rjbs@cpan.org>
192
194 This software is copyright (c) 2010 by Ricardo Signes.
195
196 This is free software; you can redistribute it and/or modify it under
197 the same terms as the Perl 5 programming language system itself.
198
199
200
201perl v5.12.1 2010-06-2E5mail::Sender::Manual::QuickStart(3)