Mail.php 28 KB

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