Programmer's Reference Guide

Zend_Mail

導入

まずはじめに

Zend_Mail は、テキストメールや MIME マルチパートメールを作成・送信するための一般的な機能を提供します。 Zend_Mail を使用すると、デフォルトの Zend_Mail_Transport_Sendmail か、あるいは Zend_Mail_Transport_Smtp を使用してメールを送信できます。

例1 Zend_Mail を使用したシンプルなメール送信

受信者、表題、本文および送信者を指定しただけの単純なメールです。 このようなメールを Zend_Mail_Transport_Sendmail を用いて送信するには、次のようにします。

  1. $mail = new Zend_Mail();
  2. $mail->setBodyText('This is the text of the mail.');
  3. $mail->setFrom('somebody@example.com', 'Some Sender');
  4. $mail->addTo('somebody_else@example.com', 'Some Recipient');
  5. $mail->setSubject('TestSubject');
  6. $mail->send();

注意: 最低限の定義
Zend_Mail でメールを送信するには、 最低 1 か所以上の受信者、送信者 ( setFrom() を使用します)、 そして本文 (テキストや HTML) を指定しなければなりません。

大半の属性については、その情報を読み込むための "get" メソッドが用意されています。詳細は、API ドキュメントを参照ください。 getRecipients() だけは特別で、 これまでに追加されたすべての受信者アドレスを配列で返します。

セキュリティの観点から、Zend_Mail はすべてのヘッダフィールドの改行文字 (\n) を取り除きます。 これにより、ヘッダインジェクションを防ぎます。 送信者名およびあて先名中の2重引用符は単一引用符に、山括弧は角括弧に変更されます。 もしその記号がメールアドレス中にある場合は除去されます。

Zend_Mail オブジェクトのほとんどのメソッドは、 流れるようなインターフェイス形式でコールすることもできます。

  1. $mail = new Zend_Mail();
  2. $mail->setBodyText('This is the text of the mail.')
  3.     ->setFrom('somebody@example.com', 'Some Sender')
  4.     ->addTo('somebody_else@example.com', 'Some Recipient')
  5.     ->setSubject('TestSubject')
  6.     ->send();

デフォルトの sendmail トランスポートの設定

Zend_Mail がデフォルトで使用するのは Zend_Mail_Transport_Sendmail です。これは、単に PHP» mail() 関数をラップしただけのものです。 » mail() 関数に追加のパラメータを渡したい場合は、 新しいインスタンスを作成する際のコンストラクタにパラメータを渡します。 新しく作成したインスタンスは、Zend_Mail のデフォルトのトランスポートとすることができます。 あるいは Zend_Mailsend() メソッドに渡すこともできます。

例2 Zend_Mail_Transport_Sendmail トランスポートへの追加パラメータの渡し方

この例は、» mail() 関数の Return-Path を変更する方法を示すものです。

  1. $tr = new Zend_Mail_Transport_Sendmail('-freturn_to_me@example.com');
  2. Zend_Mail::setDefaultTransport($tr);
  3.  
  4. $mail = new Zend_Mail();
  5. $mail->setBodyText('This is the text of the mail.');
  6. $mail->setFrom('somebody@example.com', 'Some Sender');
  7. $mail->addTo('somebody_else@example.com', 'Some Recipient');
  8. $mail->setSubject('TestSubject');
  9. $mail->send();

注意: セーフモード時の制限
PHP をセーフモードで実行している場合、オプションの追加パラメータを指定すると » mail() 関数の実行に失敗する可能性があります。

警告

Sendmail トランスポートと Windows

PHP マニュアルでは、 mail()関数は Windows と *nix ベースのシステムとでは、 異なる振る舞いをすると述べています。 Windows で Sendmail トランスポートを利用すると、 addBcc() との連携は動作しません。 他のすべての受信者が、受信者として彼を見られるように、 mail() 関数は BCC 受信者に送ります。

そのため、もし Windows サーバで BCC を使いたいなら、 SMTP トランスポートを送信に使ってください。


Zend_Mail

Comments

On Windows there is another Problem, at least if you have Mercury Mailserver listening at localhost port 25 for php giving him mails to send.

Because of the line

if (0 === strpos(PHP_OS, 'WIN')) {

in the file /Zend/Mail/Transport/Sendmail.php and the code following the name you give to

$mail->addTo('somebody_else@example.com', 'Some Recipient');

in this case 'Some Recipient' will not be added correctly to your mailheader.

If you change the line to

if (0 === strpos(PHP_OS, 'xxxxxxxxxx')) {

and therefore treat the windows / mercury -combination the same way as you treat i. e. linux / postfix, than everything works fine.

To bad. Is that a bug? I do not understand, what for the line

if (0 === strpos(PHP_OS, 'WIN')) {

than is altogether.

Regards,
Marc
Hi all,

I've just learned about this topic.
I wanna ask, how to add recipients from a variable?

Case:

$rcpt = array('oo@gmail.com', 'cc@gmail.com', 'kk@gmail.com', 'yy@gmail.com');

$mail = new Zend_Mail();
$mail->setBodyText('This is the text of the mail.');
$mail->setFrom('somebody@example.com', 'Some Sender');
$mail->addTo($rcpt);

The error is 501 invalid address.

Can you help me, please?

Thank you
@Ocky Harliansyah:

Given your $rcpt array, just:

$mail->setFrom('somebody@example.com', 'Some Sender');
foreach($rcpt as $recip) {
  $mail->addTo($recip, '');
}


Hope it helps.

+ 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