PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Jess_Kell_Example/library/Zend/Mail.php

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