Programmer's Reference Guide

SMTP 経由での送信

SMTP 接続による複数のメールの送信

デフォルトでは、ひとつの SMTP トランスポートが ひとつの接続を作成し、スクリプトの実行中はそれを使いまわします。 この SMTP 接続で、複数のメールを送信できます。 SMTP のハンドシェイクを正しく行うには、 メッセージの配送の前に RSET コマンドを発行します。

任意で、既定の reply-to ヘッダだけではなく、既定の email アドレスと名前も 定義できます。これは静的メソッド setDefaultFrom() 及び setDefaultReplyTo() を介して行なわれます。 これらの既定値は、既定値がリセット(消去)されるまで、 From/Reply-to のアドレスや 名前を指定しない場合に使われます。 既定値は clearDefaultFrom() 及び clearDefaultReplyTo を使って消去されます。

例1 SMTP 接続による複数のメールの送信

  1. // トランスポートを作成します
  2. $config = array('name' => 'sender.example.com');
  3. $transport = new Zend_Mail_Transport_Smtp('mail.example.com', $config);
  4.  
  5. // 送信するメール全てで使う From 及び Reply-To のアドレス及び名前を設定します
  6. Zend_Mail::setDefaultFrom('sender@example.com', 'John Doe');
  7. Zend_Mail::setDefaultReplyTo('replyto@example.com','Jane Doe');
  8.  
  9. // メッセージをループ処理します
  10. for ($i = 0; $i < 5; $i++) {
  11.     $mail = new Zend_Mail();
  12.     $mail->addTo('studio@example.com', 'Test');
  13.     $mail->setSubject(
  14.         'Demonstration - Sending Multiple Mails per SMTP Connection'
  15.     );
  16.     $mail->setBodyText('...Your message here...');
  17.     $mail->send($transport);
  18. }
  19.  
  20. // 既定値をリセットします
  21. Zend_Mail::clearDefaultFrom();
  22. Zend_Mail::clearDefaultReplyTo();

各配送ごとに別々の接続を使用したい場合は、 send() メソッドのコールの前後に トランスポートの作成と廃棄をする必要があります。 あるいは、トランスポートのプロトコルオブジェクトを用いて 各配送の接続を操作することもできます。

例2 トランスポートの接続の手動制御

  1. // トランスポートを作成します
  2. $transport = new Zend_Mail_Transport_Smtp();
  3.  
  4. $protocol = new Zend_Mail_Protocol_Smtp('mail.example.com');
  5. $protocol->connect();
  6. $protocol->helo('sender.example.com');
  7.  
  8. $transport->setConnection($protocol);
  9.  
  10. // メッセージをループ処理します
  11. for ($i = 0; $i < 5; $i++) {
  12.     $mail = new Zend_Mail();
  13.     $mail->addTo('studio@example.com', 'Test');
  14.     $mail->setFrom('studio@example.com', 'Test');
  15.     $mail->setSubject(
  16.         'Demonstration - Sending Multiple Mails per SMTP Connection'
  17.     );
  18.     $mail->setBodyText('...Your message here...');
  19.  
  20.     // 手動で接続を制御します
  21.     $protocol->rset();
  22.     $mail->send($transport);
  23. }
  24.  
  25. $protocol->quit();
  26. $protocol->disconnect();

SMTP 経由での送信

Comments

obeject for the Zend_Mail should be created before starting the for loop
Why?

For my opinion if we create object before the loop so for the each new message will be added new address.

more:
http://framework.zend.com/manual/en/zend.mail.adding-recipients.html
Please if you are planning on using SMTP for mailing use the following option while setting up your Zend_Mail_Transport_Smtp . Save yourself the 3hours of headache.

SPECIFY [b]'auth' => 'login'[/b] in the config options


$config = array(
                'auth' => 'login',
                'username' => $username,
                'password' => $password);

+ Add A Comment

Please do not report issues via comments; use the ZF Issue Tracker.

If you have a JIRA/Crowd account, we suggest you login first before commenting.

  • BBCode is allowed in the comment markup

  • Select a Version

    Languages Available

    Components

    Search the Manual