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.300035
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::MIME;
19
20 # You can make an email object in lots of ways. Pick one you like!
21 my $email = Email::MIME->create(...);
22
23 sendmail($email);
24
25 That's it. Your message goes out into the internet and tries to get
26 delivered to "x.ample@example.com".
27
28 In the example above, $email could be an Email::MIME object, a
29 MIME::Entity, a string containing an email message, or one of several
30 other types of input. If "Email::Abstract" can understand a value, it
31 can be passed to Email::Sender::Simple. Email::Sender::Simple tries to
32 make a good guess about how to send the message. It will usually try
33 to use the sendmail program on unix-like systems and to use SMTP on
34 Windows. You can specify a transport, if you need to, but normally
35 that shouldn't be an issue. (See "Picking a Transport", though, for
36 more information.)
37
38 Also note that we imported and used a "sendmail" routine in the example
39 above. This is exactly the same as saying:
40
41 Email::Sender::Simple->send($email);
42
43 ...but it's a lot easier to type. You can use either one.
44
45 envelope information
46
47 We didn't have to tell Email::Sender::Simple where to send the message.
48 If you don't specify recipients, it will use all the email addresses it
49 can find in the To and Cc headers by default. It will use
50 Email::Address to parse those fields. Similarly, if no sender is
51 specified, it will use the first address found in the From header.
52
53 In most email transmission systems, though, the headers are not by
54 necessity tied to the addresses used as the sender and recipients. For
55 example, your message header might say "From: mailing-list@example.com"
56 while your SMTP client says "MAIL FROM:<verp-1234@lists.example.com>".
57 This is a powerful feature, and is necessary for many email
58 application. Being able to set those distinctly is important, and
59 Email::Sender::Simple lets you do this:
60
61 sendmail($email, { to => [ $to_1, $to_2 ], from => $sender });
62
63 in case of error
64
65 When the message is sent successfully (at least on to its next hop),
66 "sendmail" will return a true value -- specifically, an
67 Email::Sender::Success object. This object only rarely has much use.
68 What's more useful is what happens if the message can't be sent.
69
70 If there is an error sending the message, an exception will be thrown.
71 It will be an object belonging to the class Email::Sender::Failure.
72 This object will have a "message" attribute describing the nature of
73 the failure. There are several specialized forms of failure, like
74 Email::Sender::Failure::Multi, which is thrown when more than one error
75 is encountered when trying to send. You don't need to know about these
76 to use Email::Sender::Simple, though. All you need to know is that
77 "sendmail" returns true on success and dies on failure.
78
79 If you'd rather not have to catch exceptions for failure to send mail,
80 you can use the "try_to_send" method, which can be imported as
81 "try_to_sendmail". This method will return just false on failure to
82 send mail.
83
84 For example:
85
86 Email::Sender::Simple->try_to_send($email, { ... });
87
88 use Email::Sender::Simple qw(try_to_sendmail);
89 try_to_sendmail($email, { ... });
90
91 Some Email::Sender transports can signal success if some, but not all,
92 recipients could be reached. Email::Sender::Simple does its best to
93 ensure that this never happens. When you are using
94 Email::Sender::Simple, mail should either be sent or not. Partial
95 success should never occur.
96
97 Picking a Transport
98 passing in your own transport
99
100 If Email::Sender::Simple doesn't pick the transport you want, or if you
101 have more specific needs, you can specify a transport in several ways.
102 The simplest is to build a transport object and pass it in. You can
103 read more about transports elsewhere. For now, we'll just assume that
104 you need to send mail via SMTP on an unusual port. You can send mail
105 like this:
106
107 my $transport = Email::Sender::Transport::SMTP->new({
108 host => 'smtp.example.com',
109 port => 2525,
110 });
111
112 sendmail($email, { transport => $transport });
113
114 Now, instead of guessing at what transport to use,
115 Email::Sender::Simple will use the one you provided. This transport
116 will have to be specified for each call to "sendmail", so you might
117 want to look at other options, which follow.
118
119 specifying transport in the environment
120
121 If you have a program that makes several calls to
122 Email::Sender::Simple, and you need to run this program using a
123 different mailserver, you can set environment variables to change the
124 default. For example:
125
126 $ export EMAIL_SENDER_TRANSPORT=SMTP
127 $ export EMAIL_SENDER_TRANSPORT_host=smtp.example.com
128 $ export EMAIL_SENDER_TRANSPORT_port=2525
129
130 $ perl your-program
131
132 It is important to note that if you have set the default transport by
133 using the environment, no subsequent "transport" args to "sendmail"
134 will be respected. If you set the default transport via the
135 environment, that's it. Everything will use that transport. (Also,
136 note that while we gave the host and port arguments above in lower
137 case, the casing of arguments in the environment is flattened to
138 support systems where environment variables are of a fixed case. So,
139 "EMAIL_SENDER_TRANSPORT_PORT" would also work.
140
141 This is extremely valuable behavior, as it allows you to audit every
142 message that would be sent by a program by running something like this:
143
144 $ export EMAIL_SENDER_TRANSPORT=Maildir
145 $ perl your-program
146
147 In that example, any message sent via Email::Sender::Simple would be
148 delivered to a maildir in the current directory.
149
150 subclassing to change the default transport
151
152 If you want to use a library that will behave like
153 Email::Sender::Simple but with a different default transport, you can
154 subclass Email::Sender::Simple and replace the
155 "build_default_transport" method.
156
157 Testing
158 Email::Sender::Simple makes it very, very easy to test code that sends
159 email. The simplest way is to do something like this:
160
161 use Test::More;
162 BEGIN { $ENV{EMAIL_SENDER_TRANSPORT} = 'Test' }
163 use YourCode;
164
165 YourCode->run;
166
167 my @deliveries = Email::Sender::Simple->default_transport->deliveries;
168
169 Now you've got an array containing every delivery performed through
170 Email::Sender::Simple, in order. Because you set the transport via the
171 environment, no other code will be able to force a different transport.
172
173 When testing code that forks, Email::Sender::Transport::SQLite can be
174 used to allow every child process to deliver to a single, easy to
175 inspect destination database.
176
177 Hey, where's my Bcc support?
178 A common question is "Why doesn't Email::Sender::Simple automatically
179 respect my Bcc header?" This is often combined with, "Here is a patch
180 to 'fix' it." This is not a bug or oversight. Bcc is being ignored
181 intentionally for now because simply adding the Bcc addresses to the
182 message recipients would not produce the usually-desired behavior.
183
184 For example, here is a set of headers:
185
186 From: sender@example.com
187 To: to_rcpt@example.com
188 Cc: cc_rcpt@example.com
189 Bcc: the_boss@example.com
190
191 In this case, we'd expect the message to be delivered to three people:
192 to_rcpt, cc_rcpt, and the_boss. This is why it's often suggested that
193 the Bcc header should be a source for envelope recipients. In fact,
194 though, a message with a Bcc header should probably be delivered only
195 to the Bcc recipients. The "B" in Bcc means "blind." The other
196 recipients should not see who has been Bcc'd. This means you want to
197 send two messages: one to to_rcpt and cc_rcpt, with no Bcc header
198 present; and another to the_boss only, with the Bcc header. If you
199 just pick up Bcc addresses as recipients, everyone will see who was
200 Bcc'd.
201
202 Email::Sender::Simple promises to send messages atomically. That is:
203 it won't deliver to only some of the recipients, and not to others.
204 That means it can't automatically detect the Bcc header and make two
205 deliveries. There would be a possibility for the second to fail after
206 the first succeeded, which would break the promise of a pure failure or
207 success.
208
209 The other strategy for dealing with Bcc is to remove the Bcc header
210 from the message and then inject the message with an envelope including
211 the Bcc addresses. The envelope information will not be visible to the
212 final recipients, so this is safe. Unfortunately, this requires
213 modifying the message, and Email::Sender::Simple should not be altering
214 the mutable email object passed to it. There is no "clone" method on
215 Email::Abstract, so it cannot just build a clone and modify that,
216 either. When such a method exists, Bcc handling may be possible.
217
218 Example Bcc Handling
219
220 If you want to support the Bcc header now, it is up to you to deal with
221 how you want to munge the mail and inject the (possibly) munged copies
222 into your outbound mailflow. It is not reasonable to suggest that
223 Email::Sender::Simple do this job.
224
225 Example 1: Explicitly set the envelope recipients for Bcc recipients
226
227 Create the email without a Bcc header, send it to the Bcc users
228 explicitly and then send it to the To/Cc users implicitly.
229
230 my $message = create_email_mime_msg; # <- whatever you do to get the message
231
232 $message->header_set('bcc'); # delete the Bcc header before sending
233 sendmail($message, { to => $rcpt }); # send to explicit Bcc address
234 sendmail($message); # and then send as normal
235
236 Example 2: Explicitly set the envelope recipients for all recipients
237
238 You can make a single call to "sendmail" by pulling all the recipient
239 addresses from the headers yourself and specifying all the envelope
240 recipients once. Again, delete the Bcc header before the message is
241 sent.
242
244 This is awesome! Where can I learn more?
245 Have a look at Email::Sender::Manual, where all the manual's documents
246 are listed. You can also look at the documentation for
247 Email::Sender::Simple and the various Email::Sender::Transport classes.
248
250 Ricardo Signes <rjbs@semiotic.systems>
251
253 This software is copyright (c) 2020 by Ricardo Signes.
254
255 This is free software; you can redistribute it and/or modify it under
256 the same terms as the Perl 5 programming language system itself.
257
258
259
260perl v5.32.1 2021-01-2E7mail::Sender::Manual::QuickStart(3)