Mail.php 27 KB

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