Zend_Mail-UsingFileTransport.xml 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect2 id="zend.mail.file-transport">
  4. <title>Using File Transport</title>
  5. <para>
  6. <classname>Zend_Mail_Transport_File</classname> is useful in a
  7. development environment or for testing purposes. Instead of sending any real
  8. emails it simply dumps the email's body and headers to a file in the filesystem.
  9. Like the other transports, it may be configured using
  10. <classname>Zend_Application_Resource_Mail</classname>, or by passing an instance to the
  11. <methodname>send()</methodname> method of a <classname>Zend_Mail</classname> instance.
  12. </para>
  13. <para>
  14. The transport has two optional parameters that can be passed to the constructor or
  15. via <methodname>setOptions()</methodname> method. The <property>path</property>
  16. option specifies the base path where new files are saved. If nothing is set, the transport
  17. uses the default system directory for temporary files. The second parameter,
  18. <property>callback</property>, defines what PHP callback should be used to generate a
  19. filename. As an example, assume we need to use the recipient's email plus some
  20. hash as the filename:
  21. </para>
  22. <programlisting language="php"><![CDATA[
  23. function recipientFilename($transport)
  24. {
  25. return $transport->recipients . '_' . mt_rand() . '.tmp';
  26. }
  27. $mail = new Zend_Mail();
  28. $mail->addTo('somebody@example.com', 'Some Recipient');
  29. // build message...
  30. $tr = new Zend_Mail_Transport_File(array('callback' => 'recipientFilename'));
  31. $mail->send($tr);
  32. ]]></programlisting>
  33. <para>
  34. The resulting file will be something like <filename>somebody@example.com_1493362665.tmp</filename>
  35. </para>
  36. <note>
  37. <title>Include randomness in filename generation</title>
  38. <para>
  39. When generating filenames, you should inject some sort of randomness into the generation
  40. to ensure that the filenames are unique. This is especially important on servers where
  41. you may expect high load, as it will ensure that despite a number of requests coming in
  42. during the same second or millisecond, the filename will still be unique.
  43. </para>
  44. </note>
  45. </sect2>