|
0
|
1 |
Sending Messages |
|
|
2 |
================ |
|
|
3 |
|
|
|
4 |
Quick Reference for Sending a Message |
|
|
5 |
------------------------------------- |
|
|
6 |
|
|
|
7 |
Sending a message is very straightforward. You create a Transport, use it to |
|
|
8 |
create the Mailer, then you use the Mailer to send the message. |
|
|
9 |
|
|
|
10 |
To send a Message: |
|
|
11 |
|
|
|
12 |
* Create a Transport from one of the provided Transports -- |
|
|
13 |
``Swift_SmtpTransport``, ``Swift_SendmailTransport``, |
|
|
14 |
``Swift_MailTransport`` or one of the aggregate Transports. |
|
|
15 |
|
|
|
16 |
* Create an instance of the ``Swift_Mailer`` class, using the Transport as |
|
|
17 |
it's constructor parameter. |
|
|
18 |
|
|
|
19 |
* Create a Message. |
|
|
20 |
|
|
|
21 |
* Send the message via the ``send()`` method on the Mailer object. |
|
|
22 |
|
|
|
23 |
When using ``send()`` the message will be sent just like it would |
|
|
24 |
be sent if you used your mail client. An integer is returned which includes |
|
|
25 |
the number of successful recipients. If none of the recipients could be sent |
|
|
26 |
to then zero will be returned, which equates to a boolean |
|
|
27 |
``false``. If you set two ``To:`` recipients and |
|
|
28 |
three ``Bcc:`` recipients in the message and all of the |
|
|
29 |
recipients are delivered to successfully then the value 5 will be returned. |
|
|
30 |
|
|
|
31 |
.. code-block:: php |
|
|
32 |
|
|
|
33 |
require_once 'lib/swift_required.php'; |
|
|
34 |
|
|
|
35 |
//Create the Transport |
|
|
36 |
$transport = Swift_SmtpTransport::newInstance('smtp.example.org', 25) |
|
|
37 |
->setUsername('your username') |
|
|
38 |
->setPassword('your password') |
|
|
39 |
; |
|
|
40 |
|
|
|
41 |
/* |
|
|
42 |
You could alternatively use a different transport such as Sendmail or Mail: |
|
|
43 |
|
|
|
44 |
//Sendmail |
|
|
45 |
$transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs'); |
|
|
46 |
|
|
|
47 |
//Mail |
|
|
48 |
$transport = Swift_MailTransport::newInstance(); |
|
|
49 |
*/ |
|
|
50 |
|
|
|
51 |
//Create the Mailer using your created Transport |
|
|
52 |
$mailer = Swift_Mailer::newInstance($transport); |
|
|
53 |
|
|
|
54 |
//Create a message |
|
|
55 |
$message = Swift_Message::newInstance('Wonderful Subject') |
|
|
56 |
->setFrom(array('john@doe.com' => 'John Doe')) |
|
|
57 |
->setTo(array('receiver@domain.org', 'other@domain.org' => 'A name')) |
|
|
58 |
->setBody('Here is the message itself') |
|
|
59 |
; |
|
|
60 |
|
|
|
61 |
//Send the message |
|
|
62 |
$result = $mailer->send($message); |
|
|
63 |
|
|
|
64 |
Transport Types |
|
|
65 |
~~~~~~~~~~~~~~~ |
|
|
66 |
|
|
|
67 |
A Transport is the component which actually does the sending. You need to |
|
|
68 |
provide a Transport object to the Mailer class and there are several possible |
|
|
69 |
options. |
|
|
70 |
|
|
|
71 |
Typically you will not need to know how a Transport works under-the-surface, |
|
|
72 |
you will only need to know how to create an instance of one, and which one to |
|
|
73 |
use for your environment. |
|
|
74 |
|
|
|
75 |
The SMTP Transport |
|
|
76 |
.................. |
|
|
77 |
|
|
|
78 |
The SMTP Transport sends messages over the (standardized) Simple Message Transfer |
|
|
79 |
Protocol. It can deal with encryption and authentication. |
|
|
80 |
|
|
|
81 |
The SMTP Transport, ``Swift_SmtpTransport`` is without doubt the most commonly |
|
|
82 |
used Transport because it will work on 99% of web servers (I just made that |
|
|
83 |
number up, but you get the idea). All the server needs is the ability to |
|
|
84 |
connect to a remote (or even local) SMTP server on the correct port number |
|
|
85 |
(usually 25). |
|
|
86 |
|
|
|
87 |
SMTP servers often require users to authenticate with a username and password |
|
|
88 |
before any mail can be sent to other domains. This is easily achieved using |
|
|
89 |
Swift Mailer with the SMTP Transport. |
|
|
90 |
|
|
|
91 |
SMTP is a protocol -- in other words it's a "way" of communicating a job |
|
|
92 |
to be done (i.e. sending a message). The SMTP protocol is the fundamental |
|
|
93 |
basis on which messages are delivered all over the internet 7 days a week, 365 |
|
|
94 |
days a year. For this reason it's the most "direct" method of sending messages |
|
|
95 |
you can use and it's the one that will give you the most power and feedback |
|
|
96 |
(such as delivery failures) when using Swift Mailer. |
|
|
97 |
|
|
|
98 |
Because SMTP is generally run as a remote service (i.e. you connect to it over |
|
|
99 |
the network/internet) it's extremely portable from server-to-server. You can |
|
|
100 |
easily store the SMTP server address and port number in a configuration file |
|
|
101 |
within your application and adjust the settings accordingly if the code is |
|
|
102 |
moved or if the SMTP server is changed. |
|
|
103 |
|
|
|
104 |
Some SMTP servers -- Google for example -- use encryption for |
|
|
105 |
security reasons. Swift Mailer supports using both SSL and TLS encryption |
|
|
106 |
settings. |
|
|
107 |
|
|
|
108 |
|
|
|
109 |
Using the SMTP Transport |
|
|
110 |
^^^^^^^^^^^^^^^^^^^^^^^^ |
|
|
111 |
|
|
|
112 |
The SMTP Transport is easy to use. Most configuration options can be set with |
|
|
113 |
the constructor. |
|
|
114 |
|
|
|
115 |
To use the SMTP Transport you need to know which SMTP server your code needs |
|
|
116 |
to connect to. Ask your web host if you're not sure. Lots of people ask me who |
|
|
117 |
to connect to -- I really can't answer that since it's a setting that's |
|
|
118 |
extremely specific to your hosting environment. |
|
|
119 |
|
|
|
120 |
To use the SMTP Transport: |
|
|
121 |
|
|
|
122 |
* Call ``Swift_SmtpTransport::newInstance()`` with the SMTP server name and |
|
|
123 |
optionally with a port number (defaults to 25). |
|
|
124 |
|
|
|
125 |
* Use the returned object to create the Mailer. |
|
|
126 |
|
|
|
127 |
A connection to the SMTP server will be established upon the first call to |
|
|
128 |
``send()``. |
|
|
129 |
|
|
|
130 |
.. code-block:: php |
|
|
131 |
|
|
|
132 |
require_once 'lib/swift_required.php'; |
|
|
133 |
|
|
|
134 |
//Create the Transport |
|
|
135 |
$transport = Swift_SmtpTransport::newInstance('smtp.example.org', 25); |
|
|
136 |
|
|
|
137 |
//Create the Mailer using your created Transport |
|
|
138 |
$mailer = Swift_Mailer::newInstance($transport); |
|
|
139 |
|
|
|
140 |
/* |
|
|
141 |
It's also possible to use multiple method calls |
|
|
142 |
|
|
|
143 |
$transport = Swift_SmtpTransport::newInstance() |
|
|
144 |
->setHost('smtp.example.org') |
|
|
145 |
->setPort(25) |
|
|
146 |
; |
|
|
147 |
*/ |
|
|
148 |
|
|
|
149 |
Encrypted SMTP |
|
|
150 |
^^^^^^^^^^^^^^ |
|
|
151 |
|
|
|
152 |
You can use SSL or TLS encryption with the SMTP Transport by specifying it as |
|
|
153 |
a parameter or with a method call. |
|
|
154 |
|
|
|
155 |
To use encryption with the SMTP Transport: |
|
|
156 |
|
|
|
157 |
* Pass the encryption setting as a third parameter to |
|
|
158 |
``Swift_SmtpTransport::newInstance()``; or |
|
|
159 |
|
|
|
160 |
* Call the ``setEncryption()`` method on the Transport. |
|
|
161 |
|
|
|
162 |
A connection to the SMTP server will be established upon the first call to |
|
|
163 |
``send()``. The connection will be initiated with the correct encryption |
|
|
164 |
settings. |
|
|
165 |
|
|
|
166 |
.. note:: |
|
|
167 |
|
|
|
168 |
For SSL or TLS encryption to work your PHP installation must have |
|
|
169 |
appropriate OpenSSL transports wrappers. You can check if "tls" and/or |
|
|
170 |
"ssl" are present in your PHP installation by using the PHP function |
|
|
171 |
``stream_get_transports()`` |
|
|
172 |
|
|
|
173 |
.. code-block:: php |
|
|
174 |
|
|
|
175 |
require_once 'lib/swift_required.php'; |
|
|
176 |
|
|
|
177 |
//Create the Transport |
|
|
178 |
$transport = Swift_SmtpTransport::newInstance('smtp.example.org', 587, 'ssl'); |
|
|
179 |
|
|
|
180 |
//Create the Mailer using your created Transport |
|
|
181 |
$mailer = Swift_Mailer::newInstance($transport); |
|
|
182 |
|
|
|
183 |
/* |
|
|
184 |
It's also possible to use multiple method calls |
|
|
185 |
|
|
|
186 |
$transport = Swift_SmtpTransport::newInstance() |
|
|
187 |
->setHost('smtp.example.org') |
|
|
188 |
->setPort(587) |
|
|
189 |
->setEncryption('ssl') |
|
|
190 |
; |
|
|
191 |
*/ |
|
|
192 |
|
|
|
193 |
SMTP with a Username and Password |
|
|
194 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
|
|
195 |
|
|
|
196 |
Some servers require authentication. You can provide a username and password |
|
|
197 |
with ``setUsername()`` and ``setPassword()``. |
|
|
198 |
|
|
|
199 |
To use a username and password with the SMTP Transport: |
|
|
200 |
|
|
|
201 |
* Create the Transport with ``Swift_SmtpTransport::newInstance()``. |
|
|
202 |
|
|
|
203 |
* Call the ``setUsername()`` and ``setPassword()`` methods on the Transport. |
|
|
204 |
|
|
|
205 |
Your username and password will be used to authenticate upon first connect |
|
|
206 |
when ``send()`` are first used on the Mailer. |
|
|
207 |
|
|
|
208 |
If authentication fails, an Exception of type |
|
|
209 |
``Swift_Transport_TransportException`` will be thrown. |
|
|
210 |
|
|
|
211 |
.. note:: |
|
|
212 |
|
|
|
213 |
If you need to know early whether or not authentication has failed and an |
|
|
214 |
Exception is going to be thrown, call the ``start()`` method on the |
|
|
215 |
created Transport. |
|
|
216 |
|
|
|
217 |
.. code-block:: php |
|
|
218 |
|
|
|
219 |
require_once 'lib/swift_required.php'; |
|
|
220 |
|
|
|
221 |
//Create the Transport the call setUsername() and setPassword() |
|
|
222 |
$transport = Swift_SmtpTransport::newInstance('smtp.example.org', 25) |
|
|
223 |
->setUsername('username') |
|
|
224 |
->setPassword('password') |
|
|
225 |
; |
|
|
226 |
|
|
|
227 |
//Create the Mailer using your created Transport |
|
|
228 |
$mailer = Swift_Mailer::newInstance($transport); |
|
|
229 |
|
|
|
230 |
The Sendmail Transport |
|
|
231 |
...................... |
|
|
232 |
|
|
|
233 |
The Sendmail Transport sends messages by communicating with a locally |
|
|
234 |
installed MTA -- such as ``sendmail``. |
|
|
235 |
|
|
|
236 |
The Sendmail Transport, ``Swift_SendmailTransport`` does not |
|
|
237 |
directly connect to any remote services. It is designed for Linux servers that |
|
|
238 |
have ``sendmail`` installed. The Transport starts a local |
|
|
239 |
``sendmail`` process and sends messages to it. Usually the |
|
|
240 |
``sendmail`` process will respond quickly as it spools your |
|
|
241 |
messages to disk before sending them. |
|
|
242 |
|
|
|
243 |
The Transport is named the Sendmail Transport for historical reasons |
|
|
244 |
(``sendmail`` was the "standard" UNIX tool for sending e-mail |
|
|
245 |
for years). It will send messages using other transfer agents such as Exim or |
|
|
246 |
Postfix despite its name, provided they have the relevant sendmail wrappers so |
|
|
247 |
that they can be started with the correct command-line flags. |
|
|
248 |
|
|
|
249 |
It's a common misconception that because the Sendmail Transport returns a |
|
|
250 |
result very quickly it must therefore deliver messages to recipients quickly |
|
|
251 |
-- this is not true. It's not slow by any means, but it's certainly not |
|
|
252 |
faster than SMTP when it comes to getting messages to the intended recipients. |
|
|
253 |
This is because sendmail itself sends the messages over SMTP once they have |
|
|
254 |
been quickly spooled to disk. |
|
|
255 |
|
|
|
256 |
The Sendmail Transport has the potential to be just as smart of the SMTP |
|
|
257 |
Transport when it comes to notifying Swift Mailer about which recipients were |
|
|
258 |
rejected, but in reality the majority of locally installed |
|
|
259 |
``sendmail`` instances are not configured well enough to |
|
|
260 |
provide any useful feedback. As such Swift Mailer may report successful |
|
|
261 |
deliveries where they did in fact fail before they even left your server. |
|
|
262 |
|
|
|
263 |
You can run the Sendmail Transport in two different modes specified by command |
|
|
264 |
line flags: |
|
|
265 |
|
|
|
266 |
* "``-bs``" runs in SMTP mode so theoretically it will act like the SMTP |
|
|
267 |
Transport |
|
|
268 |
|
|
|
269 |
* "``-t``" runs in piped mode with no feedback, but theoretically faster, |
|
|
270 |
though not advised |
|
|
271 |
|
|
|
272 |
You can think of the Sendmail Transport as a sort of asynchronous SMTP |
|
|
273 |
Transport -- though if you have problems with delivery failures you |
|
|
274 |
should try using the SMTP Transport instead. Swift Mailer isn't doing the work |
|
|
275 |
here, it's simply passing the work to somebody else (i.e. |
|
|
276 |
``sendmail``). |
|
|
277 |
|
|
|
278 |
Using the Sendmail Transport |
|
|
279 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
|
|
280 |
|
|
|
281 |
To use the Sendmail Transport you simply need to call |
|
|
282 |
``Swift_SendmailTransport::newInstance()`` with the command as a |
|
|
283 |
parameter. |
|
|
284 |
|
|
|
285 |
To use the Sendmail Transport you need to know where |
|
|
286 |
``sendmail`` or another MTA exists on the server. Swift Mailer |
|
|
287 |
uses a default value of ``/usr/sbin/sendmail``, which should |
|
|
288 |
work on most systems. |
|
|
289 |
|
|
|
290 |
You specify the entire command as a parameter (i.e. including the command line |
|
|
291 |
flags). Swift Mailer supports operational modes of "``-bs``" |
|
|
292 |
(default) and "``-t``". |
|
|
293 |
|
|
|
294 |
.. note:: |
|
|
295 |
|
|
|
296 |
If you run sendmail in "``-t``" mode you will get no feedback as to |
|
|
297 |
whether or not sending has succeeded. Use "``-bs``" unless you have a |
|
|
298 |
reason not to. |
|
|
299 |
|
|
|
300 |
To use the Sendmail Transport: |
|
|
301 |
|
|
|
302 |
* Call ``Swift_SendmailTransport::newInstance()`` with the command, including |
|
|
303 |
the correct command line flags. The default is to use ``/usr/sbin/sendmail |
|
|
304 |
-bs`` if this is not specified. |
|
|
305 |
|
|
|
306 |
* Use the returned object to create the Mailer. |
|
|
307 |
|
|
|
308 |
A sendmail process will be started upon the first call to ``send()``. If the |
|
|
309 |
process cannot be started successfully an Exception of type |
|
|
310 |
``Swift_Transport_TransportException`` will be thrown. |
|
|
311 |
|
|
|
312 |
.. code-block:: php |
|
|
313 |
|
|
|
314 |
require_once 'lib/swift_required.php'; |
|
|
315 |
|
|
|
316 |
//Create the Transport |
|
|
317 |
$transport = Swift_SendmailTransport::newInstance('/usr/sbin/exim -bs'); |
|
|
318 |
|
|
|
319 |
//Create the Mailer using your created Transport |
|
|
320 |
$mailer = Swift_Mailer::newInstance($transport); |
|
|
321 |
|
|
|
322 |
The Mail Transport |
|
|
323 |
.................. |
|
|
324 |
|
|
|
325 |
The Mail Transport sends messages by delegating to PHP's internal |
|
|
326 |
``mail()`` function. |
|
|
327 |
|
|
|
328 |
In my experience -- and others' -- the ``mail()`` |
|
|
329 |
function is not particularly predictable, or helpful. |
|
|
330 |
|
|
|
331 |
Quite notably, the ``mail()`` function behaves entirely |
|
|
332 |
differently between Linux and Windows servers. On linux it uses |
|
|
333 |
``sendmail``, but on Windows it uses SMTP. |
|
|
334 |
|
|
|
335 |
In order for the ``mail()`` function to even work at all |
|
|
336 |
``php.ini`` needs to be configured correctly, specifying the |
|
|
337 |
location of sendmail or of an SMTP server. |
|
|
338 |
|
|
|
339 |
The problem with ``mail()`` is that it "tries" to simplify things |
|
|
340 |
to the point that it actually makes things more complex due to poor interface |
|
|
341 |
design. The developers of Swift Mailer have gone to a lot of effort to make |
|
|
342 |
the Mail Transport work with a reasonable degree of consistency. |
|
|
343 |
|
|
|
344 |
Serious drawbacks when using this Transport are: |
|
|
345 |
|
|
|
346 |
* Unpredictable message headers |
|
|
347 |
|
|
|
348 |
* Lack of feedback regarding delivery failures |
|
|
349 |
|
|
|
350 |
* Lack of support for several plugins that require real-time delivery feedback |
|
|
351 |
|
|
|
352 |
It's a last resort, and we say that with a passion! |
|
|
353 |
|
|
|
354 |
Using the Mail Transport |
|
|
355 |
^^^^^^^^^^^^^^^^^^^^^^^^ |
|
|
356 |
|
|
|
357 |
To use the Mail Transport you simply need to call |
|
|
358 |
``Swift_MailTransport::newInstance()``. It's unlikely you'll need |
|
|
359 |
to configure the Transport. |
|
|
360 |
|
|
|
361 |
To use the Mail Transport: |
|
|
362 |
|
|
|
363 |
* Call ``Swift_MailTransport::newInstance()``. |
|
|
364 |
|
|
|
365 |
* Use the returned object to create the Mailer. |
|
|
366 |
|
|
|
367 |
Messages will be sent using the ``mail()`` function. |
|
|
368 |
|
|
|
369 |
.. note:: |
|
|
370 |
|
|
|
371 |
The ``mail()`` function can take a ``$additional_parameters`` parameter. |
|
|
372 |
Swift Mailer sets this to "``-f%s``" by default, where the "%s" is |
|
|
373 |
substituted with the address of the sender (via a ``sprintf()``) at send |
|
|
374 |
time. You may override this default by passing an argument to |
|
|
375 |
``newInstance()``. |
|
|
376 |
|
|
|
377 |
.. code-block:: php |
|
|
378 |
|
|
|
379 |
require_once 'lib/swift_required.php'; |
|
|
380 |
|
|
|
381 |
//Create the Transport |
|
|
382 |
$transport = Swift_MailTransport::newInstance(); |
|
|
383 |
|
|
|
384 |
//Create the Mailer using your created Transport |
|
|
385 |
$mailer = Swift_Mailer::newInstance($transport); |
|
|
386 |
|
|
|
387 |
Available Methods for Sending Messages |
|
|
388 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
|
389 |
|
|
|
390 |
The Mailer class offers two methods for sending Messages -- ``send()``. |
|
|
391 |
Each behaves in a slightly different way. |
|
|
392 |
|
|
|
393 |
When a message is sent in Swift Mailer, the Mailer class communicates with |
|
|
394 |
whichever Transport class you have chosen to use. |
|
|
395 |
|
|
|
396 |
Each recipient in the message should either be accepted or rejected by the |
|
|
397 |
Transport. For example, if the domain name on the email address is not |
|
|
398 |
reachable the SMTP Transport may reject the address because it cannot process |
|
|
399 |
it. Whichever method you use -- ``send()`` -- Swift Mailer will return |
|
|
400 |
an integer indicating the number of accepted recipients. |
|
|
401 |
|
|
|
402 |
.. note:: |
|
|
403 |
|
|
|
404 |
It's possible to find out which recipients were rejected -- we'll |
|
|
405 |
cover that later in this chapter. |
|
|
406 |
|
|
|
407 |
Using the ``send()`` Method |
|
|
408 |
........................... |
|
|
409 |
|
|
|
410 |
The ``send()`` method of the ``Swift_Mailer`` class |
|
|
411 |
sends a message using exactly the same logic as your Desktop mail client would |
|
|
412 |
use. Just pass it a Messgae and get a result. |
|
|
413 |
|
|
|
414 |
To send a Message with ``send()``: |
|
|
415 |
|
|
|
416 |
* Create a Transport from one of the provided Transports -- |
|
|
417 |
``Swift_SmtpTransport``, ``Swift_SendmailTransport``, |
|
|
418 |
``Swift_MailTransport`` or one of the aggregate Transports. |
|
|
419 |
|
|
|
420 |
* Create an instance of the ``Swift_Mailer`` class, using the Transport as |
|
|
421 |
it's constructor parameter. |
|
|
422 |
|
|
|
423 |
* Create a Message. |
|
|
424 |
|
|
|
425 |
* Send the message via the ``send()`` method on the Mailer object. |
|
|
426 |
|
|
|
427 |
The message will be sent just like it would be sent if you used your mail |
|
|
428 |
client. An integer is returned which includes the number of successful |
|
|
429 |
recipients. If none of the recipients could be sent to then zero will be |
|
|
430 |
returned, which equates to a boolean ``false``. If you set two |
|
|
431 |
``To:`` recipients and three ``Bcc:`` recipients in |
|
|
432 |
the message and all of the recipients are delivered to successfully then the |
|
|
433 |
value 5 will be returned. |
|
|
434 |
|
|
|
435 |
.. code-block:: php |
|
|
436 |
|
|
|
437 |
require_once 'lib/swift_required.php'; |
|
|
438 |
|
|
|
439 |
//Create the Transport |
|
|
440 |
$transport = Swift_SmtpTransport::newInstance('localhost', 25); |
|
|
441 |
|
|
|
442 |
//Create the Mailer using your created Transport |
|
|
443 |
$mailer = Swift_Mailer::newInstance($transport); |
|
|
444 |
|
|
|
445 |
//Create a message |
|
|
446 |
$message = Swift_Message::newInstance('Wonderful Subject') |
|
|
447 |
->setFrom(array('john@doe.com' => 'John Doe')) |
|
|
448 |
->setTo(array('receiver@domain.org', 'other@domain.org' => 'A name')) |
|
|
449 |
->setBody('Here is the message itself') |
|
|
450 |
; |
|
|
451 |
|
|
|
452 |
//Send the message |
|
|
453 |
$numSent = $mailer->send($message); |
|
|
454 |
|
|
|
455 |
printf("Sent %d messages\n", $numSent); |
|
|
456 |
|
|
|
457 |
/* Note that often that only the boolean equivalent of the |
|
|
458 |
return value is of concern (zero indicates FALSE) |
|
|
459 |
|
|
|
460 |
if ($mailer->send($message)) |
|
|
461 |
{ |
|
|
462 |
echo "Sent\n"; |
|
|
463 |
} |
|
|
464 |
else |
|
|
465 |
{ |
|
|
466 |
echo "Failed\n"; |
|
|
467 |
} |
|
|
468 |
|
|
|
469 |
*/ |
|
|
470 |
|
|
|
471 |
Sending Emails in Batch |
|
|
472 |
....................... |
|
|
473 |
|
|
|
474 |
If you want to send a separate message to each recipient so that only their |
|
|
475 |
own address shows up in the ``To:`` field, follow the following recipe: |
|
|
476 |
|
|
|
477 |
* Create a Transport from one of the provided Transports -- |
|
|
478 |
``Swift_SmtpTransport``, ``Swift_SendmailTransport``, |
|
|
479 |
``Swift_MailTransport`` or one of the aggregate Transports. |
|
|
480 |
|
|
|
481 |
* Create an instance of the ``Swift_Mailer`` class, using the Transport as |
|
|
482 |
it's constructor parameter. |
|
|
483 |
|
|
|
484 |
* Create a Message. |
|
|
485 |
|
|
|
486 |
* Iterate over the recipients and send message via the ``send()`` method on |
|
|
487 |
the Mailer object. |
|
|
488 |
|
|
|
489 |
Each recipient of the messages receives a different copy with only their own |
|
|
490 |
email address on the ``To:`` field. |
|
|
491 |
|
|
|
492 |
.. note:: |
|
|
493 |
|
|
|
494 |
In the following example, two emails are sent. One to each of |
|
|
495 |
``receiver@domain.org`` and ``other@domain.org``. These recipients will |
|
|
496 |
not be aware of each other. |
|
|
497 |
|
|
|
498 |
.. code-block:: php |
|
|
499 |
|
|
|
500 |
require_once 'lib/swift_required.php'; |
|
|
501 |
|
|
|
502 |
//Create the Transport |
|
|
503 |
$transport = Swift_SmtpTransport::newInstance('localhost', 25); |
|
|
504 |
|
|
|
505 |
//Create the Mailer using your created Transport |
|
|
506 |
$mailer = Swift_Mailer::newInstance($transport); |
|
|
507 |
|
|
|
508 |
//Create a message |
|
|
509 |
$message = Swift_Message::newInstance('Wonderful Subject') |
|
|
510 |
->setFrom(array('john@doe.com' => 'John Doe')) |
|
|
511 |
->setBody('Here is the message itself') |
|
|
512 |
; |
|
|
513 |
|
|
|
514 |
//Send the message |
|
|
515 |
$failedRecipients = array(); |
|
|
516 |
$numSent = 0; |
|
|
517 |
$to = array('receiver@domain.org', 'other@domain.org' => 'A name'); |
|
|
518 |
|
|
|
519 |
foreach ($to as $address => $name) |
|
|
520 |
{ |
|
|
521 |
$message->setTo(array($address => $name)); |
|
|
522 |
$numSent += $this->send($message, $failedRecipients); |
|
|
523 |
} |
|
|
524 |
|
|
|
525 |
printf("Sent %d messages\n", $numSent); |
|
|
526 |
|
|
|
527 |
Finding out Rejected Addresses |
|
|
528 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
|
529 |
|
|
|
530 |
It's possible to get a list of addresses that were rejected by the Transport |
|
|
531 |
by using a by-reference parameter to ``send()``. |
|
|
532 |
|
|
|
533 |
As Swift Mailer attempts to send the message to each address given to it, if a |
|
|
534 |
recipient is rejected it will be added to the array. You can pass an existing |
|
|
535 |
array, otherwise one will be created by-reference. |
|
|
536 |
|
|
|
537 |
Collecting the list of recipients that were rejected can be useful in |
|
|
538 |
circumstances where you need to "prune" a mailing list for example when some |
|
|
539 |
addresses cannot be delivered to. |
|
|
540 |
|
|
|
541 |
Getting Failures By-reference |
|
|
542 |
............................. |
|
|
543 |
|
|
|
544 |
Collecting delivery failures by-reference with the ``send()`` method is as |
|
|
545 |
simple as passing a variable name to the method call. |
|
|
546 |
|
|
|
547 |
To get failed recipients by-reference: |
|
|
548 |
|
|
|
549 |
* Pass a by-reference variable name to the ``send()`` method of the Mailer |
|
|
550 |
class. |
|
|
551 |
|
|
|
552 |
If the Transport rejects any of the recipients, the culprit addresses will be |
|
|
553 |
added to the array provided by-reference. |
|
|
554 |
|
|
|
555 |
.. note:: |
|
|
556 |
|
|
|
557 |
If the variable name does not yet exist, it will be initialized as an |
|
|
558 |
empty array and then failures will be added to that array. If the variable |
|
|
559 |
already exists it will be type-cast to an array and failures will be added |
|
|
560 |
to it. |
|
|
561 |
|
|
|
562 |
.. code-block:: php |
|
|
563 |
|
|
|
564 |
$mailer = Swift_Mailer::newInstance( ... ); |
|
|
565 |
|
|
|
566 |
$message = Swift_Message::newInstance( ... ) |
|
|
567 |
->setFrom( ... ) |
|
|
568 |
->setTo(array( |
|
|
569 |
'receiver@bad-domain.org' => 'Receiver Name', |
|
|
570 |
'other@domain.org' => 'A name', |
|
|
571 |
'other-receiver@bad-domain.org' => 'Other Name' |
|
|
572 |
)) |
|
|
573 |
->setBody( ... ) |
|
|
574 |
; |
|
|
575 |
|
|
|
576 |
//Pass a variable name to the send() method |
|
|
577 |
if (!$mailer->send($message, $failures)) |
|
|
578 |
{ |
|
|
579 |
echo "Failures:"; |
|
|
580 |
print_r($failures); |
|
|
581 |
} |
|
|
582 |
|
|
|
583 |
/* |
|
|
584 |
Failures: |
|
|
585 |
Array ( |
|
|
586 |
0 => receiver@bad-domain.org, |
|
|
587 |
1 => other-receiver@bad-domain.org |
|
|
588 |
) |
|
|
589 |
*/ |