Zend_Mail-Introduction.xml 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <sect1 id="zend.mail.introduction">
  2. <title> 简介 </title>
  3. <sect2 id="zend.mail.introduction.getting-started">
  4. <title> 起步 </title>
  5. <para>
  6. <code>Zend_Mail</code> 提供了通用化的功能来撰写和发送文本以及兼容 MIME 标准的含有多个段的邮件消息。邮件在 <code>Zend_Mail</code> 里通过缺省的 <code>Zend_Mail_Transport_Sendmail</code> 传输或通过 <code>Zend_Mail_Transport_Smtp</code> 来发送。
  7. </para>
  8. <example id="zend.mail.introduction.example-1">
  9. <title> 使用Zend_Mail发送简单邮件 </title>
  10. <para>
  11. 一个简单邮件由一个或者几个收件人,一个主题,一个邮件主体和一个发件人组成。下面的步骤,使用 <code>Zend_Mail_Transport_Sendmail</code> 来发送邮件:
  12. </para>
  13. <programlisting role="php"><![CDATA[<?php
  14. require_once 'Zend/Mail.php';
  15. $mail = new Zend_Mail();
  16. $mail->setBodyText('This is the text of the mail.');
  17. $mail->setFrom('somebody@example.com', 'Some Sender');
  18. $mail->addTo('somebody_else@example.com', 'Some Recipient');
  19. $mail->setSubject('TestSubject');
  20. $mail->send();]]>
  21. </programlisting>
  22. </example>
  23. <note>
  24. <title>Minimum definitions</title>
  25. <para>
  26. 使用<code>Zend_Mail</code>来发送邮件,你至少得指定一个收件人, 一个发件人(例如通过<code>setFrom()</code>方法)和一个邮件消息主体(文本 和/或者 HTML)。
  27. </para>
  28. </note>
  29. <para>
  30. 通过“get”方法可以读取绝大多数储存在“mail”对象中的邮件属性,更进一步的细节请参阅API文档。<code>getRecipients()</code>是一个特例,它返回一个含有所有先前被加入的收件人地址的数组。
  31. </para>
  32. <para>
  33. 出于安全原因,<code>Zend_Mail</code> 过滤了邮件头中所有字段,以防止基于换行符(<code>\n</code>)邮件头注入(header injection)漏洞攻击。
  34. </para>
  35. <para>
  36. 你也可以使用大部分带有方便的 fluent interface 的 <code>Zend_Mail</code> 对象的方法。 Fluent interface 意思是每个方法返回一个引用到它被调用的对象,所以你可以立即调用其它方法。
  37. </para>
  38. <programlisting role="php"><![CDATA[<?php
  39. require_once 'Zend/Mail.php';
  40. $mail = new Zend_Mail();
  41. $mail->setBodyText('This is the text of the mail.')
  42. ->setFrom('somebody@example.com', 'Some Sender')
  43. ->addTo('somebody_else@example.com', 'Some Recipient')
  44. ->setSubject('TestSubject')
  45. ->send();]]>
  46. </programlisting>
  47. </sect2>
  48. <sect2 id="zend.mail.introduction.sendmail">
  49. <title> 配置缺省的 sendmail 传送器(transport) </title>
  50. <para>
  51. <code>Zend_Mail</code> 实例的缺省的传送器是 <code>Zend_Mail_Transport_Sendmail</code>,它是 PHP <ulink url="http://php.net/mail"><code>mail()</code></ulink> 函数的基本封装。如果你想传递另外的参数给 <ulink url="http://php.net/mail"><code>mail()</code></ulink> 函数,只需要创建一个新的传送器实例并传递参数给构造器。新的传送器实例可以当作缺省的 <code>Zend_Mail</code> 的传送器,或者它可以被传递给 <code>Zend_Mail</code> 的 <code>send()</code> 方法。
  52. </para>
  53. <example id="zend.mail.introduction.sendmail.example-1">
  54. <title> 传递另外的参数给 Zend_Mail_Transport_Sendmail 传送器 </title>
  55. <para>
  56. 这个例子示范如何修改 <ulink url="http://php.net/mail"><code>mail()</code></ulink> 函数的返回路径。
  57. </para>
  58. <programlisting role="php"><![CDATA[<?php
  59. require_once 'Zend/Mail.php';
  60. require_once 'Zend/Mail/Transport/Sendmail.php';
  61. $tr = new Zend_Mail_Transport_Sendmail('-freturn_to_me@example.com');
  62. Zend_Mail::setDefaultTransport($tr);
  63. $mail = new Zend_Mail();
  64. $mail->setBodyText('This is the text of the mail.');
  65. $mail->setFrom('somebody@example.com', 'Some Sender');
  66. $mail->addTo('somebody_else@example.com', 'Some Recipient');
  67. $mail->setSubject('TestSubject');
  68. $mail->send();]]>
  69. </programlisting>
  70. </example>
  71. <note>
  72. <title> 安全模式限制 </title>
  73. <para>
  74. 如果 PHP 运行在安全模式,另外的可选参数将导致 <ulink url="http://php.net/mail"><code>mail()</code></ulink> 函数失败。
  75. </para>
  76. </note>
  77. </sect2>
  78. </sect1>
  79. <!--
  80. vim:se ts=4 sw=4 et:
  81. -->