欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Zend Framework入门教程之Zend_Mail用法示例

程序员文章站 2024-04-01 20:48:28
本文实例讲述了zend framework入门教程之zend_mail用法。分享给大家供大家参考,具体如下: zend_mail组件提供了通用化的功能来创建和发送文本。...

本文实例讲述了zend framework入门教程之zend_mail用法。分享给大家供大家参考,具体如下:

zend_mail组件提供了通用化的功能来创建和发送文本。

zend_mail通过php内建的mail()函数或者直接通过smtp连接来发送邮件。

一个简单的邮件由收件人、主题、邮件内容以及发件人等内容组成。

步骤如下

1.创建对象
2.设置邮件内容
3.发送

案例:

<?php
require_once "zend/mail.php";
$my_mail = new zend_mail();             //创建一个对象
$my_mail->addto("jiqing9006@126.com","jim");    //添加一个收件人
$my_mail->setsubject("just a test");        //设置主题
$my_mail->setbodytext("hello jim!");        //为邮件设置正文内容
$my_mail->setfrom("706507884@qq.com","jiqing");   //为邮件设置发件人
echo "邮件设置完毕";
echo "<p>";
echo "邮件收件人为:";
$result = $my_mail->getheaders();
echo $result['to'][0];
echo "<p>";
echo "邮件主题为:";
echo $my_mail->getsubject();
echo "<p>";
echo "邮件内容为:";
$result = $my_mail->getbodytext();
echo $result->getcontent();
echo "<p>";
echo "邮件发件人为:";
echo $my_mail->getfrom();
echo "<p>";
$my_mail->send();

结果:

邮件设置完毕
邮件收件人为:jim
邮件主题为:just a test
邮件内容为:hello jim!
邮件发件人为:706507884@qq.com

fatal error: uncaught exception 'zend_mail_transport_exception' with message 'unable to send mail. mail() [<a href='function.mail'>function.mail</a>]: failed to connect to mailserver at "localhost" port 25, verify your "smtp" and "smtp_port" setting in php.ini or use ini_set()' in c:\zend\library\zend\mail\transport\sendmail.php:137 stack trace: #0 c:\zend\library\zend\mail\transport\abstract.php(348): zend_mail_transport_sendmail->_sendmail() #1 c:\zend\library\zend\mail.php(1194): zend_mail_transport_abstract->send(object(zend_mail)) #2 d:\xampp\htdocs\test.php(24): zend_mail->send() #3 {main} thrown in c:\zend\library\zend\mail\transport\sendmail.php on line 137

点评:

这里执行不能成功,是因为没有配置好mail服务器。

源码分析:

<?php
/**
 * zend framework
 *
 * license
 *
 * this source file is subject to the new bsd license that is bundled
 * with this package in the file license.txt.
 * it is also available through the world-wide-web at this url:
 * http://framework.zend.com/license/new-bsd
 * if you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category  zend
 * @package  zend_mail
 * @copyright copyright (c) 2005-2012 zend technologies usa inc. (http://www.zend.com)
 * @license  http://framework.zend.com/license/new-bsd   new bsd license
 * @version  $id: mail.php 24593 2012-01-05 20:35:02z matthew $
 */
/**
 * @see zend_mail_transport_abstract
 */
require_once 'zend/mail/transport/abstract.php';
/**
 * @see zend_mime
 */
require_once 'zend/mime.php';
/**
 * @see zend_mime_message
 */
require_once 'zend/mime/message.php';
/**
 * @see zend_mime_part
 */
require_once 'zend/mime/part.php';
/**
 * class for sending an email.
 *
 * @category  zend
 * @package  zend_mail
 * @copyright copyright (c) 2005-2012 zend technologies usa inc. (http://www.zend.com)
 * @license  http://framework.zend.com/license/new-bsd   new bsd license
 */
