Programmer's Reference Guide
| Sending via SMTP |
Sending Multiple Mails per SMTP Connection
By default, a single SMTP transport creates a single connection and re-uses it for the lifetime of the script execution. You may send multiple e-mails through this SMTP connection. A RSET command is issued before each delivery to ensure the correct SMTP handshake is followed.
Optionally, you can also define a default From email address and name, as well as a default reply-to header. This can be done through the static methods setDefaultFrom() and setDefaultReplyTo(). These defaults will be used when you don't specify a From/Reply-to Address or -Name until the defaults are reset (cleared). Resetting the defaults can be done through the use of the clearDefaultFrom() and clearDefaultReplyTo.
Example #1 Sending Multiple Mails per SMTP Connection
- // Create transport
- $transport = new Zend_Mail_Transport_Smtp('mail.example.com', $config);
- // Set From & Reply-To address and name for all emails to send.
- Zend_Mail::setDefaultFrom('sender@example.com', 'John Doe');
- Zend_Mail::setDefaultReplyTo('replyto@example.com','Jane Doe');
- // Loop through messages
- for ($i = 0; $i < 5; $i++) {
- $mail = new Zend_Mail();
- $mail->addTo('studio@example.com', 'Test');
- $mail->setSubject(
- 'Demonstration - Sending Multiple Mails per SMTP Connection'
- );
- $mail->setBodyText('...Your message here...');
- $mail->send($transport);
- }
- // Reset defaults
- Zend_Mail::clearDefaultFrom();
- Zend_Mail::clearDefaultReplyTo();
If you wish to have a separate connection for each mail delivery, you will need to create and destroy your transport before and after each send() method is called. Or alternatively, you can manipulate the connection between each delivery by accessing the transport's protocol object.
Example #2 Manually controlling the transport connection
- // Create transport
- $transport = new Zend_Mail_Transport_Smtp();
- $protocol = new Zend_Mail_Protocol_Smtp('mail.example.com');
- $protocol->connect();
- $protocol->helo('sender.example.com');
- $transport->setConnection($protocol);
- // Loop through messages
- for ($i = 0; $i < 5; $i++) {
- $mail = new Zend_Mail();
- $mail->addTo('studio@example.com', 'Test');
- $mail->setFrom('studio@example.com', 'Test');
- $mail->setSubject(
- 'Demonstration - Sending Multiple Mails per SMTP Connection'
- );
- $mail->setBodyText('...Your message here...');
- // Manually control the connection
- $protocol->rset();
- $mail->send($transport);
- }
- $protocol->quit();
- $protocol->disconnect();
| Sending via SMTP |
Add A Comment
Please do not report issues via comments; use the ZF Issue Tracker.
If you have a JIRA/Crowd account, we suggest you login first before commenting.
Select a Version
Languages Available
Components
Search the Manual
Navigation
- Programmer's Reference Guide
- Programmer's Reference Guide
- Zend Framework Reference
- Zend_Mail
- Introduction
- Sending via SMTP
- Sending Multiple Mails per SMTP Connection
- Using Different Transports
- HTML E-Mail
- Attachments
- Adding Recipients
- Controlling the MIME Boundary
- Additional Headers
- Character Sets
- Encoding
- SMTP Authentication
- Securing SMTP Transport
- Reading Mail Messages

Comments
For my opinion if we create object before the loop so for the each new message will be added new address.
more:
http://framework.zend.com/manual/en/zend.mail.adding-recipients.html
Zend_Mail_Transport_Smtp. Save yourself the 3hours of headache.SPECIFY
[b]'auth' => 'login'[/b]in the config options$config = array(
'auth' => 'login',
'username' => $username,
'password' => $password);