2
0

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. * Array of all recipients
  82. * @var array
  83. */
  84. protected $_recipients = array();
  85. /**
  86. * Reply-To header
  87. * @var string
  88. */
  89. protected $_replyTo = null;
  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 (null !== $this->_from) {
  533. /**
  534. * @see Zend_Mail_Exception
  535. */
  536. require_once 'Zend/Mail/Exception.php';
  537. throw new Zend_Mail_Exception('From Header set twice');
  538. }
  539. $email = $this->_filterEmail($email);
  540. $name = $this->_filterName($name);
  541. $this->_from = $email;
  542. $this->_storeHeader('From', $this->_formatAddress($email, $name), true);
  543. return $this;
  544. }
  545. /**
  546. * Set Reply-To Header
  547. *
  548. * @param string $email
  549. * @param string $name
  550. * @return Zend_Mail
  551. * @throws Zend_Mail_Exception if called more than one time
  552. */
  553. public function setReplyTo($email, $name = null)
  554. {
  555. if (null !== $this->_replyTo) {
  556. /**
  557. * @see Zend_Mail_Exception
  558. */
  559. require_once 'Zend/Mail/Exception.php';
  560. throw new Zend_Mail_Exception('Reply-To Header set twice');
  561. }
  562. $email = $this->_filterEmail($email);
  563. $name = $this->_filterName($name);
  564. $this->_replyTo = $email;
  565. $this->_storeHeader('Reply-To', $this->_formatAddress($email, $name), true);
  566. return $this;
  567. }
  568. /**
  569. * Returns the sender of the mail
  570. *
  571. * @return string
  572. */
  573. public function getFrom()
  574. {
  575. return $this->_from;
  576. }
  577. /**
  578. * Returns the current Reply-To address of the message
  579. *
  580. * @return string|null Reply-To address, null when not set
  581. */
  582. public function getReplyTo()
  583. {
  584. return $this->_replyTo;
  585. }
  586. /**
  587. * Clears the sender from the mail
  588. *
  589. * @return Zend_Mail Provides fluent interface
  590. */
  591. public function clearFrom()
  592. {
  593. $this->_from = null;
  594. $this->_clearHeader('From');
  595. return $this;
  596. }
  597. /**
  598. * Clears the current Reply-To address from the message
  599. *
  600. * @return Zend_Mail Provides fluent interface
  601. */
  602. public function clearReplyTo()
  603. {
  604. $this->_replyTo = null;
  605. $this->_clearHeader('Reply-To');
  606. return $this;
  607. }
  608. /**
  609. * Sets the Return-Path header of the message
  610. *
  611. * @param string $email
  612. * @return Zend_Mail Provides fluent interface
  613. * @throws Zend_Mail_Exception if set multiple times
  614. */
  615. public function setReturnPath($email)
  616. {
  617. if ($this->_returnPath === null) {
  618. $email = $this->_filterEmail($email);
  619. $this->_returnPath = $email;
  620. $this->_storeHeader('Return-Path', $email, false);
  621. } else {
  622. /**
  623. * @see Zend_Mail_Exception
  624. */
  625. require_once 'Zend/Mail/Exception.php';
  626. throw new Zend_Mail_Exception('Return-Path Header set twice');
  627. }
  628. return $this;
  629. }
  630. /**
  631. * Returns the current Return-Path address of the message
  632. *
  633. * If no Return-Path header is set, returns the value of {@link $_from}.
  634. *
  635. * @return string
  636. */
  637. public function getReturnPath()
  638. {
  639. if (null !== $this->_returnPath) {
  640. return $this->_returnPath;
  641. }
  642. return $this->_from;
  643. }
  644. /**
  645. * Clears the current Return-Path address from the message
  646. *
  647. * @return Zend_Mail Provides fluent interface
  648. */
  649. public function clearReturnPath()
  650. {
  651. $this->_returnPath = null;
  652. $this->_clearHeader('Return-Path');
  653. return $this;
  654. }
  655. /**
  656. * Sets the subject of the message
  657. *
  658. * @param string $subject
  659. * @return Zend_Mail Provides fluent interface
  660. * @throws Zend_Mail_Exception
  661. */
  662. public function setSubject($subject)
  663. {
  664. if ($this->_subject === null) {
  665. $subject = $this->_filterOther($subject);
  666. $this->_subject = $this->_encodeHeader($subject);
  667. $this->_storeHeader('Subject', $this->_subject);
  668. } else {
  669. /**
  670. * @see Zend_Mail_Exception
  671. */
  672. require_once 'Zend/Mail/Exception.php';
  673. throw new Zend_Mail_Exception('Subject set twice');
  674. }
  675. return $this;
  676. }
  677. /**
  678. * Returns the encoded subject of the message
  679. *
  680. * @return string
  681. */
  682. public function getSubject()
  683. {
  684. return $this->_subject;
  685. }
  686. /**
  687. * Clears the encoded subject from the message
  688. *
  689. * @return Zend_Mail Provides fluent interface
  690. */
  691. public function clearSubject()
  692. {
  693. $this->_subject = null;
  694. $this->_clearHeader('Subject');
  695. return $this;
  696. }
  697. /**
  698. * Sets Date-header
  699. *
  700. * @param string $date
  701. * @return Zend_Mail Provides fluent interface
  702. * @throws Zend_Mail_Exception if called subsequent times
  703. */
  704. public function setDate($date = null)
  705. {
  706. if ($this->_date === null) {
  707. if ($date === null) {
  708. $date = date('r');
  709. } else if (is_int($date)) {
  710. $date = date('r', $date);
  711. } else if (is_string($date)) {
  712. $date = strtotime($date);
  713. if ($date === false || $date < 0) {
  714. /**
  715. * @see Zend_Mail_Exception
  716. */
  717. require_once 'Zend/Mail/Exception.php';
  718. throw new Zend_Mail_Exception('String representations of Date Header must be ' .
  719. 'strtotime()-compatible');
  720. }
  721. $date = date('r', $date);
  722. } else if ($date instanceof Zend_Date) {
  723. $date = $date->get(Zend_Date::RFC_2822);
  724. } else {
  725. /**
  726. * @see Zend_Mail_Exception
  727. */
  728. require_once 'Zend/Mail/Exception.php';
  729. throw new Zend_Mail_Exception(__METHOD__ . ' only accepts UNIX timestamps, Zend_Date objects, ' .
  730. ' and strtotime()-compatible strings');
  731. }
  732. $this->_date = $date;
  733. $this->_storeHeader('Date', $date);
  734. } else {
  735. /**
  736. * @see Zend_Mail_Exception
  737. */
  738. require_once 'Zend/Mail/Exception.php';
  739. throw new Zend_Mail_Exception('Date Header set twice');
  740. }
  741. return $this;
  742. }
  743. /**
  744. * Returns the formatted date of the message
  745. *
  746. * @return string
  747. */
  748. public function getDate()
  749. {
  750. return $this->_date;
  751. }
  752. /**
  753. * Clears the formatted date from the message
  754. *
  755. * @return Zend_Mail Provides fluent interface
  756. */
  757. public function clearDate()
  758. {
  759. $this->_date = null;
  760. $this->_clearHeader('Date');
  761. return $this;
  762. }
  763. /**
  764. * Sets the Message-ID of the message
  765. *
  766. * @param boolean|string $id
  767. * true :Auto
  768. * false :No set
  769. * null :No set
  770. * string:Sets string
  771. * @return Zend_Mail Provides fluent interface
  772. * @throws Zend_Mail_Exception
  773. */
  774. public function setMessageId($id = true)
  775. {
  776. if ($id === null || $id === false) {
  777. return $this;
  778. } elseif ($id === true) {
  779. $id = $this->createMessageId();
  780. }
  781. if ($this->_messageId === null) {
  782. $id = $this->_filterOther($id);
  783. $this->_messageId = $id;
  784. $this->_storeHeader('Message-Id', $this->_messageId);
  785. } else {
  786. /**
  787. * @see Zend_Mail_Exception
  788. */
  789. require_once 'Zend/Mail/Exception.php';
  790. throw new Zend_Mail_Exception('Message-ID set twice');
  791. }
  792. return $this;
  793. }
  794. /**
  795. * Returns the Message-ID of the message
  796. *
  797. * @return string
  798. */
  799. public function getMessageId()
  800. {
  801. return $this->_messageId;
  802. }
  803. /**
  804. * Clears the Message-ID from the message
  805. *
  806. * @return Zend_Mail Provides fluent interface
  807. */
  808. public function clearMessageId()
  809. {
  810. $this->_messageId = null;
  811. $this->_clearHeader('Message-Id');
  812. return $this;
  813. }
  814. /**
  815. * Creates the Message-ID
  816. *
  817. * @return string
  818. */
  819. public function createMessageId() {
  820. $time = time();
  821. if ($this->_from !== null) {
  822. $user = $this->_from;
  823. } elseif (isset($_SERVER['REMOTE_ADDR'])) {
  824. $user = $_SERVER['REMOTE_ADDR'];
  825. } else {
  826. $user = getmypid();
  827. }
  828. $rand = mt_rand();
  829. if ($this->_recipients !== array()) {
  830. $recipient = array_rand($this->_recipients);
  831. } else {
  832. $recipient = 'unknown';
  833. }
  834. if (isset($_SERVER["SERVER_NAME"])) {
  835. $hostName = $_SERVER["SERVER_NAME"];
  836. } else {
  837. $hostName = php_uname('n');
  838. }
  839. return sha1($time . $user . $rand . $recipient) . '@' . $hostName;
  840. }
  841. /**
  842. * Add a custom header to the message
  843. *
  844. * @param string $name
  845. * @param string $value
  846. * @param boolean $append
  847. * @return Zend_Mail Provides fluent interface
  848. * @throws Zend_Mail_Exception on attempts to create standard headers
  849. */
  850. public function addHeader($name, $value, $append = false)
  851. {
  852. $prohibit = array('to', 'cc', 'bcc', 'from', 'subject',
  853. 'return-path', 'date', 'message-id',
  854. );
  855. if (in_array(strtolower($name), $prohibit)) {
  856. /**
  857. * @see Zend_Mail_Exception
  858. */
  859. require_once 'Zend/Mail/Exception.php';
  860. throw new Zend_Mail_Exception('Cannot set standard header from addHeader()');
  861. }
  862. $value = $this->_filterOther($value);
  863. $value = $this->_encodeHeader($value);
  864. $this->_storeHeader($name, $value, $append);
  865. return $this;
  866. }
  867. /**
  868. * Return mail headers
  869. *
  870. * @return void
  871. */
  872. public function getHeaders()
  873. {
  874. return $this->_headers;
  875. }
  876. /**
  877. * Sends this email using the given transport or a previously
  878. * set DefaultTransport or the internal mail function if no
  879. * default transport had been set.
  880. *
  881. * @param Zend_Mail_Transport_Abstract $transport
  882. * @return Zend_Mail Provides fluent interface
  883. */
  884. public function send($transport = null)
  885. {
  886. if ($transport === null) {
  887. if (! self::$_defaultTransport instanceof Zend_Mail_Transport_Abstract) {
  888. require_once 'Zend/Mail/Transport/Sendmail.php';
  889. $transport = new Zend_Mail_Transport_Sendmail();
  890. } else {
  891. $transport = self::$_defaultTransport;
  892. }
  893. }
  894. if ($this->_date === null) {
  895. $this->setDate();
  896. }
  897. $transport->send($this);
  898. return $this;
  899. }
  900. /**
  901. * Filter of email data
  902. *
  903. * @param string $email
  904. * @return string
  905. */
  906. protected function _filterEmail($email)
  907. {
  908. $rule = array("\r" => '',
  909. "\n" => '',
  910. "\t" => '',
  911. '"' => '',
  912. ',' => '',
  913. '<' => '',
  914. '>' => '',
  915. );
  916. return strtr($email, $rule);
  917. }
  918. /**
  919. * Filter of name data
  920. *
  921. * @param string $name
  922. * @return string
  923. */
  924. protected function _filterName($name)
  925. {
  926. $rule = array("\r" => '',
  927. "\n" => '',
  928. "\t" => '',
  929. '"' => "'",
  930. '<' => '[',
  931. '>' => ']',
  932. );
  933. return trim(strtr($name, $rule));
  934. }
  935. /**
  936. * Filter of other data
  937. *
  938. * @param string $data
  939. * @return string
  940. */
  941. protected function _filterOther($data)
  942. {
  943. $rule = array("\r" => '',
  944. "\n" => '',
  945. "\t" => '',
  946. );
  947. return strtr($data, $rule);
  948. }
  949. /**
  950. * Formats e-mail address
  951. *
  952. * @param string $email
  953. * @param string $name
  954. * @return string
  955. */
  956. protected function _formatAddress($email, $name)
  957. {
  958. if ($name === '' || $name === null || $name === $email) {
  959. return $email;
  960. } else {
  961. $encodedName = $this->_encodeHeader($name);
  962. if ($encodedName === $name && strpos($name, ',') !== false) {
  963. $format = '"%s" <%s>';
  964. } else {
  965. $format = '%s <%s>';
  966. }
  967. return sprintf($format, $encodedName, $email);
  968. }
  969. }
  970. }