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 1.300031
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" would 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
184 Hey, where's my Bcc support?
185 A common question is "Why doesn't Email::Sender::Simple automatically
186 respect my Bcc header?" This is often combined with, "Here is a patch
187 to 'fix' it." This is not a bug or oversight. Bcc is being ignored
188 intentionally for now because simply adding the Bcc addresses to the
189 message recipients would not produce the usually-desired behavior.
190
191 For example, here is a set of headers:
192
193 From: sender@example.com
194 To: to_rcpt@example.com
195 Cc: cc_rcpt@example.com
196 Bcc: the_boss@example.com
197
198 In this case, we'd expect the message to be delivered to three people:
199 to_rcpt, cc_rcpt, and the_boss. This is why it's often suggested that
200 the Bcc header should be a source for envelope recipients. In fact,
201 though, a message with a Bcc header should probably be delivered only
202 to the Bcc recipients. The "B" in Bcc means "blind." The other
203 recipients should not see who has been Bcc'd. This means you want to
204 send two messages: one to to_rcpt and cc_rcpt, with no Bcc header
205 present; and another to the_boss only, with the Bcc header. If you
206 just pick up Bcc addresses as recipients, everyone will see who was
207 Bcc'd.
208
209 Email::Sender::Simple promises to send messages atomically. That is:
210 it won't deliver to only some of the recipients, and not to others.
211 That means it can't automatically detect the Bcc header and make two
212 deliveries. There would be a possibility for the second to fail after
213 the first succeeded, which would break the promise of a pure failure or
214 success.
215
216 The other strategy for dealing with Bcc is to remove the Bcc header
217 from the message and then inject the message with an envelope including
218 the Bcc addresses. The envelope information will not be visible to the
219 final recipients, so this is safe. Unfortunately, this requires
220 modifying the message, and Email::Sender::Simple should not be altering
221 the mutable email object passed to it. There is no "clone" method on
222 Email::Abstract, so it cannot just build a clone and modify that,
223 either. When such a method exists, Bcc handling may be possible.
224
225 Example Bcc Handling
226
227 If you want to support the Bcc header now, it is up to you to deal with
228 how you want to munge the mail and inject the (possibly) munged copies
229 into your outbound mailflow. It is not reasonable to suggest that
230 Email::Sender::Simple do this job.
231
232 Example 1: Explicitly set the envelope recipients for Bcc recipients
233
234 Create the email without a Bcc header, send it to the Bcc users
235 explicitly and then send it to the To/Cc users implicitly.
236
237 my $message = create_email_mime_msg; # <- whatever you do to get the message
238
239 $message->header_set('bcc'); # delete the Bcc header before sending
240 sendmail($message, { to => $rcpt }); # send to explicit Bcc address
241 sendmail($message); # and then send as normal
242
243 Example 2: Explicitly set the envelope recipients for all recipients
244
245 You can make a single call to "sendmail" by pulling all the recipient
246 addresses from the headers yourself and specifying all the envelope
247 recipients once. Again, delete the Bcc header before the message is
248 sent.
249
251 This is awesome! Where can I learn more?
252 Have a look at Email::Sender::Manual, where all the manual's documents
253 are listed. You can also look at the documentation for
254 Email::Sender::Simple and the various Email::Sender::Transport classes.
255
257 Ricardo Signes <rjbs@cpan.org>
258
260 This software is copyright (c) 2017 by Ricardo Signes.
261
262 This is free software; you can redistribute it and/or modify it under
263 the same terms as the Perl 5 programming language system itself.
264
265
266
267perl v5.28.0 2017-04-0E4mail::Sender::Manual::QuickStart(3)