PageRenderTime 57ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/zend/classes/Zend/Mail.php

https://github.com/dari88/kohanaPress-Sample_Code_for_kohana_3.3
PHP | 1276 lines | 561 code | 148 blank | 567 comment | 60 complexity | c75f606ac98b84d12b3a030a2841472c MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. <?php defined('SYSPATH') OR die('No direct script access.');
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Mail
  17. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Mail.php 23775 2011-03-01 17:25:24Z ralph $
  20. */
  21. /**
  22. * @see Zend_Mail_Transport_Abstract
  23. */
  24. // require_once 'Zend/Mail/Transport/Abstract.php';
  25. /**
  26. * @see Zend_Mime
  27. */
  28. // require_once 'Zend/Mime.php';
  29. /**
  30. * @see Zend_Mime_Message
  31. */
  32. // require_once 'Zend/Mime/Message.php';
  33. /**
  34. * @see Zend_Mime_Part
  35. */
  36. // require_once 'Zend/Mime/Part.php';
  37. /**
  38. * Class for sending an email.
  39. *
  40. * @category Zend
  41. * @package Zend_Mail
  42. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  43. * @license http://framework.zend.com/license/new-bsd New BSD License
  44. */
  45. class Zend_Mail extends Zend_Mime_Message
  46. {
  47. /**#@+
  48. * @access protected
  49. */
  50. /**
  51. * @var Zend_Mail_Transport_Abstract
  52. * @static
  53. */
  54. protected static $_defaultTransport = null;
  55. /**
  56. * @var array
  57. * @static
  58. */
  59. protected static $_defaultFrom;
  60. /**
  61. * @var array
  62. * @static
  63. */
  64. protected static $_defaultReplyTo;
  65. /**
  66. * Mail character set
  67. * @var string
  68. */
  69. protected $_charset = 'iso-8859-1';
  70. /**
  71. * Mail headers
  72. * @var array
  73. */
  74. protected $_headers = array();
  75. /**
  76. * Encoding of Mail headers
  77. * @var string
  78. */
  79. protected $_headerEncoding = Zend_Mime::ENCODING_QUOTEDPRINTABLE;
  80. /**
  81. * From: address
  82. * @var string
  83. */
  84. protected $_from = null;
  85. /**
  86. * To: addresses
  87. * @var array
  88. */
  89. protected $_to = array();
  90. /**
  91. * Array of all recipients
  92. * @var array
  93. */
  94. protected $_recipients = array();
  95. /**
  96. * Reply-To header
  97. * @var string
  98. */
  99. protected $_replyTo = null;
  100. /**
  101. * Return-Path header
  102. * @var string
  103. */
  104. protected $_returnPath = null;
  105. /**
  106. * Subject: header
  107. * @var string
  108. */
  109. protected $_subject = null;
  110. /**
  111. * Date: header
  112. * @var string
  113. */
  114. protected $_date = null;
  115. /**
  116. * Message-ID: header
  117. * @var string
  118. */
  119. protected $_messageId = null;
  120. /**
  121. * text/plain MIME part
  122. * @var false|Zend_Mime_Part
  123. */
  124. protected $_bodyText = false;
  125. /**
  126. * text/html MIME part
  127. * @var false|Zend_Mime_Part
  128. */
  129. protected $_bodyHtml = false;
  130. /**
  131. * MIME boundary string
  132. * @var string
  133. */
  134. protected $_mimeBoundary = null;
  135. /**
  136. * Content type of the message
  137. * @var string
  138. */
  139. protected $_type = null;
  140. /**#@-*/
  141. /**
  142. * Flag: whether or not email has attachments
  143. * @var boolean
  144. */
  145. public $hasAttachments = false;
  146. /**
  147. * Sets the default mail transport for all following uses of
  148. * Zend_Mail::send();
  149. *
  150. * @todo Allow passing a string to indicate the transport to load
  151. * @todo Allow passing in optional options for the transport to load
  152. * @param Zend_Mail_Transport_Abstract $transport
  153. */
  154. public static function setDefaultTransport(Zend_Mail_Transport_Abstract $transport)
  155. {
  156. self::$_defaultTransport = $transport;
  157. }
  158. /**
  159. * Gets the default mail transport for all following uses of
  160. * unittests
  161. *
  162. * @todo Allow passing a string to indicate the transport to load
  163. * @todo Allow passing in optional options for the transport to load
  164. */
  165. public static function getDefaultTransport()
  166. {
  167. return self::$_defaultTransport;
  168. }
  169. /**
  170. * Clear the default transport property
  171. */
  172. public static function clearDefaultTransport()
  173. {
  174. self::$_defaultTransport = null;
  175. }
  176. /**
  177. * Public constructor
  178. *
  179. * @param string $charset
  180. * @return void
  181. */
  182. public function __construct($charset = null)
  183. {
  184. if ($charset != null) {
  185. $this->_charset = $charset;
  186. }
  187. }
  188. /**
  189. * Return charset string
  190. *
  191. * @return string
  192. */
  193. public function getCharset()
  194. {
  195. return $this->_charset;
  196. }
  197. /**
  198. * Set content type
  199. *
  200. * Should only be used for manually setting multipart content types.
  201. *
  202. * @param string $type Content type
  203. * @return Zend_Mail Implements fluent interface
  204. * @throws Zend_Mail_Exception for types not supported by Zend_Mime
  205. */
  206. public function setType($type)
  207. {
  208. $allowed = array(
  209. Zend_Mime::MULTIPART_ALTERNATIVE,
  210. Zend_Mime::MULTIPART_MIXED,
  211. Zend_Mime::MULTIPART_RELATED,
  212. );
  213. if (!in_array($type, $allowed)) {
  214. /**
  215. * @see Zend_Mail_Exception
  216. */
  217. // require_once 'Zend/Mail/Exception.php';
  218. throw new Exception('Invalid content type "' . $type . '"');
  219. }
  220. $this->_type = $type;
  221. return $this;
  222. }
  223. /**
  224. * Get content type of the message
  225. *
  226. * @return string
  227. */
  228. public function getType()
  229. {
  230. return $this->_type;
  231. }
  232. /**
  233. * Set an arbitrary mime boundary for the message
  234. *
  235. * If not set, Zend_Mime will generate one.
  236. *
  237. * @param string $boundary
  238. * @return Zend_Mail Provides fluent interface
  239. */
  240. public function setMimeBoundary($boundary)
  241. {
  242. $this->_mimeBoundary = $boundary;
  243. return $this;
  244. }
  245. /**
  246. * Return the boundary string used for the message
  247. *
  248. * @return string
  249. */
  250. public function getMimeBoundary()
  251. {
  252. return $this->_mimeBoundary;
  253. }
  254. /**
  255. * Return encoding of mail headers
  256. *
  257. * @deprecated use {@link getHeaderEncoding()} instead
  258. * @return string
  259. */
  260. public function getEncodingOfHeaders()
  261. {
  262. return $this->getHeaderEncoding();
  263. }
  264. /**
  265. * Return the encoding of mail headers
  266. *
  267. * Either Zend_Mime::ENCODING_QUOTEDPRINTABLE or Zend_Mime::ENCODING_BASE64
  268. *
  269. * @return string
  270. */
  271. public function getHeaderEncoding()
  272. {
  273. return $this->_headerEncoding;
  274. }
  275. /**
  276. * Set the encoding of mail headers
  277. *
  278. * @deprecated Use {@link setHeaderEncoding()} instead.
  279. * @param string $encoding
  280. * @return Zend_Mail
  281. */
  282. public function setEncodingOfHeaders($encoding)
  283. {
  284. return $this->setHeaderEncoding($encoding);
  285. }
  286. /**
  287. * Set the encoding of mail headers
  288. *
  289. * @param string $encoding Zend_Mime::ENCODING_QUOTEDPRINTABLE or Zend_Mime::ENCODING_BASE64
  290. * @return Zend_Mail Provides fluent interface
  291. */
  292. public function setHeaderEncoding($encoding)
  293. {
  294. $allowed = array(
  295. Zend_Mime::ENCODING_BASE64,
  296. Zend_Mime::ENCODING_QUOTEDPRINTABLE
  297. );
  298. if (!in_array($encoding, $allowed)) {
  299. /**
  300. * @see Zend_Mail_Exception
  301. */
  302. // require_once 'Zend/Mail/Exception.php';
  303. throw new Exception('Invalid encoding "' . $encoding . '"');
  304. }
  305. $this->_headerEncoding = $encoding;
  306. return $this;
  307. }
  308. /**
  309. * Sets the text body for the message.
  310. *
  311. * @param string $txt
  312. * @param string $charset
  313. * @param string $encoding
  314. * @return Zend_Mail Provides fluent interface
  315. */
  316. public function setBodyText($txt, $charset = null, $encoding = Zend_Mime::ENCODING_QUOTEDPRINTABLE)
  317. {
  318. if ($charset === null) {
  319. $charset = $this->_charset;
  320. }
  321. $mp = new Zend_Mime_Part($txt);
  322. $mp->encoding = $encoding;
  323. $mp->type = Zend_Mime::TYPE_TEXT;
  324. $mp->disposition = Zend_Mime::DISPOSITION_INLINE;
  325. $mp->charset = $charset;
  326. $this->_bodyText = $mp;
  327. return $this;
  328. }
  329. /**
  330. * Return text body Zend_Mime_Part or string
  331. *
  332. * @param bool textOnly Whether to return just the body text content or the MIME part; defaults to false, the MIME part
  333. * @return false|Zend_Mime_Part|string
  334. */
  335. public function getBodyText($textOnly = false)
  336. {
  337. if ($textOnly && $this->_bodyText) {
  338. $body = $this->_bodyText;
  339. return $body->getContent();
  340. }
  341. return $this->_bodyText;
  342. }
  343. /**
  344. * Sets the HTML body for the message
  345. *
  346. * @param string $html
  347. * @param string $charset
  348. * @param string $encoding
  349. * @return Zend_Mail Provides fluent interface
  350. */
  351. public function setBodyHtml($html, $charset = null, $encoding = Zend_Mime::ENCODING_QUOTEDPRINTABLE)
  352. {
  353. if ($charset === null) {
  354. $charset = $this->_charset;
  355. }
  356. $mp = new Zend_Mime_Part($html);
  357. $mp->encoding = $encoding;
  358. $mp->type = Zend_Mime::TYPE_HTML;
  359. $mp->disposition = Zend_Mime::DISPOSITION_INLINE;
  360. $mp->charset = $charset;
  361. $this->_bodyHtml = $mp;
  362. return $this;
  363. }
  364. /**
  365. * Return Zend_Mime_Part representing body HTML
  366. *
  367. * @param bool $htmlOnly Whether to return the body HTML only, or the MIME part; defaults to false, the MIME part
  368. * @return false|Zend_Mime_Part|string
  369. */
  370. public function getBodyHtml($htmlOnly = false)
  371. {
  372. if ($htmlOnly && $this->_bodyHtml) {
  373. $body = $this->_bodyHtml;
  374. return $body->getContent();
  375. }
  376. return $this->_bodyHtml;
  377. }
  378. /**
  379. * Adds an existing attachment to the mail message
  380. *
  381. * @param Zend_Mime_Part $attachment
  382. * @return Zend_Mail Provides fluent interface
  383. */
  384. public function addAttachment(Zend_Mime_Part $attachment)
  385. {
  386. $this->addPart($attachment);
  387. $this->hasAttachments = true;
  388. return $this;
  389. }
  390. /**
  391. * Creates a Zend_Mime_Part attachment
  392. *
  393. * Attachment is automatically added to the mail object after creation. The
  394. * attachment object is returned to allow for further manipulation.
  395. *
  396. * @param string $body
  397. * @param string $mimeType
  398. * @param string $disposition
  399. * @param string $encoding
  400. * @param string $filename OPTIONAL A filename for the attachment
  401. * @return Zend_Mime_Part Newly created Zend_Mime_Part object (to allow
  402. * advanced settings)
  403. */
  404. public function createAttachment($body,
  405. $mimeType = Zend_Mime::TYPE_OCTETSTREAM,
  406. $disposition = Zend_Mime::DISPOSITION_ATTACHMENT,
  407. $encoding = Zend_Mime::ENCODING_BASE64,
  408. $filename = null)
  409. {
  410. $mp = new Zend_Mime_Part($body);
  411. $mp->encoding = $encoding;
  412. $mp->type = $mimeType;
  413. $mp->disposition = $disposition;
  414. $mp->filename = $filename;
  415. $this->addAttachment($mp);
  416. return $mp;
  417. }
  418. /**
  419. * Return a count of message parts
  420. *
  421. * @return integer
  422. */
  423. public function getPartCount()
  424. {
  425. return count($this->_parts);
  426. }
  427. /**
  428. * Encode header fields
  429. *
  430. * Encodes header content according to RFC1522 if it contains non-printable
  431. * characters.
  432. *
  433. * @param string $value
  434. * @return string
  435. */
  436. protected function _encodeHeader($value)
  437. {
  438. if (Zend_Mime::isPrintable($value) === false) {
  439. if ($this->getHeaderEncoding() === Zend_Mime::ENCODING_QUOTEDPRINTABLE) {
  440. $value = Zend_Mime::encodeQuotedPrintableHeader($value, $this->getCharset(), Zend_Mime::LINELENGTH, Zend_Mime::LINEEND);
  441. } else {
  442. $value = Zend_Mime::encodeBase64Header($value, $this->getCharset(), Zend_Mime::LINELENGTH, Zend_Mime::LINEEND);
  443. }
  444. }
  445. return $value;
  446. }
  447. /**
  448. * Add a header to the message
  449. *
  450. * Adds a header to this message. If append is true and the header already
  451. * exists, raises a flag indicating that the header should be appended.
  452. *
  453. * @param string $headerName
  454. * @param string $value
  455. * @param bool $append
  456. */
  457. protected function _storeHeader($headerName, $value, $append = false)
  458. {
  459. if (isset($this->_headers[$headerName])) {
  460. $this->_headers[$headerName][] = $value;
  461. } else {
  462. $this->_headers[$headerName] = array($value);
  463. }
  464. if ($append) {
  465. $this->_headers[$headerName]['append'] = true;
  466. }
  467. }
  468. /**
  469. * Clear header from the message
  470. *
  471. * @param string $headerName
  472. * @deprecated use public method directly
  473. */
  474. protected function _clearHeader($headerName)
  475. {
  476. $this->clearHeader($headerName);
  477. }
  478. /**
  479. * Helper function for adding a recipient and the corresponding header
  480. *
  481. * @param string $headerName
  482. * @param string $email
  483. * @param string $name
  484. */
  485. protected function _addRecipientAndHeader($headerName, $email, $name)
  486. {
  487. $email = $this->_filterEmail($email);
  488. $name = $this->_filterName($name);
  489. // prevent duplicates
  490. $this->_recipients[$email] = 1;
  491. $this->_storeHeader($headerName, $this->_formatAddress($email, $name), true);
  492. }
  493. /**
  494. * Adds To-header and recipient, $email can be an array, or a single string address
  495. *
  496. * @param string|array $email
  497. * @param string $name
  498. * @return Zend_Mail Provides fluent interface
  499. */
  500. public function addTo($email, $name='')
  501. {
  502. if (!is_array($email)) {
  503. $email = array($name => $email);
  504. }
  505. foreach ($email as $n => $recipient) {
  506. $this->_addRecipientAndHeader('To', $recipient, is_int($n) ? '' : $n);
  507. $this->_to[] = $recipient;
  508. }
  509. return $this;
  510. }
  511. /**
  512. * Adds Cc-header and recipient, $email can be an array, or a single string address
  513. *
  514. * @param string|array $email
  515. * @param string $name
  516. * @return Zend_Mail Provides fluent interface
  517. */
  518. public function addCc($email, $name='')
  519. {
  520. if (!is_array($email)) {
  521. $email = array($name => $email);
  522. }
  523. foreach ($email as $n => $recipient) {
  524. $this->_addRecipientAndHeader('Cc', $recipient, is_int($n) ? '' : $n);
  525. }
  526. return $this;
  527. }
  528. /**
  529. * Adds Bcc recipient, $email can be an array, or a single string address
  530. *
  531. * @param string|array $email
  532. * @return Zend_Mail Provides fluent interface
  533. */
  534. public function addBcc($email)
  535. {
  536. if (!is_array($email)) {
  537. $email = array($email);
  538. }
  539. foreach ($email as $recipient) {
  540. $this->_addRecipientAndHeader('Bcc', $recipient, '');
  541. }
  542. return $this;
  543. }
  544. /**
  545. * Return list of recipient email addresses
  546. *
  547. * @return array (of strings)
  548. */
  549. public function getRecipients()
  550. {
  551. return array_keys($this->_recipients);
  552. }
  553. /**
  554. * Clear header from the message
  555. *
  556. * @param string $headerName
  557. * @return Zend_Mail Provides fluent inter
  558. */
  559. public function clearHeader($headerName)
  560. {
  561. if (isset($this->_headers[$headerName])){
  562. unset($this->_headers[$headerName]);
  563. }
  564. return $this;
  565. }
  566. /**
  567. * Clears list of recipient email addresses
  568. *
  569. * @return Zend_Mail Provides fluent interface
  570. */
  571. public function clearRecipients()
  572. {
  573. $this->_recipients = array();
  574. $this->_to = array();
  575. $this->clearHeader('To');
  576. $this->clearHeader('Cc');
  577. $this->clearHeader('Bcc');
  578. return $this;
  579. }
  580. /**
  581. * Sets From-header and sender of the message
  582. *
  583. * @param string $email
  584. * @param string $name
  585. * @return Zend_Mail Provides fluent interface
  586. * @throws Zend_Mail_Exception if called subsequent times
  587. */
  588. public function setFrom($email, $name = null)
  589. {
  590. if (null !== $this->_from) {
  591. /**
  592. * @see Zend_Mail_Exception
  593. */
  594. // require_once 'Zend/Mail/Exception.php';
  595. throw new Exception('From Header set twice');
  596. }
  597. $email = $this->_filterEmail($email);
  598. $name = $this->_filterName($name);
  599. $this->_from = $email;
  600. $this->_storeHeader('From', $this->_formatAddress($email, $name), true);
  601. return $this;
  602. }
  603. /**
  604. * Set Reply-To Header
  605. *
  606. * @param string $email
  607. * @param string $name
  608. * @return Zend_Mail
  609. * @throws Zend_Mail_Exception if called more than one time
  610. */
  611. public function setReplyTo($email, $name = null)
  612. {
  613. if (null !== $this->_replyTo) {
  614. /**
  615. * @see Zend_Mail_Exception
  616. */
  617. // require_once 'Zend/Mail/Exception.php';
  618. throw new Exception('Reply-To Header set twice');
  619. }
  620. $email = $this->_filterEmail($email);
  621. $name = $this->_filterName($name);
  622. $this->_replyTo = $email;
  623. $this->_storeHeader('Reply-To', $this->_formatAddress($email, $name), true);
  624. return $this;
  625. }
  626. /**
  627. * Returns the sender of the mail
  628. *
  629. * @return string
  630. */
  631. public function getFrom()
  632. {
  633. return $this->_from;
  634. }
  635. /**
  636. * Returns the current Reply-To address of the message
  637. *
  638. * @return string|null Reply-To address, null when not set
  639. */
  640. public function getReplyTo()
  641. {
  642. return $this->_replyTo;
  643. }
  644. /**
  645. * Clears the sender from the mail
  646. *
  647. * @return Zend_Mail Provides fluent interface
  648. */
  649. public function clearFrom()
  650. {
  651. $this->_from = null;
  652. $this->clearHeader('From');
  653. return $this;
  654. }
  655. /**
  656. * Clears the current Reply-To address from the message
  657. *
  658. * @return Zend_Mail Provides fluent interface
  659. */
  660. public function clearReplyTo()
  661. {
  662. $this->_replyTo = null;
  663. $this->clearHeader('Reply-To');
  664. return $this;
  665. }
  666. /**
  667. * Sets Default From-email and name of the message
  668. *
  669. * @param string $email
  670. * @param string Optional $name
  671. * @return void
  672. */
  673. public static function setDefaultFrom($email, $name = null)
  674. {
  675. self::$_defaultFrom = array('email' => $email, 'name' => $name);
  676. }
  677. /**
  678. * Returns the default sender of the mail
  679. *
  680. * @return null|array Null if none was set.
  681. */
  682. public static function getDefaultFrom()
  683. {
  684. return self::$_defaultFrom;
  685. }
  686. /**
  687. * Clears the default sender from the mail
  688. *
  689. * @return void
  690. */
  691. public static function clearDefaultFrom()
  692. {
  693. self::$_defaultFrom = null;
  694. }
  695. /**
  696. * Sets From-name and -email based on the defaults
  697. *
  698. * @return Zend_Mail Provides fluent interface
  699. */
  700. public function setFromToDefaultFrom() {
  701. $from = self::getDefaultFrom();
  702. if($from === null) {
  703. // require_once 'Zend/Mail/Exception.php';
  704. throw new Exception(
  705. 'No default From Address set to use');
  706. }
  707. $this->setFrom($from['email'], $from['name']);
  708. return $this;
  709. }
  710. /**
  711. * Sets Default ReplyTo-address and -name of the message
  712. *
  713. * @param string $email
  714. * @param string Optional $name
  715. * @return void
  716. */
  717. public static function setDefaultReplyTo($email, $name = null)
  718. {
  719. self::$_defaultReplyTo = array('email' => $email, 'name' => $name);
  720. }
  721. /**
  722. * Returns the default Reply-To Address and Name of the mail
  723. *
  724. * @return null|array Null if none was set.
  725. */
  726. public static function getDefaultReplyTo()
  727. {
  728. return self::$_defaultReplyTo;
  729. }
  730. /**
  731. * Clears the default ReplyTo-address and -name from the mail
  732. *
  733. * @return void
  734. */
  735. public static function clearDefaultReplyTo()
  736. {
  737. self::$_defaultReplyTo = null;
  738. }
  739. /**
  740. * Sets ReplyTo-name and -email based on the defaults
  741. *
  742. * @return Zend_Mail Provides fluent interface
  743. */
  744. public function setReplyToFromDefault() {
  745. $replyTo = self::getDefaultReplyTo();
  746. if($replyTo === null) {
  747. // require_once 'Zend/Mail/Exception.php';
  748. throw new Exception(
  749. 'No default Reply-To Address set to use');
  750. }
  751. $this->setReplyTo($replyTo['email'], $replyTo['name']);
  752. return $this;
  753. }
  754. /**
  755. * Sets the Return-Path header of the message
  756. *
  757. * @param string $email
  758. * @return Zend_Mail Provides fluent interface
  759. * @throws Zend_Mail_Exception if set multiple times
  760. */
  761. public function setReturnPath($email)
  762. {
  763. if ($this->_returnPath === null) {
  764. $email = $this->_filterEmail($email);
  765. $this->_returnPath = $email;
  766. $this->_storeHeader('Return-Path', $email, false);
  767. } else {
  768. /**
  769. * @see Zend_Mail_Exception
  770. */
  771. // require_once 'Zend/Mail/Exception.php';
  772. throw new Exception('Return-Path Header set twice');
  773. }
  774. return $this;
  775. }
  776. /**
  777. * Returns the current Return-Path address of the message
  778. *
  779. * If no Return-Path header is set, returns the value of {@link $_from}.
  780. *
  781. * @return string
  782. */
  783. public function getReturnPath()
  784. {
  785. if (null !== $this->_returnPath) {
  786. return $this->_returnPath;
  787. }
  788. return $this->_from;
  789. }
  790. /**
  791. * Clears the current Return-Path address from the message
  792. *
  793. * @return Zend_Mail Provides fluent interface
  794. */
  795. public function clearReturnPath()
  796. {
  797. $this->_returnPath = null;
  798. $this->clearHeader('Return-Path');
  799. return $this;
  800. }
  801. /**
  802. * Sets the subject of the message
  803. *
  804. * @param string $subject
  805. * @return Zend_Mail Provides fluent interface
  806. * @throws Zend_Mail_Exception
  807. */
  808. public function setSubject($subject)
  809. {
  810. if ($this->_subject === null) {
  811. $subject = $this->_filterOther($subject);
  812. $this->_subject = $this->_encodeHeader($subject);
  813. $this->_storeHeader('Subject', $this->_subject);
  814. } else {
  815. /**
  816. * @see Zend_Mail_Exception
  817. */
  818. // require_once 'Zend/Mail/Exception.php';
  819. throw new Exception('Subject set twice');
  820. }
  821. return $this;
  822. }
  823. /**
  824. * Returns the encoded subject of the message
  825. *
  826. * @return string
  827. */
  828. public function getSubject()
  829. {
  830. return $this->_subject;
  831. }
  832. /**
  833. * Clears the encoded subject from the message
  834. *
  835. * @return Zend_Mail Provides fluent interface
  836. */
  837. public function clearSubject()
  838. {
  839. $this->_subject = null;
  840. $this->clearHeader('Subject');
  841. return $this;
  842. }
  843. /**
  844. * Sets Date-header
  845. *
  846. * @param timestamp|string|Zend_Date $date
  847. * @return Zend_Mail Provides fluent interface
  848. * @throws Zend_Mail_Exception if called subsequent times or wrong date format.
  849. */
  850. public function setDate($date = null)
  851. {
  852. if ($this->_date === null) {
  853. if ($date === null) {
  854. $date = date('r');
  855. } else if (is_int($date)) {
  856. $date = date('r', $date);
  857. } else if (is_string($date)) {
  858. $date = strtotime($date);
  859. if ($date === false || $date < 0) {
  860. /**
  861. * @see Zend_Mail_Exception
  862. */
  863. // require_once 'Zend/Mail/Exception.php';
  864. throw new Exception('String representations of Date Header must be ' .
  865. 'strtotime()-compatible');
  866. }
  867. $date = date('r', $date);
  868. } else if ($date instanceof Zend_Date) {
  869. $date = $date->get(Zend_Date::RFC_2822);
  870. } else {
  871. /**
  872. * @see Zend_Mail_Exception
  873. */
  874. // require_once 'Zend/Mail/Exception.php';
  875. throw new Exception(__METHOD__ . ' only accepts UNIX timestamps, Zend_Date objects, ' .
  876. ' and strtotime()-compatible strings');
  877. }
  878. $this->_date = $date;
  879. $this->_storeHeader('Date', $date);
  880. } else {
  881. /**
  882. * @see Zend_Mail_Exception
  883. */
  884. // require_once 'Zend/Mail/Exception.php';
  885. throw new Exception('Date Header set twice');
  886. }
  887. return $this;
  888. }
  889. /**
  890. * Returns the formatted date of the message
  891. *
  892. * @return string
  893. */
  894. public function getDate()
  895. {
  896. return $this->_date;
  897. }
  898. /**
  899. * Clears the formatted date from the message
  900. *
  901. * @return Zend_Mail Provides fluent interface
  902. */
  903. public function clearDate()
  904. {
  905. $this->_date = null;
  906. $this->clearHeader('Date');
  907. return $this;
  908. }
  909. /**
  910. * Sets the Message-ID of the message
  911. *
  912. * @param boolean|string $id
  913. * true :Auto
  914. * false :No set
  915. * null :No set
  916. * string:Sets given string (Angle brackets is not necessary)
  917. * @return Zend_Mail Provides fluent interface
  918. * @throws Zend_Mail_Exception
  919. */
  920. public function setMessageId($id = true)
  921. {
  922. if ($id === null || $id === false) {
  923. return $this;
  924. } elseif ($id === true) {
  925. $id = $this->createMessageId();
  926. }
  927. if ($this->_messageId === null) {
  928. $id = $this->_filterOther($id);
  929. $this->_messageId = $id;
  930. $this->_storeHeader('Message-Id', '<' . $this->_messageId . '>');
  931. } else {
  932. /**
  933. * @see Zend_Mail_Exception
  934. */
  935. // require_once 'Zend/Mail/Exception.php';
  936. throw new Exception('Message-ID set twice');
  937. }
  938. return $this;
  939. }
  940. /**
  941. * Returns the Message-ID of the message
  942. *
  943. * @return string
  944. */
  945. public function getMessageId()
  946. {
  947. return $this->_messageId;
  948. }
  949. /**
  950. * Clears the Message-ID from the message
  951. *
  952. * @return Zend_Mail Provides fluent interface
  953. */
  954. public function clearMessageId()
  955. {
  956. $this->_messageId = null;
  957. $this->clearHeader('Message-Id');
  958. return $this;
  959. }
  960. /**
  961. * Creates the Message-ID
  962. *
  963. * @return string
  964. */
  965. public function createMessageId() {
  966. $time = time();
  967. if ($this->_from !== null) {
  968. $user = $this->_from;
  969. } elseif (isset($_SERVER['REMOTE_ADDR'])) {
  970. $user = $_SERVER['REMOTE_ADDR'];
  971. } else {
  972. $user = getmypid();
  973. }
  974. $rand = mt_rand();
  975. if ($this->_recipients !== array()) {
  976. $recipient = array_rand($this->_recipients);
  977. } else {
  978. $recipient = 'unknown';
  979. }
  980. if (isset($_SERVER["SERVER_NAME"])) {
  981. $hostName = $_SERVER["SERVER_NAME"];
  982. } else {
  983. $hostName = php_uname('n');
  984. }
  985. return sha1($time . $user . $rand . $recipient) . '@' . $hostName;
  986. }
  987. /**
  988. * Add a custom header to the message
  989. *
  990. * @param string $name
  991. * @param string $value
  992. * @param boolean $append
  993. * @return Zend_Mail Provides fluent interface
  994. * @throws Zend_Mail_Exception on attempts to create standard headers
  995. */
  996. public function addHeader($name, $value, $append = false)
  997. {
  998. $prohibit = array('to', 'cc', 'bcc', 'from', 'subject',
  999. 'reply-to', 'return-path',
  1000. 'date', 'message-id',
  1001. );
  1002. if (in_array(strtolower($name), $prohibit)) {
  1003. /**
  1004. * @see Zend_Mail_Exception
  1005. */
  1006. // require_once 'Zend/Mail/Exception.php';
  1007. throw new Exception('Cannot set standard header from addHeader()');
  1008. }
  1009. $value = $this->_filterOther($value);
  1010. $value = $this->_encodeHeader($value);
  1011. $this->_storeHeader($name, $value, $append);
  1012. return $this;
  1013. }
  1014. /**
  1015. * Return mail headers
  1016. *
  1017. * @return void
  1018. */
  1019. public function getHeaders()
  1020. {
  1021. return $this->_headers;
  1022. }
  1023. /**
  1024. * Sends this email using the given transport or a previously
  1025. * set DefaultTransport or the internal mail function if no
  1026. * default transport had been set.
  1027. *
  1028. * @param Zend_Mail_Transport_Abstract $transport
  1029. * @return Zend_Mail Provides fluent interface
  1030. */
  1031. public function send($transport = null)
  1032. {
  1033. if ($transport === null) {
  1034. if (! self::$_defaultTransport instanceof Zend_Mail_Transport_Abstract) {
  1035. // require_once 'Zend/Mail/Transport/Sendmail.php';
  1036. $transport = new Zend_Mail_Transport_Sendmail();
  1037. } else {
  1038. $transport = self::$_defaultTransport;
  1039. }
  1040. }
  1041. if ($this->_date === null) {
  1042. $this->setDate();
  1043. }
  1044. if(null === $this->_from && null !== self::getDefaultFrom()) {
  1045. $this->setFromToDefaultFrom();
  1046. }
  1047. if(null === $this->_replyTo && null !== self::getDefaultReplyTo()) {
  1048. $this->setReplyToFromDefault();
  1049. }
  1050. $transport->send($this);
  1051. return $this;
  1052. }
  1053. /**
  1054. * Filter of email data
  1055. *
  1056. * @param string $email
  1057. * @return string
  1058. */
  1059. protected function _filterEmail($email)
  1060. {
  1061. $rule = array("\r" => '',
  1062. "\n" => '',
  1063. "\t" => '',
  1064. '"' => '',
  1065. ',' => '',
  1066. '<' => '',
  1067. '>' => '',
  1068. );
  1069. return strtr($email, $rule);
  1070. }
  1071. /**
  1072. * Filter of name data
  1073. *
  1074. * @param string $name
  1075. * @return string
  1076. */
  1077. protected function _filterName($name)
  1078. {
  1079. $rule = array("\r" => '',
  1080. "\n" => '',
  1081. "\t" => '',
  1082. '"' => "'",
  1083. '<' => '[',
  1084. '>' => ']',
  1085. );
  1086. return trim(strtr($name, $rule));
  1087. }
  1088. /**
  1089. * Filter of other data
  1090. *
  1091. * @param string $data
  1092. * @return string
  1093. */
  1094. protected function _filterOther($data)
  1095. {
  1096. $rule = array("\r" => '',
  1097. "\n" => '',
  1098. "\t" => '',
  1099. );
  1100. return strtr($data, $rule);
  1101. }
  1102. /**
  1103. * Formats e-mail address
  1104. *
  1105. * @param string $email
  1106. * @param string $name
  1107. * @return string
  1108. */
  1109. protected function _formatAddress($email, $name)
  1110. {
  1111. if ($name === '' || $name === null || $name === $email) {
  1112. return $email;
  1113. } else {
  1114. $encodedName = $this->_encodeHeader($name);
  1115. if ($encodedName === $name && strcspn($name, '()<>[]:;@\\,') != strlen($name)) {
  1116. $format = '"%s" <%s>';
  1117. } else {
  1118. $format = '%s <%s>';
  1119. }
  1120. return sprintf($format, $encodedName, $email);
  1121. }
  1122. }
  1123. }