无为清净楼资源网 Design By www.qnjia.com

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

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

Zend_Mail通过PHP内建的mail()函数或者直接通过SMTP连接来发送邮件。

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

步骤如下

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

案例:

<"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 &quot;localhost&quot; port 25, verify your &quot;SMTP&quot; and &quot;smtp_port&quot; 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服务器。

源码分析:

<"' . $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) "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程序设计有所帮助。

标签:
Zend,Framework,Zend_Mail

无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。