2
0

Mail.php 26 KB

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