Mail.php 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  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 timestamp|string|Zend_Date $date
  701. * @return Zend_Mail Provides fluent interface
  702. * @throws Zend_Mail_Exception if called subsequent times or wrong date format.
  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 given 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. 'reply-to', 'return-path',
  854. 'date', 'message-id',
  855. );
  856. if (in_array(strtolower($name), $prohibit)) {
  857. /**
  858. * @see Zend_Mail_Exception
  859. */
  860. require_once 'Zend/Mail/Exception.php';
  861. throw new Zend_Mail_Exception('Cannot set standard header from addHeader()');
  862. }
  863. $value = $this->_filterOther($value);
  864. $value = $this->_encodeHeader($value);
  865. $this->_storeHeader($name, $value, $append);
  866. return $this;
  867. }
  868. /**
  869. * Return mail headers
  870. *
  871. * @return void
  872. */
  873. public function getHeaders()
  874. {
  875. return $this->_headers;
  876. }
  877. /**
  878. * Sends this email using the given transport or a previously
  879. * set DefaultTransport or the internal mail function if no
  880. * default transport had been set.
  881. *
  882. * @param Zend_Mail_Transport_Abstract $transport
  883. * @return Zend_Mail Provides fluent interface
  884. */
  885. public function send($transport = null)
  886. {
  887. if ($transport === null) {
  888. if (! self::$_defaultTransport instanceof Zend_Mail_Transport_Abstract) {
  889. require_once 'Zend/Mail/Transport/Sendmail.php';
  890. $transport = new Zend_Mail_Transport_Sendmail();
  891. } else {
  892. $transport = self::$_defaultTransport;
  893. }
  894. }
  895. if ($this->_date === null) {
  896. $this->setDate();
  897. }
  898. $transport->send($this);
  899. return $this;
  900. }
  901. /**
  902. * Filter of email data
  903. *
  904. * @param string $email
  905. * @return string
  906. */
  907. protected function _filterEmail($email)
  908. {
  909. $rule = array("\r" => '',
  910. "\n" => '',
  911. "\t" => '',
  912. '"' => '',
  913. ',' => '',
  914. '<' => '',
  915. '>' => '',
  916. );
  917. return strtr($email, $rule);
  918. }
  919. /**
  920. * Filter of name data
  921. *
  922. * @param string $name
  923. * @return string
  924. */
  925. protected function _filterName($name)
  926. {
  927. $rule = array("\r" => '',
  928. "\n" => '',
  929. "\t" => '',
  930. '"' => "'",
  931. '<' => '[',
  932. '>' => ']',
  933. );
  934. return trim(strtr($name, $rule));
  935. }
  936. /**
  937. * Filter of other data
  938. *
  939. * @param string $data
  940. * @return string
  941. */
  942. protected function _filterOther($data)
  943. {
  944. $rule = array("\r" => '',
  945. "\n" => '',
  946. "\t" => '',
  947. );
  948. return strtr($data, $rule);
  949. }
  950. /**
  951. * Formats e-mail address
  952. *
  953. * @param string $email
  954. * @param string $name
  955. * @return string
  956. */
  957. protected function _formatAddress($email, $name)
  958. {
  959. if ($name === '' || $name === null || $name === $email) {
  960. return $email;
  961. } else {
  962. $encodedName = $this->_encodeHeader($name);
  963. if ($encodedName === $name && strpos($name, ',') !== false) {
  964. $format = '"%s" <%s>';
  965. } else {
  966. $format = '%s <%s>';
  967. }
  968. return sprintf($format, $encodedName, $email);
  969. }
  970. }
  971. }