PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Mail.php

https://github.com/humansky/qframe
PHP | 649 lines | 257 code | 77 blank | 315 comment | 27 complexity | 14a498d08eef4aa24962e57bc47dbcba MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-3.0
  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-2007 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /**
  21. * Zend_Mail_Exception
  22. */
  23. require_once 'Zend/Mail/Exception.php';
  24. /**
  25. * Zend_Mail_Transport_Abstract
  26. */
  27. require_once 'Zend/Mail/Transport/Abstract.php';
  28. /**
  29. * Zend_Mime
  30. */
  31. require_once 'Zend/Mime.php';
  32. /**
  33. * Zend_Mail_Transport_Abstract
  34. */
  35. require_once 'Zend/Mail/Transport/Abstract.php';
  36. /**
  37. * Zend_Mime_Message
  38. */
  39. require_once 'Zend/Mime/Message.php';
  40. /**
  41. * Zend_Mime_Part
  42. */
  43. require_once 'Zend/Mime/Part.php';
  44. /**
  45. * Class for sending an email.
  46. *
  47. * @category Zend
  48. * @package Zend_Mail
  49. * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
  50. * @license http://framework.zend.com/license/new-bsd New BSD License
  51. */
  52. class Zend_Mail extends Zend_Mime_Message
  53. {
  54. /**#@+
  55. * @access protected
  56. */
  57. /**
  58. * @var Zend_Mail_Transport_Abstract
  59. * @static
  60. */
  61. protected static $_defaultTransport = null;
  62. /**
  63. * Mail character set
  64. * @var string
  65. */
  66. protected $_charset = null;
  67. /**
  68. * Mail headers
  69. * @var array
  70. */
  71. protected $_headers = array();
  72. /**
  73. * From: address
  74. * @var string
  75. */
  76. protected $_from = null;
  77. /**
  78. * To: addresses
  79. * @var array
  80. */
  81. protected $_to = array();
  82. /**
  83. * Array of all recipients
  84. * @var array
  85. */
  86. protected $_recipients = array();
  87. /**
  88. * Return-Path header
  89. * @var string
  90. */
  91. protected $_returnPath = null;
  92. /**
  93. * Subject: header
  94. * @var string
  95. */
  96. protected $_subject = null;
  97. /**
  98. * text/plain MIME part
  99. * @var false|Zend_Mime_Part
  100. */
  101. protected $_bodyText = false;
  102. /**
  103. * text/html MIME part
  104. * @var false|Zend_Mime_Part
  105. */
  106. protected $_bodyHtml = false;
  107. /**
  108. * MIME boundary string
  109. * @var string
  110. */
  111. protected $_mimeBoundary = null;
  112. /**
  113. * Content type of the message
  114. * @var string
  115. */
  116. protected $_type = null;
  117. /**#@-*/
  118. /**
  119. * Flag: whether or not email has attachments
  120. * @var boolean
  121. */
  122. public $hasAttachments = false;
  123. /**
  124. * Sets the default mail transport for all following uses of
  125. * Zend_Mail::send();
  126. *
  127. * @todo Allow passing a string to indicate the transport to load
  128. * @todo Allow passing in optional options for the transport to load
  129. * @param Zend_Mail_Transport_Abstract $transport
  130. */
  131. public static function setDefaultTransport(Zend_Mail_Transport_Abstract $transport)
  132. {
  133. self::$_defaultTransport = $transport;
  134. }
  135. /**
  136. * Public constructor
  137. *
  138. * @param string $charset
  139. */
  140. public function __construct($charset='iso-8859-1')
  141. {
  142. $this->_charset = $charset;
  143. }
  144. /**
  145. * Return charset string
  146. *
  147. * @return string
  148. */
  149. public function getCharset()
  150. {
  151. return $this->_charset;
  152. }
  153. /**
  154. * Set content type
  155. *
  156. * Should only be used for manually setting multipart content types.
  157. *
  158. * @param string $type Content type
  159. * @return Zend_Mail Implements fluent interface
  160. * @throws Zend_Mail_Exception for types not supported by Zend_Mime
  161. */
  162. public function setType($type)
  163. {
  164. $allowed = array(
  165. Zend_Mime::MULTIPART_ALTERNATIVE,
  166. Zend_Mime::MULTIPART_MIXED,
  167. Zend_Mime::MULTIPART_RELATED,
  168. );
  169. if (!in_array($type, $allowed)) {
  170. throw new Zend_Mail_Exception('Invalid content type "' . $type . '"');
  171. }
  172. $this->_type = $type;
  173. return $this;
  174. }
  175. /**
  176. * Get content type of the message
  177. *
  178. * @return string
  179. */
  180. public function getType()
  181. {
  182. return $this->_type;
  183. }
  184. /**
  185. * Set an arbitrary mime boundary for the message
  186. *
  187. * If not set, Zend_Mime will generate one.
  188. *
  189. * @param string $boundary
  190. * @return Zend_Mail Provides fluent interface
  191. */
  192. public function setMimeBoundary($boundary)
  193. {
  194. $this->_mimeBoundary = $boundary;
  195. }
  196. /**
  197. * Return the boundary string used for the message
  198. *
  199. * @return string
  200. */
  201. public function getMimeBoundary()
  202. {
  203. return $this->_mimeBoundary;
  204. }
  205. /**
  206. * Sets the text body for the message.
  207. *
  208. * @param string $txt
  209. * @param string $charset
  210. * @param string $encoding
  211. * @return Zend_Mail Provides fluent interface
  212. */
  213. public function setBodyText($txt, $charset = null, $encoding = Zend_Mime::ENCODING_QUOTEDPRINTABLE)
  214. {
  215. if ($charset === null) {
  216. $charset = $this->_charset;
  217. }
  218. $mp = new Zend_Mime_Part($txt);
  219. $mp->encoding = $encoding;
  220. $mp->type = Zend_Mime::TYPE_TEXT;
  221. $mp->disposition = Zend_Mime::DISPOSITION_INLINE;
  222. $mp->charset = $charset;
  223. $this->_bodyText = $mp;
  224. return $this;
  225. }
  226. /**
  227. * Return text body Zend_Mime_Part or string
  228. *
  229. * @param bool textOnly Whether to return just the body text content or the MIME part; defaults to false, the MIME part
  230. * @return false|Zend_Mime_Part|string
  231. */
  232. public function getBodyText($textOnly = false)
  233. {
  234. if ($textOnly && $this->_bodyText) {
  235. $body = $this->_bodyText;
  236. return $body->getContent();
  237. }
  238. return $this->_bodyText;
  239. }
  240. /**
  241. * Sets the HTML body for the message
  242. *
  243. * @param string $html
  244. * @param string $charset
  245. * @param string $encoding
  246. * @return Zend_Mail Provides fluent interface
  247. */
  248. public function setBodyHtml($html, $charset = null, $encoding = Zend_Mime::ENCODING_QUOTEDPRINTABLE)
  249. {
  250. if ($charset === null) {
  251. $charset = $this->_charset;
  252. }
  253. $mp = new Zend_Mime_Part($html);
  254. $mp->encoding = $encoding;
  255. $mp->type = Zend_Mime::TYPE_HTML;
  256. $mp->disposition = Zend_Mime::DISPOSITION_INLINE;
  257. $mp->charset = $charset;
  258. $this->_bodyHtml = $mp;
  259. return $this;
  260. }
  261. /**
  262. * Return Zend_Mime_Part representing body HTML
  263. *
  264. * @param bool $htmlOnly Whether to return the body HTML only, or the MIME part; defaults to false, the MIME part
  265. * @return false|Zend_Mime_Part|string
  266. */
  267. public function getBodyHtml($htmlOnly = false)
  268. {
  269. if ($htmlOnly && $this->_bodyHtml) {
  270. $body = $this->_bodyHtml;
  271. return $body->getContent();
  272. }
  273. return $this->_bodyHtml;
  274. }
  275. /**
  276. * Adds an existing attachment to the mail message
  277. *
  278. * @param Zend_Mime_Part $attachment
  279. * @return Zend_Mail Provides fluent interface
  280. */
  281. public function addAttachment(Zend_Mime_Part $attachment)
  282. {
  283. $this->addPart($attachment);
  284. $this->hasAttachments = true;
  285. return $this;
  286. }
  287. /**
  288. * Creates a Zend_Mime_Part attachment
  289. *
  290. * Attachment is automatically added to the mail object after creation. The
  291. * attachment object is returned to allow for further manipulation.
  292. *
  293. * @param string $body
  294. * @param string $mimeType
  295. * @param string $disposition
  296. * @param string $encoding
  297. * @param string $filename OPTIONAL A filename for the attachment
  298. * @return Zend_Mime_Part Newly created Zend_Mime_Part object (to allow
  299. * advanced settings)
  300. */
  301. public function createAttachment($body,
  302. $mimeType = Zend_Mime::TYPE_OCTETSTREAM,
  303. $disposition = Zend_Mime::DISPOSITION_ATTACHMENT,
  304. $encoding = Zend_Mime::ENCODING_BASE64,
  305. $filename = null)
  306. {
  307. $mp = new Zend_Mime_Part($body);
  308. $mp->encoding = $encoding;
  309. $mp->type = $mimeType;
  310. $mp->disposition = $disposition;
  311. $mp->filename = $filename;
  312. $this->addAttachment($mp);
  313. return $mp;
  314. }
  315. /**
  316. * Return a count of message parts
  317. *
  318. * @return void
  319. */
  320. public function getPartCount()
  321. {
  322. return count($this->_parts);
  323. }
  324. /**
  325. * Encode header fields
  326. *
  327. * Encodes header content according to RFC1522 if it contains non-printable
  328. * characters.
  329. *
  330. * @param string $value
  331. * @return string
  332. */
  333. protected function _encodeHeader($value)
  334. {
  335. if (Zend_Mime::isPrintable($value)) {
  336. return $value;
  337. } else {
  338. $quotedValue = Zend_Mime::encodeQuotedPrintable($value);
  339. $quotedValue = str_replace(array('?', ' '), array('=3F', '=20'), $quotedValue);
  340. return '=?' . $this->_charset . '?Q?' . $quotedValue . '?=';
  341. }
  342. }
  343. /**
  344. * Add a header to the message
  345. *
  346. * Adds a header to this message. If append is true and the header already
  347. * exists, raises a flag indicating that the header should be appended.
  348. *
  349. * @param string $headerName
  350. * @param string $value
  351. * @param boolean $append
  352. */
  353. protected function _storeHeader($headerName, $value, $append=false)
  354. {
  355. $value = strtr($value,"\r\n\t",'???');
  356. if (isset($this->_headers[$headerName])) {
  357. $this->_headers[$headerName][] = $value;
  358. } else {
  359. $this->_headers[$headerName] = array($value);
  360. }
  361. if ($append) {
  362. $this->_headers[$headerName]['append'] = true;
  363. }
  364. }
  365. /**
  366. * Add a recipient
  367. *
  368. * @param string $email
  369. */
  370. protected function _addRecipient($email, $to = false)
  371. {
  372. // prevent duplicates
  373. $this->_recipients[$email] = 1;
  374. if ($to) {
  375. $this->_to[] = $email;
  376. }
  377. }
  378. /**
  379. * Helper function for adding a recipient and the corresponding header
  380. *
  381. * @param string $headerName
  382. * @param string $name
  383. * @param string $email
  384. */
  385. protected function _addRecipientAndHeader($headerName, $name, $email)
  386. {
  387. $email = strtr($email,"\r\n\t",'???');
  388. $this->_addRecipient($email, ('To' == $headerName) ? true : false);
  389. if ($name != '') {
  390. $name = '"' . $this->_encodeHeader($name) . '" ';
  391. }
  392. $this->_storeHeader($headerName, $name .'<'. $email . '>', true);
  393. }
  394. /**
  395. * Adds To-header and recipient
  396. *
  397. * @param string $name
  398. * @param string $email
  399. * @return Zend_Mail Provides fluent interface
  400. */
  401. public function addTo($email, $name='')
  402. {
  403. $this->_addRecipientAndHeader('To', $name, $email);
  404. return $this;
  405. }
  406. /**
  407. * Adds Cc-header and recipient
  408. *
  409. * @param string $name
  410. * @param string $email
  411. * @return Zend_Mail Provides fluent interface
  412. */
  413. public function addCc($email, $name='')
  414. {
  415. $this->_addRecipientAndHeader('Cc', $name, $email);
  416. return $this;
  417. }
  418. /**
  419. * Adds Bcc recipient
  420. *
  421. * @param string $email
  422. * @return Zend_Mail Provides fluent interface
  423. */
  424. public function addBcc($email)
  425. {
  426. $this->_addRecipientAndHeader('Bcc', '', $email);
  427. return $this;
  428. }
  429. /**
  430. * Return list of recipient email addresses
  431. *
  432. * @return array (of strings)
  433. */
  434. public function getRecipients()
  435. {
  436. return array_keys($this->_recipients);
  437. }
  438. /**
  439. * Sets From-header and sender of the message
  440. *
  441. * @param string $email
  442. * @param string $name
  443. * @return Zend_Mail Provides fluent interface
  444. * @throws Zend_Mail_Exception if called subsequent times
  445. */
  446. public function setFrom($email, $name = '')
  447. {
  448. if ($this->_from === null) {
  449. $email = strtr($email,"\r\n\t",'???');
  450. $this->_from = $email;
  451. $this->_storeHeader('From', $this->_encodeHeader('"'.$name.'"').' <'.$email.'>', true);
  452. } else {
  453. throw new Zend_Mail_Exception('From Header set twice');
  454. }
  455. return $this;
  456. }
  457. /**
  458. * Returns the sender of the mail
  459. *
  460. * @return string
  461. */
  462. public function getFrom()
  463. {
  464. return $this->_from;
  465. }
  466. /**
  467. * Sets the Return-Path header for an email
  468. *
  469. * @param string $email
  470. * @return Zend_Mail Provides fluent interface
  471. * @throws Zend_Mail_Exception if set multiple times
  472. */
  473. public function setReturnPath($email)
  474. {
  475. if ($this->_returnPath === null) {
  476. $email = strtr($email,"\r\n\t",'???');
  477. $this->_returnPath = $email;
  478. $this->_storeHeader('Return-Path', $email, false);
  479. } else {
  480. throw new Zend_Mail_Exception('Return-Path Header set twice');
  481. }
  482. return $this;
  483. }
  484. /**
  485. * Returns the current Return-Path address for the email
  486. *
  487. * If no Return-Path header is set, returns the value of {@link $_from}.
  488. *
  489. * @return string
  490. */
  491. public function getReturnPath()
  492. {
  493. if (null !== $this->_returnPath) {
  494. return $this->_returnPath;
  495. }
  496. return $this->_from;
  497. }
  498. /**
  499. * Sets the subject of the message
  500. *
  501. * @param string $subject
  502. * @return Zend_Mail Provides fluent interface
  503. */
  504. public function setSubject($subject)
  505. {
  506. if ($this->_subject === null) {
  507. $subject = strtr($subject,"\r\n\t",'???');
  508. $this->_subject = $this->_encodeHeader($subject);
  509. $this->_storeHeader('Subject', $this->_subject);
  510. } else {
  511. throw new Zend_Mail_Exception('Subject set twice');
  512. }
  513. return $this;
  514. }
  515. /**
  516. * Returns the encoded subject of the message
  517. *
  518. * @return string
  519. */
  520. public function getSubject()
  521. {
  522. return $this->_subject;
  523. }
  524. /**
  525. * Add a custom header to the message
  526. *
  527. * @param string $name
  528. * @param string $value
  529. * @param boolean $append
  530. * @return Zend_Mail Provides fluent interface
  531. * @throws Zend_Mail_Exception on attempts to create standard headers
  532. */
  533. public function addHeader($name, $value, $append = false)
  534. {
  535. if (in_array(strtolower($name), array('to', 'cc', 'bcc', 'from', 'subject', 'return-path'))) {
  536. throw new Zend_Mail_Exception('Cannot set standard header from addHeader()');
  537. }
  538. $value = strtr($value,"\r\n\t",'???');
  539. $value = $this->_encodeHeader($value);
  540. $this->_storeHeader($name, $value, $append);
  541. }
  542. /**
  543. * Return mail headers
  544. *
  545. * @return void
  546. */
  547. public function getHeaders()
  548. {
  549. return $this->_headers;
  550. }
  551. /**
  552. * Sends this email using the given transport or a previously
  553. * set DefaultTransport or the internal mail function if no
  554. * default transport had been set.
  555. *
  556. * @param Zend_Mail_Transport_Abstract $transport
  557. * @return Zend_Mail Provides fluent interface
  558. */
  559. public function send($transport = null)
  560. {
  561. if ($transport === null) {
  562. if (! self::$_defaultTransport instanceof Zend_Mail_Transport_Abstract) {
  563. require_once 'Zend/Mail/Transport/Sendmail.php';
  564. $transport = new Zend_Mail_Transport_Sendmail();
  565. } else {
  566. $transport = self::$_defaultTransport;
  567. }
  568. }
  569. $transport->send($this);
  570. return $this;
  571. }
  572. }