简介
Zend_Mail提供了通用化的功能来创作和发送文本以及兼容MIME标准的含有多个段的邮件消息。
Zend_Mail通过php内建的mail()函数或者直接通过SMTP连接来发送邮件
使用Zend_Mail发送简单邮件
一个简单邮件由一个或者几个收件人,一个主题,一个邮件主体和一个发件人组成。
下面的步骤,使用了PHP的mail()函数来发送邮件:
<?php
require_once 'Zend/Mail.php';
$mail = new Zend_Mail();
$mail->setBodyText('This is the text of the mail.');
$mail->setFrom('somebody@example.com', 'Some Sender');
$mail->addTo('somebody_else@example.com', 'Some Recipient');
$mail->setSubject('TestSubject');
$mail->send();
?>
通过“get”方法可以读取绝大多数储存在“mail”对象中的邮件属性,
getRecipients()是一个特例,它返回一个含有所有先前被加入的收件人地址的数组。
出于安全原因,Zend_Mail了过滤邮件头中所有字段,
以防止基于换行符(\n)邮件头注入(header injection)漏洞攻击
通过SMTP发送邮件
以SMTP方式发送邮件,Zend_Mail_Transport_Smtp对象需要在send()方法被调用之前创建并注册到Zend_Mail中去。
当前脚本程序中所有使用Zend_Mail::send()发送的邮件,都将以SMTP方式传送:
<?php
require_once 'Zend/Mail/Transport/Smtp.php';
$tr = new Zend_Mail_Transport_Smtp('mail.example.com');
Zend_Mail::setDefaultTransport($tr);
?>
setDefaultTransport()方法的调用和Zend_Mail_Transport_Smtp对象的构造代价并不昂贵。
这两行可以添加在脚本程序的配置文件(例如config.inc或者类似文件)中,从而为整个脚本程序配置Zend_Mail类的行为。
如此可以把配置信息从应用程序逻辑分离出来邮件是通过SMTP还是mail()发送,使用什么邮件服务器等等。
通过一个SMTP连接发送多个邮件
在缺省状态下,系统会为每一个被发送的邮件建立一个SMTP连接。
如果想通过一个SMTP连接发送多个邮件,可以自己调用connect()函数。
如果在send()被调用之前就已经建立了一个传送连接,那么这个连接会被使用并且不会被自动关闭。
<?php
require_once 'Zend/Mail.php';
$mail = new Zend_Mail();
// build message...
require_once 'Zend/Mail/Transport/Smtp.php';
$tr = new Zend_Mail_Transport_Smtp('mail.example.com');
Zend_Mail::setDefaultTransport($tr);
$tr->connect();
for ($i = 0; $i < 5; $i++) {
$mail->send();
}
$tr->disconnect();
?>
使用不同的Transport对象
有时想想使用不同的连接来发送不同的邮件,
也可以不预先调用setDefaultTransport()方法,而直接将Transport对象传递给send()。
被传递的transport对象会在实际的send()调用中替代缺省的transport:
<?php
require_once 'Zend/Mail.php';
$mail = new Zend_Mail();
// build message...
require_once 'Zend/Mail/Transport/Smtp.php';
$tr1 = new Zend_Mail_Transport_Smtp('server@example.com');
$tr2 = new Zend_Mail_Transport_Smtp('other_server@example.com');
$mail->send($tr1);
$mail->send($tr2);
$mail->send(); // use default again
?>
外加transport,需要实现Zend_Mail_Transport_Interface接口。
HTML邮件
发送HTML格式的邮件,使用setBodyHTML()方法来设置邮件正文而不是setBodyText()方法,
MIME类型会被自动设置为text/html。 如果既使用HTML又使用纯文本,
那么multipart/alternative MIME类型的邮件消息将会自动产生:
<?php
require_once 'Zend/Mail.php';
$mail = new Zend_Mail();
$mail->setBodyText('My Nice Test Text');
$mail->setBodyHtml('My Nice <b>Test</b> Text');
$mail->setFrom('somebody@example.com', 'Some Sender');
$mail->addTo('somebody_else@example.com', 'Some Recipient');
$mail->setSubject('TestSubject');
$mail->send();
?>
附件
使用addAttachment()方法可以将文件附加到邮件中。
Zend_Mail会缺省地认为该文件是二进制对象(application/octet-stream),以 base64编码传输,
并且作为邮件的附件处理。 通过传递额外的参数给addAttachment()方法可以覆盖上述缺省设定:
<?php
require_once 'Zend/Mail.php';
$mail = new Zend_Mail();
// build message...
$mail->addAttachment($someBinaryString);
$mail->addAttachment($myImage, 'image/gif', Zend_Mime::DISPOSITION_INLINE, Zend_Mime::ENCODING_8BIT);
?>
如果想得到对此附件MIME段产生的更多控制,可以使用addAttachment()方法的返回值来修改它的属性。
方法addAttachment()返回了一个Zend_Mime_Part对象:
<?php
require_once 'Zend/Mail.php';
$mail = new Zend_Mail();
$at = $mail->addAttachment($myImage);
$at->type = 'image/gif';
$at->disposition = Zend_Mime::DISPOSITION_INLINE;
$at->encoding = Zend_Mime::ENCODING_8BIT;
$at->filename = 'test.gif';
$mail->send();
?>
增加收件人
有三种方法可以增加收件人:
addTo():增加一个收件人到邮件头“To”(收件人)
addCc():增加一个收件人到邮件头“Cc”(抄送)
addBcc():增加一个收件人到邮件头“Bcc”(暗送)
addTo()和addCc()接受可选的第二个参数用于为邮件头的收件人指定具有可读性的名字。
控制MIME分界线
在一个包含多个段的邮件里,用于分隔邮件不同段的MIME分界线(MIME boundary)通常是随机生成的。
但是在某些情况下,也许会希望使用特定的MIME分界线。
如下面的例子所示,可以使用setMimeBoundary()方法来做到这一点:
<?php
require_once 'Zend/Mail.php';
$mail = new Zend_Mail();
$mail->setMimeBoundary('=_' . md5(microtime(1) . $someId++);
// build message...
?>
外加邮件头信息
使用addHeader()方法可以外加任意的邮件头信息。它需要两个参数,头信息的名称和值,
第三个可选的参数,它决定了该邮件头信息是否可以有多个值:
<?php
require_once 'Zend/Mail.php';
$mail = new Zend_Mail();
$mail->addHeader('X-MailGenerator', 'MyCoolApplication');
$mail->addHeader('X-greetingsTo', 'Mom', true); // multiple values
$mail->addHeader('X-greetingsTo', 'Dad', true);
?>
字符集
Zend_Mail不会对邮件段字符集的正确性进行检查。 当Zend_Mail初始化的时候,
缺省地为邮件设置了iso8859-1字符集。
程序必须确保添加至邮件对象的所有段都以正确的字符集编码。
在添加一个新的邮件段时,可以为每个段指定不同的字符集。
字符集只适用于文本格式中的段。
编码
文本和HTML信息在缺省下以Quoted-printable机制编码。
在调用addAttachment()方法添加附件的时候, 如果没有指定编码方式,都将以base64方式编码,
或稍后通过给MIME段对象(Zend_Mime_Part)相应属性赋值来指定编码方式。
7Bit和8Bit编码目前仅仅在二进制数据上可行。
Zend_Mail_Transport_Smtp编码行以一个点(.)或者两个点(..)起始,以确保邮件不违反SMTP协议。
SMTP 身份验证
Zend_Mail 支持使用 SMTP 身份验证,通过配置数组传递“auth”参数到 Zend_Mail_Transport_Smtp 的构造函数中。
可用的内建身份验证方法为 PLAIN,LOGIN 和 CRAM-MD5,这些都需要在配置数组中设置“username”和“password”。
<?php
require_once 'Zend/Mail.php';
require_once 'Zend/Mail/Transport/Smtp.php';
$config = array('auth' => 'login',
'username' => 'myusername',
'password' => 'password');
$transport = new Zend_Mail_Transport_Smtp('mail.server.com', $config);
$mail = new Zend_Mail();
$mail->setBodyText('This is the text of the mail.');
$mail->setFrom('sender@test.com', 'Some Sender');
$mail->addTo('recipient@test.com', 'Some Recipient');
$mail->setSubject('TestSubject');
$mail->send($transport);
?>
身份验证的类型是大小写不敏感的,但是不能有标点符号。
例如,使用 CRAM-MD5 类型,则应当传递 'auth' => 'crammd5' 到 Zend_Mail_Transport_Smtp 的构造函数中。
Securing SMTP Transport 安全性
Zend_Mail also supports the use of either TLS or SSL to secure a SMTP connection.
This can be enabled be passing the 'ssl' parameter to the configuration array
in the Zend_Mail_Transport_Smtp constructor with a value of either 'ssl' or 'tls'.
A port can optionally be supplied, otherwise it defaults to 25 for TLS or 465 for SSL.
<?php
require_once 'Zend/Mail.php';
require_once 'Zend/Mail/Transport/Smtp.php';
$config = array('ssl' => 'tls',
'port' => 25); // Optional port number supplied
$transport = new Zend_Mail_Transport_Smtp('mail.server.com', $config);
$mail = new Zend_Mail();
$mail->setBodyText('This is the text of the mail.');
$mail->setFrom('sender@test.com', 'Some Sender');
$mail->addTo('recipient@test.com', 'Some Recipient');
$mail->setSubject('TestSubject');
$mail->send($transport);
?>
Reading Mail Messages 读取邮件
Zend_Mail can read mail messages from several local or remote mail storages.
All of them have the same basic API to count and fetch messages
and some of them implement additional interfaces for not so common features.
For a feature overview of the implemented storages see the following table.
Feature Mbox Maildir Pop3 IMAP
Storage type local local remote remote
Fetch message Yes Yes Yes Yes
Fetch mime-part emulated emulated emulated emulated
Folders Yes Yes No Yes
Create message/folder No todo No todo
Flags No Yes No Yes
Simple example using Pop3
<?php
$mail = new Zend_Mail_Storage_Pop3(array('host' => 'localhost',
'user' => 'test',
'password' => 'test'));
echo $mail->countMessages() . " messages found\n";
foreach ($mail as $message) {
echo "Mail from '{$message->from}': {$message->subject}\n";
}
?>
$mail = new Zend_Mail_Storage_Mbox(array('filename' => '/home/test/mail/inbox'));
$mail = new Zend_Mail_Storage_Maildir(array('dirname' => '/home/test/mail/'));
// connecting with Pop3
$mail = new Zend_Mail_Storage_Pop3(array('host' => 'example.com'
'user' => 'test',
'password' => 'test'));
// connecting with Imap
$mail = new Zend_Mail_Storage_Imap(array('host' => 'example.com'
'user' => 'test',
'password' => 'test'));
// example for a none standard port
$mail = new Zend_Mail_Storage_Pop3(array('host' => 'example.com',
'port' => 1120
'user' => 'test',
'password' => 'test'));
Advanced Use 高级应用
Using NOOP
Caching instances
Extending Protocol Classes
Zend_Mail提供了通用化的功能来创作和发送文本以及兼容MIME标准的含有多个段的邮件消息。
Zend_Mail通过php内建的mail()函数或者直接通过SMTP连接来发送邮件
使用Zend_Mail发送简单邮件
一个简单邮件由一个或者几个收件人,一个主题,一个邮件主体和一个发件人组成。
下面的步骤,使用了PHP的mail()函数来发送邮件:
<?php
require_once 'Zend/Mail.php';
$mail = new Zend_Mail();
$mail->setBodyText('This is the text of the mail.');
$mail->setFrom('somebody@example.com', 'Some Sender');
$mail->addTo('somebody_else@example.com', 'Some Recipient');
$mail->setSubject('TestSubject');
$mail->send();
?>
通过“get”方法可以读取绝大多数储存在“mail”对象中的邮件属性,
getRecipients()是一个特例,它返回一个含有所有先前被加入的收件人地址的数组。
出于安全原因,Zend_Mail了过滤邮件头中所有字段,
以防止基于换行符(\n)邮件头注入(header injection)漏洞攻击
通过SMTP发送邮件
以SMTP方式发送邮件,Zend_Mail_Transport_Smtp对象需要在send()方法被调用之前创建并注册到Zend_Mail中去。
当前脚本程序中所有使用Zend_Mail::send()发送的邮件,都将以SMTP方式传送:
<?php
require_once 'Zend/Mail/Transport/Smtp.php';
$tr = new Zend_Mail_Transport_Smtp('mail.example.com');
Zend_Mail::setDefaultTransport($tr);
?>
setDefaultTransport()方法的调用和Zend_Mail_Transport_Smtp对象的构造代价并不昂贵。
这两行可以添加在脚本程序的配置文件(例如config.inc或者类似文件)中,从而为整个脚本程序配置Zend_Mail类的行为。
如此可以把配置信息从应用程序逻辑分离出来邮件是通过SMTP还是mail()发送,使用什么邮件服务器等等。
通过一个SMTP连接发送多个邮件
在缺省状态下,系统会为每一个被发送的邮件建立一个SMTP连接。
如果想通过一个SMTP连接发送多个邮件,可以自己调用connect()函数。
如果在send()被调用之前就已经建立了一个传送连接,那么这个连接会被使用并且不会被自动关闭。
<?php
require_once 'Zend/Mail.php';
$mail = new Zend_Mail();
// build message...
require_once 'Zend/Mail/Transport/Smtp.php';
$tr = new Zend_Mail_Transport_Smtp('mail.example.com');
Zend_Mail::setDefaultTransport($tr);
$tr->connect();
for ($i = 0; $i < 5; $i++) {
$mail->send();
}
$tr->disconnect();
?>
使用不同的Transport对象
有时想想使用不同的连接来发送不同的邮件,
也可以不预先调用setDefaultTransport()方法,而直接将Transport对象传递给send()。
被传递的transport对象会在实际的send()调用中替代缺省的transport:
<?php
require_once 'Zend/Mail.php';
$mail = new Zend_Mail();
// build message...
require_once 'Zend/Mail/Transport/Smtp.php';
$tr1 = new Zend_Mail_Transport_Smtp('server@example.com');
$tr2 = new Zend_Mail_Transport_Smtp('other_server@example.com');
$mail->send($tr1);
$mail->send($tr2);
$mail->send(); // use default again
?>
外加transport,需要实现Zend_Mail_Transport_Interface接口。
HTML邮件
发送HTML格式的邮件,使用setBodyHTML()方法来设置邮件正文而不是setBodyText()方法,
MIME类型会被自动设置为text/html。 如果既使用HTML又使用纯文本,
那么multipart/alternative MIME类型的邮件消息将会自动产生:
<?php
require_once 'Zend/Mail.php';
$mail = new Zend_Mail();
$mail->setBodyText('My Nice Test Text');
$mail->setBodyHtml('My Nice <b>Test</b> Text');
$mail->setFrom('somebody@example.com', 'Some Sender');
$mail->addTo('somebody_else@example.com', 'Some Recipient');
$mail->setSubject('TestSubject');
$mail->send();
?>
附件
使用addAttachment()方法可以将文件附加到邮件中。
Zend_Mail会缺省地认为该文件是二进制对象(application/octet-stream),以 base64编码传输,
并且作为邮件的附件处理。 通过传递额外的参数给addAttachment()方法可以覆盖上述缺省设定:
<?php
require_once 'Zend/Mail.php';
$mail = new Zend_Mail();
// build message...
$mail->addAttachment($someBinaryString);
$mail->addAttachment($myImage, 'image/gif', Zend_Mime::DISPOSITION_INLINE, Zend_Mime::ENCODING_8BIT);
?>
如果想得到对此附件MIME段产生的更多控制,可以使用addAttachment()方法的返回值来修改它的属性。
方法addAttachment()返回了一个Zend_Mime_Part对象:
<?php
require_once 'Zend/Mail.php';
$mail = new Zend_Mail();
$at = $mail->addAttachment($myImage);
$at->type = 'image/gif';
$at->disposition = Zend_Mime::DISPOSITION_INLINE;
$at->encoding = Zend_Mime::ENCODING_8BIT;
$at->filename = 'test.gif';
$mail->send();
?>
增加收件人
有三种方法可以增加收件人:
addTo():增加一个收件人到邮件头“To”(收件人)
addCc():增加一个收件人到邮件头“Cc”(抄送)
addBcc():增加一个收件人到邮件头“Bcc”(暗送)
addTo()和addCc()接受可选的第二个参数用于为邮件头的收件人指定具有可读性的名字。
控制MIME分界线
在一个包含多个段的邮件里,用于分隔邮件不同段的MIME分界线(MIME boundary)通常是随机生成的。
但是在某些情况下,也许会希望使用特定的MIME分界线。
如下面的例子所示,可以使用setMimeBoundary()方法来做到这一点:
<?php
require_once 'Zend/Mail.php';
$mail = new Zend_Mail();
$mail->setMimeBoundary('=_' . md5(microtime(1) . $someId++);
// build message...
?>
外加邮件头信息
使用addHeader()方法可以外加任意的邮件头信息。它需要两个参数,头信息的名称和值,
第三个可选的参数,它决定了该邮件头信息是否可以有多个值:
<?php
require_once 'Zend/Mail.php';
$mail = new Zend_Mail();
$mail->addHeader('X-MailGenerator', 'MyCoolApplication');
$mail->addHeader('X-greetingsTo', 'Mom', true); // multiple values
$mail->addHeader('X-greetingsTo', 'Dad', true);
?>
字符集
Zend_Mail不会对邮件段字符集的正确性进行检查。 当Zend_Mail初始化的时候,
缺省地为邮件设置了iso8859-1字符集。
程序必须确保添加至邮件对象的所有段都以正确的字符集编码。
在添加一个新的邮件段时,可以为每个段指定不同的字符集。
字符集只适用于文本格式中的段。
编码
文本和HTML信息在缺省下以Quoted-printable机制编码。
在调用addAttachment()方法添加附件的时候, 如果没有指定编码方式,都将以base64方式编码,
或稍后通过给MIME段对象(Zend_Mime_Part)相应属性赋值来指定编码方式。
7Bit和8Bit编码目前仅仅在二进制数据上可行。
Zend_Mail_Transport_Smtp编码行以一个点(.)或者两个点(..)起始,以确保邮件不违反SMTP协议。
SMTP 身份验证
Zend_Mail 支持使用 SMTP 身份验证,通过配置数组传递“auth”参数到 Zend_Mail_Transport_Smtp 的构造函数中。
可用的内建身份验证方法为 PLAIN,LOGIN 和 CRAM-MD5,这些都需要在配置数组中设置“username”和“password”。
<?php
require_once 'Zend/Mail.php';
require_once 'Zend/Mail/Transport/Smtp.php';
$config = array('auth' => 'login',
'username' => 'myusername',
'password' => 'password');
$transport = new Zend_Mail_Transport_Smtp('mail.server.com', $config);
$mail = new Zend_Mail();
$mail->setBodyText('This is the text of the mail.');
$mail->setFrom('sender@test.com', 'Some Sender');
$mail->addTo('recipient@test.com', 'Some Recipient');
$mail->setSubject('TestSubject');
$mail->send($transport);
?>
身份验证的类型是大小写不敏感的,但是不能有标点符号。
例如,使用 CRAM-MD5 类型,则应当传递 'auth' => 'crammd5' 到 Zend_Mail_Transport_Smtp 的构造函数中。
Securing SMTP Transport 安全性
Zend_Mail also supports the use of either TLS or SSL to secure a SMTP connection.
This can be enabled be passing the 'ssl' parameter to the configuration array
in the Zend_Mail_Transport_Smtp constructor with a value of either 'ssl' or 'tls'.
A port can optionally be supplied, otherwise it defaults to 25 for TLS or 465 for SSL.
<?php
require_once 'Zend/Mail.php';
require_once 'Zend/Mail/Transport/Smtp.php';
$config = array('ssl' => 'tls',
'port' => 25); // Optional port number supplied
$transport = new Zend_Mail_Transport_Smtp('mail.server.com', $config);
$mail = new Zend_Mail();
$mail->setBodyText('This is the text of the mail.');
$mail->setFrom('sender@test.com', 'Some Sender');
$mail->addTo('recipient@test.com', 'Some Recipient');
$mail->setSubject('TestSubject');
$mail->send($transport);
?>
Reading Mail Messages 读取邮件
Zend_Mail can read mail messages from several local or remote mail storages.
All of them have the same basic API to count and fetch messages
and some of them implement additional interfaces for not so common features.
For a feature overview of the implemented storages see the following table.
Feature Mbox Maildir Pop3 IMAP
Storage type local local remote remote
Fetch message Yes Yes Yes Yes
Fetch mime-part emulated emulated emulated emulated
Folders Yes Yes No Yes
Create message/folder No todo No todo
Flags No Yes No Yes
Simple example using Pop3
<?php
$mail = new Zend_Mail_Storage_Pop3(array('host' => 'localhost',
'user' => 'test',
'password' => 'test'));
echo $mail->countMessages() . " messages found\n";
foreach ($mail as $message) {
echo "Mail from '{$message->from}': {$message->subject}\n";
}
?>
$mail = new Zend_Mail_Storage_Mbox(array('filename' => '/home/test/mail/inbox'));
$mail = new Zend_Mail_Storage_Maildir(array('dirname' => '/home/test/mail/'));
// connecting with Pop3
$mail = new Zend_Mail_Storage_Pop3(array('host' => 'example.com'
'user' => 'test',
'password' => 'test'));
// connecting with Imap
$mail = new Zend_Mail_Storage_Imap(array('host' => 'example.com'
'user' => 'test',
'password' => 'test'));
// example for a none standard port
$mail = new Zend_Mail_Storage_Pop3(array('host' => 'example.com',
'port' => 1120
'user' => 'test',
'password' => 'test'));
Advanced Use 高级应用
Using NOOP
Caching instances
Extending Protocol Classes