Mail.php 32 KB

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