1 <?xml version="1.0" encoding="UTF-8"?>
3 <sect1 id="zend.mail.multiple-emails">
4 <title>Sending Multiple Mails per SMTP Connection</title>
7 By default, a single SMTP transport creates a single connection and
8 re-uses it for the lifetime of the script execution. You may send multiple
9 e-mails through this SMTP connection. A RSET command is issued before each
10 delivery to ensure the correct SMTP handshake is followed.
14 Optionally, you can also define a default From email address and name,
15 as well as a default reply-to header. This can be done through the static
16 methods <methodname>setDefaultFrom()</methodname> and
17 <methodname>setDefaultReplyTo()</methodname>. These defaults will be used when you
18 don't specify a From/Reply-to Address or -Name until the defaults are reset (cleared).
19 Resetting the defaults can be done through the use of the
20 <methodname>clearDefaultFrom()</methodname> and
21 <methodname>clearDefaultReplyTo</methodname>.
24 <example id="zend.mail.multiple-emails.example-1">
25 <title>Sending Multiple Mails per SMTP Connection</title>
27 <programlisting language="php"><![CDATA[
29 $config = array('name' => 'sender.example.com');
30 $transport = new Zend_Mail_Transport_Smtp('mail.example.com', $config);
32 // Set From & Reply-To address and name for all emails to send.
33 Zend_Mail::setDefaultFrom('sender@example.com', 'John Doe');
34 Zend_Mail::setDefaultReplyTo('replyto@example.com','Jane Doe');
36 // Loop through messages
37 for ($i = 0; $i < 5; $i++) {
38 $mail = new Zend_Mail();
39 $mail->addTo('studio@example.com', 'Test');
42 'Demonstration - Sending Multiple Mails per SMTP Connection'
44 $mail->setBodyText('...Your message here...');
45 $mail->send($transport);
49 Zend_Mail::clearDefaultFrom();
50 Zend_Mail::clearDefaultReplyTo();
55 If you wish to have a separate connection for each mail
56 delivery, you will need to create and destroy your transport before and
57 after each <methodname>send()</methodname> method is called. Or alternatively,
58 you can manipulate the connection between each delivery by accessing the
59 transport's protocol object.
62 <example id="zend.mail.multiple-emails.example-2">
63 <title>Manually controlling the transport connection</title>
65 <programlisting language="php"><![CDATA[
67 $transport = new Zend_Mail_Transport_Smtp();
69 $protocol = new Zend_Mail_Protocol_Smtp('mail.example.com');
71 $protocol->helo('sender.example.com');
73 $transport->setConnection($protocol);
75 // Loop through messages
76 for ($i = 0; $i < 5; $i++) {
77 $mail = new Zend_Mail();
78 $mail->addTo('studio@example.com', 'Test');
79 $mail->setFrom('studio@example.com', 'Test');
81 'Demonstration - Sending Multiple Mails per SMTP Connection'
83 $mail->setBodyText('...Your message here...');
85 // Manually control the connection
87 $mail->send($transport);
91 $protocol->disconnect();