Package Home

Zend Framework 2 Documentation (Manual)

PHK Home

File: /modules/zend.mail.multiple-emails.html

Size:14915
Storage flags:no_autoload,compress/gzip (24%)

Sending Multiple Mails per SMTP Connection — Zend Framework 2 2.4.2 documentation

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.

Sending Multiple Mails per SMTP Connection

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Create transport
$config = array('name' => 'sender.example.com');
$transport = new Zend\Mail\Transport\Smtp('mail.example.com', $config);

// Set From & Reply-To address and name for all emails to send.
Zend\Mail\Message::setDefaultFrom('sender@example.com', 'John Doe');
Zend\Mail\Message::setDefaultReplyTo('replyto@example.com','Jane Doe');

// Loop through messages
for ($i = 0; $i < 5; $i++) {
    $mail = new Zend\Mail\Message();
    $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\Message::clearDefaultFrom();
Zend\Mail\Message::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.

Manually controlling the transport connection

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// 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\Message();
    $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();

This Page

Note: You need to stay logged into your GitHub account to contribute to the documentation.

Edit this document

Edit this document

The source code of this file is hosted on GitHub. Everyone can update and fix errors in this document with few clicks - no downloads needed.

  1. Login with your GitHub account.
  2. Go to Sending Multiple Mails per SMTP Connection on GitHub.
  3. Edit file contents using GitHub's text editor in your web browser
  4. Fill in the Commit message text box at the end of the page telling why you did the changes. Press Propose file change button next to it when done.
  5. On Send a pull request page you don't need to fill in text anymore. Just press Send pull request button.
  6. Your changes are now queued for review under project's Pull requests tab on GitHub.

For more information about the PHK package format: http://phk.tekwire.net