Mail.php 26 KB

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