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

  1. // Create transport
  2. 'name' => 'sender.example.com''mail.example.com', $config);
  3.  
  4. // Set From & Reply-To address and name for all emails to send.
  5. '[email protected]', 'John Doe''[email protected]','Jane Doe');
  6.  
  7. // Loop through messages
  8. '[email protected]', 'Test''Demonstration - Sending Multiple Mails per SMTP Connection''...Your message here...'// Reset defaults

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

  1. // Create transport
  2. 'mail.example.com');
  3. $protocol->connect();
  4. $protocol->helo('sender.example.com');
  5.  
  6. $transport->setConnection($protocol);
  7.  
  8. // Loop through messages
  9. '[email protected]', 'Test''[email protected]', 'Test''Demonstration - Sending Multiple Mails per SMTP Connection''...Your message here...');
  10.  
  11.     // Manually control the connection

Sending via SMTP