class zend_mail extends zend_mime_message
{
  /**#@+
   * @access protected
   */
  /**
   * @var zend_mail_transport_abstract
   * @static
   */
  protected static $_defaulttransport = null;
  /**
   * @var array
   * @static
   */
  protected static $_defaultfrom;
  /**
   * @var array
   * @static
   */
  protected static $_defaultreplyto;
  /**
   * mail character set
   * @var string
   */
  protected $_charset = 'iso-8859-1';
  /**
   * mail headers
   * @var array
   */
  protected $_headers = array();
  /**
   * encoding of mail headers
   * @var string
   */
  protected $_headerencoding = zend_mime::encoding_quotedprintable;
  /**
   * from: address
   * @var string
   */
  protected $_from = null;
  /**
   * to: addresses
   * @var array
   */
  protected $_to = array();
  /**
   * array of all recipients
   * @var array
   */
  protected $_recipients = array();
  /**
   * reply-to header
   * @var string
   */
  protected $_replyto = null;
  /**
   * return-path header
   * @var string
   */
  protected $_returnpath = null;
  /**
   * subject: header
   * @var string
   */
  protected $_subject = null;
  /**
   * date: header
   * @var string
   */
  protected $_date = null;
  /**
   * message-id: header
   * @var string
   */
  protected $_messageid = null;
  /**
   * text/plain mime part
   * @var false|zend_mime_part
   */
  protected $_bodytext = false;
  /**
   * text/html mime part
   * @var false|zend_mime_part
   */
  protected $_bodyhtml = false;
  /**
   * mime boundary string
   * @var string
   */
  protected $_mimeboundary = null;
  /**
   * content type of the message
   * @var string
   */
  protected $_type = null;
  /**#@-*/
  /**
   * flag: whether or not email has attachments
   * @var boolean
   */
  public $hasattachments = false;
  /**
   * sets the default mail transport for all following uses of
   * zend_mail::send();
   *
   * @todo allow passing a string to indicate the transport to load
   * @todo allow passing in optional options for the transport to load
   * @param zend_mail_transport_abstract $transport
   */
  public static function setdefaulttransport(zend_mail_transport_abstract $transport)
  {
    self::$_defaulttransport = $transport;
  }
  /**
   * gets the default mail transport for all following uses of
   * unittests
   *
   * @todo allow passing a string to indicate the transport to load
   * @todo allow passing in optional options for the transport to load
   */
  public static function getdefaulttransport()
  {
    return self::$_defaulttransport;
  }
  /**
   * clear the default transport property
   */
  public static function cleardefaulttransport()
  {
    self::$_defaulttransport = null;
  }
  /**
   * public constructor
   *
   * @param string $charset
   * @return void
   */
  public function __construct($charset = null)
  {
    if ($charset != null) {
      $this->_charset = $charset;
    }
  }
  /**
   * return charset string
   *
   * @return string
   */
  public function getcharset()
  {
    return $this->_charset;
  }
  /**
   * set content type
   *
   * should only be used for manually setting multipart content types.
   *
   * @param string $type content type
   * @return zend_mail implements fluent interface
   * @throws zend_mail_exception for types not supported by zend_mime
   */
  public function settype($type)
  {
    $allowed = array(
      zend_mime::multipart_alternative,
      zend_mime::multipart_mixed,
      zend_mime::multipart_related,
    );
    if (!in_array($type, $allowed)) {
      /**
       * @see zend_mail_exception
       */
      require_once 'zend/mail/exception.php';
      throw new zend_mail_exception('invalid content type "' . $type . '"');
    }
    $this->_type = $type;
    return $this;
  }
  /**
   * get content type of the message
   *
   * @return string
   */
  public function gettype()
  {
    return $this->_type;
  }
  /**
   * set an arbitrary mime boundary for the message
   *
   * if not set, zend_mime will generate one.
   *
   * @param string  $boundary
   * @return zend_mail provides fluent interface
   */
  public function setmimeboundary($boundary)
  {
    $this->_mimeboundary = $boundary;
    return $this;
  }
  /**
   * return the boundary string used for the message
   *
   * @return string
   */
  public function getmimeboundary()
  {
    return $this->_mimeboundary;
  }
  /**
   * return encoding of mail headers
   *
   * @deprecated use {@link getheaderencoding()} instead
   * @return string
   */
  public function getencodingofheaders()
  {
    return $this->getheaderencoding();
  }
  /**
   * return the encoding of mail headers
   *
   * either zend_mime::encoding_quotedprintable or zend_mime::encoding_base64
   *
   * @return string
   */
  public function getheaderencoding()
  {
    return $this->_headerencoding;
  }
  /**
   * set the encoding of mail headers
   *
   * @deprecated use {@link setheaderencoding()} instead.
   * @param string $encoding
   * @return zend_mail
   */
  public function setencodingofheaders($encoding)
  {
    return $this->setheaderencoding($encoding);
  }
  /**
   * set the encoding of mail headers
   *
   * @param string $encoding zend_mime::encoding_quotedprintable or zend_mime::encoding_base64
   * @return zend_mail provides fluent interface
   */
  public function setheaderencoding($encoding)
  {
    $allowed = array(
      zend_mime::encoding_base64,
      zend_mime::encoding_quotedprintable
    );
    if (!in_array($encoding, $allowed)) {
      /**
       * @see zend_mail_exception
       */
      require_once 'zend/mail/exception.php';
      throw new zend_mail_exception('invalid encoding "' . $encoding . '"');
    }
    $this->_headerencoding = $encoding;
    return $this;
  }
  /**
   * sets the text body for the message.
   *
   * @param string $txt
   * @param string $charset
   * @param string $encoding
   * @return zend_mail provides fluent interface
  */
  public function setbodytext($txt, $charset = null, $encoding = zend_mime::encoding_quotedprintable)
  {
    if ($charset === null) {
      $charset = $this->_charset;
    }
    $mp = new zend_mime_part($txt);
    $mp->encoding = $encoding;
    $mp->type = zend_mime::type_text;
    $mp->disposition = zend_mime::disposition_inline;
    $mp->charset = $charset;
    $this->_bodytext = $mp;
    return $this;
  }
  /**
   * return text body zend_mime_part or string
   *
   * @param bool textonly whether to return just the body text content or the mime part; defaults to false, the mime part
   * @return false|zend_mime_part|string
   */
  public function getbodytext($textonly = false)
  {
    if ($textonly && $this->_bodytext) {
      $body = $this->_bodytext;
      return $body->getcontent();
    }
    return $this->_bodytext;
  }
  /**
   * sets the html body for the message
   *
   * @param string  $html
   * @param string  $charset
   * @param string  $encoding
   * @return zend_mail provides fluent interface
   */
  public function setbodyhtml($html, $charset = null, $encoding = zend_mime::encoding_quotedprintable)
  {
    if ($charset === null) {
      $charset = $this->_charset;
    }
    $mp = new zend_mime_part($html);
    $mp->encoding = $encoding;
    $mp->type = zend_mime::type_html;
    $mp->disposition = zend_mime::disposition_inline;
    $mp->charset = $charset;
    $this->_bodyhtml = $mp;
    return $this;
  }
  /**
   * return zend_mime_part representing body html
   *
   * @param bool $htmlonly whether to return the body html only, or the mime part; defaults to false, the mime part
   * @return false|zend_mime_part|string
   */
  public function getbodyhtml($htmlonly = false)
  {
    if ($htmlonly && $this->_bodyhtml) {
      $body = $this->_bodyhtml;
      return $body->getcontent();
    }
    return $this->_bodyhtml;
  }
  /**
   * adds an existing attachment to the mail message
   *
   * @param zend_mime_part $attachment
   * @return zend_mail provides fluent interface
   */
  public function addattachment(zend_mime_part $attachment)
  {
    $this->addpart($attachment);
    $this->hasattachments = true;
    return $this;
  }
  /**
   * creates a zend_mime_part attachment
   *
   * attachment is automatically added to the mail object after creation. the
   * attachment object is returned to allow for further manipulation.
   *
   * @param string     $body
   * @param string     $mimetype
   * @param string     $disposition
   * @param string     $encoding
   * @param string     $filename optional a filename for the attachment
   * @return zend_mime_part newly created zend_mime_part object (to allow
   * advanced settings)
   */
  public function createattachment($body,
                   $mimetype  = zend_mime::type_octetstream,
                   $disposition = zend_mime::disposition_attachment,
                   $encoding  = zend_mime::encoding_base64,
                   $filename  = null)
  {
    $mp = new zend_mime_part($body);
    $mp->encoding = $encoding;
    $mp->type = $mimetype;
    $mp->disposition = $disposition;
    $mp->filename = $filename;
    $this->addattachment($mp);
    return $mp;
  }
  /**
   * return a count of message parts
   *
   * @return integer
   */
  public function getpartcount()
  {
    return count($this->_parts);
  }
  /**
   * encode header fields
   *
   * encodes header content according to rfc1522 if it contains non-printable
   * characters.
   *
   * @param string $value
   * @return string
   */
  protected function _encodeheader($value)
  {
    if (zend_mime::isprintable($value) === false) {
      if ($this->getheaderencoding() === zend_mime::encoding_quotedprintable) {
        $value = zend_mime::encodequotedprintableheader($value, $this->getcharset(), zend_mime::linelength, zend_mime::lineend);
      } else {
        $value = zend_mime::encodebase64header($value, $this->getcharset(), zend_mime::linelength, zend_mime::lineend);
      }
    }
    return $value;
  }
  /**
   * add a header to the message
   *
   * adds a header to this message. if append is true and the header already
   * exists, raises a flag indicating that the header should be appended.
   *
   * @param string $headername
   * @param string $value
   * @param bool $append
   */
  protected function _storeheader($headername, $value, $append = false)
  {
    if (isset($this->_headers[$headername])) {
      $this->_headers[$headername][] = $value;
    } else {
      $this->_headers[$headername] = array($value);
    }
    if ($append) {
      $this->_headers[$headername]['append'] = true;
    }
  }
  /**
   * clear header from the message
   *
   * @param string $headername
   * @deprecated use public method directly
   */
  protected function _clearheader($headername)
  {
    $this->clearheader($headername);
  }
  /**
   * helper function for adding a recipient and the corresponding header
   *
   * @param string $headername
   * @param string $email
   * @param string $name
   */
  protected function _addrecipientandheader($headername, $email, $name)
  {
    $email = $this->_filteremail($email);
    $name = $this->_filtername($name);
    // prevent duplicates
    $this->_recipients[$email] = 1;
    $this->_storeheader($headername, $this->_formataddress($email, $name), true);
  }
  /**
   * adds to-header and recipient, $email can be an array, or a single string address
   *
   * @param string|array $email
   * @param string $name
   * @return zend_mail provides fluent interface
   */
  public function addto($email, $name='')
  {
    if (!is_array($email)) {
      $email = array($name => $email);
    }
    foreach ($email as $n => $recipient) {
      $this->_addrecipientandheader('to', $recipient, is_int($n) ? '' : $n);
      $this->_to[] = $recipient;
    }
    return $this;
  }
  /**
   * adds cc-header and recipient, $email can be an array, or a single string address
   *
   * @param string|array  $email
   * @param string  $name
   * @return zend_mail provides fluent interface
   */
  public function addcc($email, $name='')
  {
    if (!is_array($email)) {
      $email = array($name => $email);
    }
    foreach ($email as $n => $recipient) {
      $this->_addrecipientandheader('cc', $recipient, is_int($n) ? '' : $n);
    }
    return $this;
  }
  /**
   * adds bcc recipient, $email can be an array, or a single string address
   *
   * @param string|array  $email
   * @return zend_mail provides fluent interface
   */
  public function addbcc($email)
  {
    if (!is_array($email)) {
      $email = array($email);
    }
    foreach ($email as $recipient) {
      $this->_addrecipientandheader('bcc', $recipient, '');
    }
    return $this;
  }
  /**
   * return list of recipient email addresses
   *
   * @return array (of strings)
   */
  public function getrecipients()
  {
    return array_keys($this->_recipients);
  }
  /**
   * clear header from the message
   *
   * @param string $headername
   * @return zend_mail provides fluent inter
   */
  public function clearheader($headername)
  {
    if (isset($this->_headers[$headername])){
      unset($this->_headers[$headername]);
    }
    return $this;
  }
  /**
   * clears list of recipient email addresses
   *
   * @return zend_mail provides fluent interface
   */
  public function clearrecipients()
  {
    $this->_recipients = array();
    $this->_to = array();
    $this->clearheader('to');
    $this->clearheader('cc');
    $this->clearheader('bcc');
    return $this;
  }
  /**
   * sets from-header and sender of the message
   *
   * @param string  $email
   * @param string  $name
   * @return zend_mail provides fluent interface
   * @throws zend_mail_exception if called subsequent times
   */
  public function setfrom($email, $name = null)
  {
    if (null !== $this->_from) {
      /**
       * @see zend_mail_exception
       */
      require_once 'zend/mail/exception.php';
      throw new zend_mail_exception('from header set twice');
    }
    $email = $this->_filteremail($email);
    $name = $this->_filtername($name);
    $this->_from = $email;
    $this->_storeheader('from', $this->_formataddress($email, $name), true);
    return $this;
  }
  /**
   * set reply-to header
   *
   * @param string $email
   * @param string $name
   * @return zend_mail
   * @throws zend_mail_exception if called more than one time
   */
  public function setreplyto($email, $name = null)
  {
    if (null !== $this->_replyto) {
      /**
       * @see zend_mail_exception
       */
      require_once 'zend/mail/exception.php';
      throw new zend_mail_exception('reply-to header set twice');
    }
    $email = $this->_filteremail($email);
    $name = $this->_filtername($name);
    $this->_replyto = $email;
    $this->_storeheader('reply-to', $this->_formataddress($email, $name), true);
    return $this;
  }
  /**
   * returns the sender of the mail
   *
   * @return string
   */
  public function getfrom()
  {
    return $this->_from;
  }
  /**
   * returns the current reply-to address of the message
   *
   * @return string|null reply-to address, null when not set
   */
  public function getreplyto()
  {
    return $this->_replyto;
  }
  /**
   * clears the sender from the mail
   *
   * @return zend_mail provides fluent interface
   */
  public function clearfrom()
  {
    $this->_from = null;
    $this->clearheader('from');
    return $this;
  }
   /**
   * clears the current reply-to address from the message
   *
   * @return zend_mail provides fluent interface
   */
  public function clearreplyto()
  {
    $this->_replyto = null;
    $this->clearheader('reply-to');
    return $this;
  }
  /**
   * sets default from-email and name of the message
   *
   * @param string        $email
   * @param string  optional  $name
   * @return void
   */
  public static function setdefaultfrom($email, $name = null)
  {
    self::$_defaultfrom = array('email' => $email, 'name' => $name);
  }
  /**
   * returns the default sender of the mail
   *
   * @return null|array  null if none was set.
   */
  public static function getdefaultfrom()
  {
    return self::$_defaultfrom;
  }
  /**
   * clears the default sender from the mail
   *
   * @return void
   */
  public static function cleardefaultfrom()
  {
    self::$_defaultfrom = null;
  }
  /**
   * sets from-name and -email based on the defaults
   *
   * @return zend_mail provides fluent interface
   */
  public function setfromtodefaultfrom() {
    $from = self::getdefaultfrom();
    if($from === null) {
      require_once 'zend/mail/exception.php';
      throw new zend_mail_exception(
        'no default from address set to use');
    }
    $this->setfrom($from['email'], $from['name']);
    return $this;
  }
  /**
   * sets default replyto-address and -name of the message
   *
   * @param string        $email
   * @param string  optional  $name
   * @return void
   */
  public static function setdefaultreplyto($email, $name = null)
  {
    self::$_defaultreplyto = array('email' => $email, 'name' => $name);
  }
  /**
   * returns the default reply-to address and name of the mail
   *
   * @return null|array  null if none was set.
   */
  public static function getdefaultreplyto()
  {
    return self::$_defaultreplyto;
  }
  /**
   * clears the default replyto-address and -name from the mail
   *
   * @return void
   */
  public static function cleardefaultreplyto()
  {
    self::$_defaultreplyto = null;
  }
  /**
   * sets replyto-name and -email based on the defaults
   *
   * @return zend_mail provides fluent interface
   */
  public function setreplytofromdefault() {
    $replyto = self::getdefaultreplyto();
    if($replyto === null) {
      require_once 'zend/mail/exception.php';
      throw new zend_mail_exception(
        'no default reply-to address set to use');
    }
    $this->setreplyto($replyto['email'], $replyto['name']);
    return $this;
  }
  /**
   * sets the return-path header of the message
   *
   * @param string  $email
   * @return zend_mail provides fluent interface
   * @throws zend_mail_exception if set multiple times
   */
  public function setreturnpath($email)
  {
    if ($this->_returnpath === null) {
      $email = $this->_filteremail($email);
      $this->_returnpath = $email;
      $this->_storeheader('return-path', $email, false);
    } else {
      /**
       * @see zend_mail_exception
       */
      require_once 'zend/mail/exception.php';
      throw new zend_mail_exception('return-path header set twice');
    }
    return $this;
  }
  /**
   * returns the current return-path address of the message
   *
   * if no return-path header is set, returns the value of {@link $_from}.
   *
   * @return string
   */
  public function getreturnpath()
  {
    if (null !== $this->_returnpath) {
      return $this->_returnpath;
    }
    return $this->_from;
  }
  /**
   * clears the current return-path address from the message
   *
   * @return zend_mail provides fluent interface
   */
  public function clearreturnpath()
  {
    $this->_returnpath = null;
    $this->clearheader('return-path');
    return $this;
  }
  /**
   * sets the subject of the message
   *
   * @param  string  $subject
   * @return zend_mail provides fluent interface
   * @throws zend_mail_exception
   */
  public function setsubject($subject)
  {
    if ($this->_subject === null) {
      $subject = $this->_filterother($subject);
      $this->_subject = $this->_encodeheader($subject);
      $this->_storeheader('subject', $this->_subject);
    } else {
      /**
       * @see zend_mail_exception
       */
      require_once 'zend/mail/exception.php';
      throw new zend_mail_exception('subject set twice');
    }
    return $this;
  }
  /**
   * returns the encoded subject of the message
   *
   * @return string
   */
  public function getsubject()
  {
    return $this->_subject;
  }
  /**
   * clears the encoded subject from the message
   *
   * @return zend_mail provides fluent interface
   */
  public function clearsubject()
  {
    $this->_subject = null;
    $this->clearheader('subject');
    return $this;
  }
  /**
   * sets date-header
   *
   * @param timestamp|string|zend_date $date
   * @return zend_mail provides fluent interface
   * @throws zend_mail_exception if called subsequent times or wrong date format.
   */
  public function setdate($date = null)
  {
    if ($this->_date === null) {
      if ($date === null) {
        $date = date('r');
      } else if (is_int($date)) {
        $date = date('r', $date);
      } else if (is_string($date)) {
        $date = strtotime($date);
        if ($date === false || $date < 0) {
          /**
           * @see zend_mail_exception
           */
          require_once 'zend/mail/exception.php';
          throw new zend_mail_exception('string representations of date header must be ' .
                         'strtotime()-compatible');
        }
        $date = date('r', $date);
      } else if ($date instanceof zend_date) {
        $date = $date->get(zend_date::rfc_2822);
      } else {
        /**
         * @see zend_mail_exception
         */
        require_once 'zend/mail/exception.php';
        throw new zend_mail_exception(__method__ . ' only accepts unix timestamps, zend_date objects, ' .
                       ' and strtotime()-compatible strings');
      }
      $this->_date = $date;
      $this->_storeheader('date', $date);
    } else {
      /**
       * @see zend_mail_exception
       */
      require_once 'zend/mail/exception.php';
      throw new zend_mail_exception('date header set twice');
    }
    return $this;
  }
  /**
   * returns the formatted date of the message
   *
   * @return string
   */
  public function getdate()
  {
    return $this->_date;
  }
  /**
   * clears the formatted date from the message
   *
   * @return zend_mail provides fluent interface
   */
  public function cleardate()
  {
    $this->_date = null;
    $this->clearheader('date');
    return $this;
  }
  /**
   * sets the message-id of the message
   *
   * @param  boolean|string $id
   * true :auto
   * false :no set
   * null :no set
   * string:sets given string (angle brackets is not necessary)
   * @return zend_mail provides fluent interface
   * @throws zend_mail_exception
   */
  public function setmessageid($id = true)
  {
    if ($id === null || $id === false) {
      return $this;
    } elseif ($id === true) {
      $id = $this->createmessageid();
    }
    if ($this->_messageid === null) {
      $id = $this->_filterother($id);
      $this->_messageid = $id;
      $this->_storeheader('message-id', '<' . $this->_messageid . '>');
    } else {
      /**
       * @see zend_mail_exception
       */
      require_once 'zend/mail/exception.php';
      throw new zend_mail_exception('message-id set twice');
    }
    return $this;
  }
  /**
   * returns the message-id of the message
   *
   * @return string
   */
  public function getmessageid()
  {
    return $this->_messageid;
  }
  /**
   * clears the message-id from the message
   *
   * @return zend_mail provides fluent interface
   */
  public function clearmessageid()
  {
    $this->_messageid = null;
    $this->clearheader('message-id');
    return $this;
  }
  /**
   * creates the message-id
   *
   * @return string
   */
  public function createmessageid() {
    $time = time();
    if ($this->_from !== null) {
      $user = $this->_from;
    } elseif (isset($_server['remote_addr'])) {
      $user = $_server['remote_addr'];
    } else {
      $user = getmypid();
    }
    $rand = mt_rand();
    if ($this->_recipients !== array()) {
      $recipient = array_rand($this->_recipients);
    } else {
      $recipient = 'unknown';
    }
    if (isset($_server["server_name"])) {
      $hostname = $_server["server_name"];
    } else {
      $hostname = php_uname('n');
    }
    return sha1($time . $user . $rand . $recipient) . '@' . $hostname;
  }
  /**
   * add a custom header to the message
   *
   * @param string       $name
   * @param string       $value
   * @param boolean       $append
   * @return zend_mail      provides fluent interface
   * @throws zend_mail_exception on attempts to create standard headers
   */
  public function addheader($name, $value, $append = false)
  {
    $prohibit = array('to', 'cc', 'bcc', 'from', 'subject',
             'reply-to', 'return-path',
             'date', 'message-id',
             );
    if (in_array(strtolower($name), $prohibit)) {
      /**
       * @see zend_mail_exception
       */
      require_once 'zend/mail/exception.php';
      throw new zend_mail_exception('cannot set standard header from addheader()');
    }
    $value = $this->_filterother($value);
    $value = $this->_encodeheader($value);
    $this->_storeheader($name, $value, $append);
    return $this;
  }
  /**
   * return mail headers
   *
   * @return void
   */
  public function getheaders()
  {
    return $this->_headers;
  }
  /**
   * sends this email using the given transport or a previously
   * set defaulttransport or the internal mail function if no
   * default transport had been set.
   *
   * @param zend_mail_transport_abstract $transport
   * @return zend_mail          provides fluent interface
   */
  public function send($transport = null)
  {
    if ($transport === null) {
      if (! self::$_defaulttransport instanceof zend_mail_transport_abstract) {
        require_once 'zend/mail/transport/sendmail.php';
        $transport = new zend_mail_transport_sendmail();
      } else {
        $transport = self::$_defaulttransport;
      }
    }
    if ($this->_date === null) {
      $this->setdate();
    }
    if(null === $this->_from && null !== self::getdefaultfrom()) {
      $this->setfromtodefaultfrom();
    }
    if(null === $this->_replyto && null !== self::getdefaultreplyto()) {
      $this->setreplytofromdefault();
    }
    $transport->send($this);
    return $this;
  }
  /**
   * filter of email data
   *
   * @param string $email
   * @return string
   */
  protected function _filteremail($email)
  {
    $rule = array("\r" => '',
           "\n" => '',
           "\t" => '',
           '"' => '',
           ',' => '',
           '<' => '',
           '>' => '',
    );
    return strtr($email, $rule);
  }
  /**
   * filter of name data
   *
   * @param string $name
   * @return string
   */
  protected function _filtername($name)
  {
    $rule = array("\r" => '',
           "\n" => '',
           "\t" => '',
           '"' => "'",
           '<' => '[',
           '>' => ']',
    );
    return trim(strtr($name, $rule));
  }
  /**
   * filter of other data
   *
   * @param string $data
   * @return string
   */
  protected function _filterother($data)
  {
    $rule = array("\r" => '',
           "\n" => '',
           "\t" => '',
    );
    return strtr($data, $rule);
  }
  /**
   * formats e-mail address
   *
   * @param string $email
   * @param string $name
   * @return string
   */
  protected function _formataddress($email, $name)
  {
    if ($name === '' || $name === null || $name === $email) {
      return $email;
    } else {
      $encodedname = $this->_encodeheader($name);
      if ($encodedname === $name && strcspn($name, '()<>[]:;@\\,') != strlen($name)) {
        $format = '"%s" <%s>';
      } else {
        $format = '%s <%s>';
      }
      return sprintf($format, $encodedname, $email);
    }
  }
}

更多关于zend相关内容感兴趣的读者可查看本站专题:《zend framework框架入门教程》、《php优秀开发框架总结》、《yii框架入门及常用技巧总结》、《thinkphp入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家基于zend framework框架的php程序设计有所帮助。