Utiliser différents transports - Zend_Mail

Utiliser différents transports

Au cas où vous voudriez envoyer différents courriels via des connexions différentes, vous pouvez aussi passer l'objet de transport directement à send() sans être obligé d'appeler setDefaultTransport() avant. L'objet passé va être prioritaire sur le transport par défaut pour la requête send() courante.

Example #1 Utiliser différents transports

  1. $mail = new Zend_Mail();
  2. // construction du message
  3. $tr1 = new Zend_Mail_Transport_Smtp('');
  4. $tr2 = new Zend_Mail_Transport_Smtp('');
  5. $mail->send($tr1);
  6. $mail->send($tr2);
  7. $mail->send()// utilisation du transport par défaut

Note: Transports additionnels
Des transports additionnels peuvent-être écrit en implémentant Zend_Mail_Transport_Interface.

Using File Transport

Zend_Mail_Transport_File is useful in a development environment or for testing purposes. Instead of sending any real emails it simply dumps the email's body and headers to a file in the filesystem. Like the other transports, it may be configured using Zend_Application_Resource_Mail, or by passing an instance to the send() method of a Zend_Mail instance.

The transport has two optional parameters that can be passed to the constructor or via setOptions() method. The path option specifies the base path where new files are saved. If nothing is set, the transport uses the default system directory for temporary files. The second parameter, callback, defines what PHP callback should be used to generate a filename. As an example, assume we need to use the recipient's email plus some hash as the filename:

  1. function recipientFilename($transport)
  2. {
  3.     return $transport->recipients . '_' . mt_rand() . '.tmp';
  4. }
  6. $mail = new Zend_Mail();
  7. $mail->addTo('', 'Some Recipient');
  8. // build message...
  9. $tr = new Zend_Mail_Transport_File(array('callback' => 'recipientFilename'));
  10. $mail->send($tr);

The resulting file will be something like somebody@example.com_1493362665.tmp

Note: Include randomness in filename generation
When generating filenames, you should inject some sort of randomness into the generation to ensure that the filenames are unique. This is especially important on servers where you may expect high load, as it will ensure that despite a number of requests coming in during the same second or millisecond, the filename will still be unique.


© 2006-2022 by Zend by Perforce. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.