Mail.php 27 KB

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