Zend_Mail-MultipleEmails.xml 3.3 KB

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