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

/DevApp/library/Zend/Mail.php

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