PageRenderTime 56ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/PEAR/Mail/mime.php

https://bitbucket.org/sunil_nextbits/magento2
PHP | 1095 lines | 670 code | 67 blank | 358 comment | 66 complexity | 93a00275edb0d9e94364ae11175741d1 MD5 | raw file
  1. <?php
  2. /**
  3. * The Mail_Mime class is used to create MIME E-mail messages
  4. *
  5. * The Mail_Mime class provides an OO interface to create MIME
  6. * enabled email messages. This way you can create emails that
  7. * contain plain-text bodies, HTML bodies, attachments, inline
  8. * images and specific headers.
  9. *
  10. * Compatible with PHP versions 4 and 5
  11. *
  12. * LICENSE: This LICENSE is in the BSD license style.
  13. * Copyright (c) 2002-2003, Richard Heyes <richard@phpguru.org>
  14. * Copyright (c) 2003-2006, PEAR <pear-group@php.net>
  15. * All rights reserved.
  16. *
  17. * Redistribution and use in source and binary forms, with or
  18. * without modification, are permitted provided that the following
  19. * conditions are met:
  20. *
  21. * - Redistributions of source code must retain the above copyright
  22. * notice, this list of conditions and the following disclaimer.
  23. * - Redistributions in binary form must reproduce the above copyright
  24. * notice, this list of conditions and the following disclaimer in the
  25. * documentation and/or other materials provided with the distribution.
  26. * - Neither the name of the authors, nor the names of its contributors
  27. * may be used to endorse or promote products derived from this
  28. * software without specific prior written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  31. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  32. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  34. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  35. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  36. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  37. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  38. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  39. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  40. * THE POSSIBILITY OF SUCH DAMAGE.
  41. *
  42. * @category Mail
  43. * @package Mail_Mime
  44. * @author Richard Heyes <richard@phpguru.org>
  45. * @author Tomas V.V. Cox <cox@idecnet.com>
  46. * @author Cipriano Groenendal <cipri@php.net>
  47. * @author Sean Coates <sean@php.net>
  48. * @copyright 2003-2006 PEAR <pear-group@php.net>
  49. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  50. * @version CVS: $Id: mime.php,v 1.81 2007/06/21 19:08:28 cipri Exp $
  51. * @link http://pear.php.net/package/Mail_mime
  52. *
  53. * This class is based on HTML Mime Mail class from
  54. * Richard Heyes <richard@phpguru.org> which was based also
  55. * in the mime_mail.class by Tobias Ratschiller <tobias@dnet.it>
  56. * and Sascha Schumann <sascha@schumann.cx>
  57. */
  58. /**
  59. * require PEAR
  60. *
  61. * This package depends on PEAR to raise errors.
  62. */
  63. require_once 'PEAR.php';
  64. /**
  65. * require Mail_mimePart
  66. *
  67. * Mail_mimePart contains the code required to
  68. * create all the different parts a mail can
  69. * consist of.
  70. */
  71. require_once 'Mail/mimePart.php';
  72. /**
  73. * The Mail_Mime class provides an OO interface to create MIME
  74. * enabled email messages. This way you can create emails that
  75. * contain plain-text bodies, HTML bodies, attachments, inline
  76. * images and specific headers.
  77. *
  78. * @category Mail
  79. * @package Mail_Mime
  80. * @author Richard Heyes <richard@phpguru.org>
  81. * @author Tomas V.V. Cox <cox@idecnet.com>
  82. * @author Cipriano Groenendal <cipri@php.net>
  83. * @author Sean Coates <sean@php.net>
  84. * @copyright 2003-2006 PEAR <pear-group@php.net>
  85. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  86. * @version Release: @package_version@
  87. * @link http://pear.php.net/package/Mail_mime
  88. */
  89. class Mail_mime
  90. {
  91. /**
  92. * Contains the plain text part of the email
  93. *
  94. * @var string
  95. * @access private
  96. */
  97. var $_txtbody;
  98. /**
  99. * Contains the html part of the email
  100. *
  101. * @var string
  102. * @access private
  103. */
  104. var $_htmlbody;
  105. /**
  106. * contains the mime encoded text
  107. *
  108. * @var string
  109. * @access private
  110. */
  111. var $_mime;
  112. /**
  113. * contains the multipart content
  114. *
  115. * @var string
  116. * @access private
  117. */
  118. var $_multipart;
  119. /**
  120. * list of the attached images
  121. *
  122. * @var array
  123. * @access private
  124. */
  125. var $_html_images = array();
  126. /**
  127. * list of the attachements
  128. *
  129. * @var array
  130. * @access private
  131. */
  132. var $_parts = array();
  133. /**
  134. * Build parameters
  135. *
  136. * @var array
  137. * @access private
  138. */
  139. var $_build_params = array();
  140. /**
  141. * Headers for the mail
  142. *
  143. * @var array
  144. * @access private
  145. */
  146. var $_headers = array();
  147. /**
  148. * End Of Line sequence (for serialize)
  149. *
  150. * @var string
  151. * @access private
  152. */
  153. var $_eol;
  154. /**
  155. * Constructor function.
  156. *
  157. * @param string $crlf what type of linebreak to use.
  158. * Defaults to "\r\n"
  159. *
  160. * @return void
  161. *
  162. * @access public
  163. */
  164. function Mail_mime($crlf = "\r\n")
  165. {
  166. $this->_setEOL($crlf);
  167. $this->_build_params = array(
  168. 'head_encoding' => 'quoted-printable',
  169. 'text_encoding' => '7bit',
  170. 'html_encoding' => 'quoted-printable',
  171. '7bit_wrap' => 998,
  172. 'html_charset' => 'ISO-8859-1',
  173. 'text_charset' => 'ISO-8859-1',
  174. 'head_charset' => 'ISO-8859-1'
  175. );
  176. }
  177. /**
  178. * wakeup function called by unserialize. It re-sets the EOL constant
  179. *
  180. * @access private
  181. * @return void
  182. */
  183. function __wakeup()
  184. {
  185. $this->_setEOL($this->_eol);
  186. }
  187. /**
  188. * Accessor function to set the body text. Body text is used if
  189. * it's not an html mail being sent or else is used to fill the
  190. * text/plain part that emails clients who don't support
  191. * html should show.
  192. *
  193. * @param string $data Either a string or
  194. * the file name with the contents
  195. * @param bool $isfile If true the first param should be treated
  196. * as a file name, else as a string (default)
  197. * @param bool $append If true the text or file is appended to
  198. * the existing body, else the old body is
  199. * overwritten
  200. *
  201. * @return mixed true on success or PEAR_Error object
  202. * @access public
  203. */
  204. function setTXTBody($data, $isfile = false, $append = false)
  205. {
  206. if (!$isfile) {
  207. if (!$append) {
  208. $this->_txtbody = $data;
  209. } else {
  210. $this->_txtbody .= $data;
  211. }
  212. } else {
  213. $cont = $this->_file2str($data);
  214. if (PEAR::isError($cont)) {
  215. return $cont;
  216. }
  217. if (!$append) {
  218. $this->_txtbody = $cont;
  219. } else {
  220. $this->_txtbody .= $cont;
  221. }
  222. }
  223. return true;
  224. }
  225. /**
  226. * Adds a html part to the mail.
  227. *
  228. * @param string $data either a string or the file name with the
  229. * contents
  230. * @param bool $isfile a flag that determines whether $data is a
  231. * filename, or a string(false, default)
  232. *
  233. * @return bool true on success
  234. * @access public
  235. */
  236. function setHTMLBody($data, $isfile = false)
  237. {
  238. if (!$isfile) {
  239. $this->_htmlbody = $data;
  240. } else {
  241. $cont = $this->_file2str($data);
  242. if (PEAR::isError($cont)) {
  243. return $cont;
  244. }
  245. $this->_htmlbody = $cont;
  246. }
  247. return true;
  248. }
  249. /**
  250. * Adds an image to the list of embedded images.
  251. *
  252. * @param string $file the image file name OR image data itself
  253. * @param string $c_type the content type
  254. * @param string $name the filename of the image.
  255. * Only used if $file is the image data.
  256. * @param bool $isfile whether $file is a filename or not.
  257. * Defaults to true
  258. *
  259. * @return bool true on success
  260. * @access public
  261. */
  262. function addHTMLImage($file, $c_type='application/octet-stream',
  263. $name = '', $isfile = true)
  264. {
  265. $filedata = ($isfile === true) ? $this->_file2str($file)
  266. : $file;
  267. if ($isfile === true) {
  268. $filename = ($name == '' ? $file : $name);
  269. } else {
  270. $filename = $name;
  271. }
  272. if (PEAR::isError($filedata)) {
  273. return $filedata;
  274. }
  275. $this->_html_images[] = array(
  276. 'body' => $filedata,
  277. 'name' => $filename,
  278. 'c_type' => $c_type,
  279. 'cid' => md5(uniqid(time()))
  280. );
  281. return true;
  282. }
  283. /**
  284. * Adds a file to the list of attachments.
  285. *
  286. * @param string $file The file name of the file to attach
  287. * OR the file contents itself
  288. * @param string $c_type The content type
  289. * @param string $name The filename of the attachment
  290. * Only use if $file is the contents
  291. * @param bool $isfile Whether $file is a filename or not
  292. * Defaults to true
  293. * @param string $encoding The type of encoding to use.
  294. * Defaults to base64.
  295. * Possible values: 7bit, 8bit, base64,
  296. * or quoted-printable.
  297. * @param string $disposition The content-disposition of this file
  298. * Defaults to attachment.
  299. * Possible values: attachment, inline.
  300. * @param string $charset The character set used in the filename
  301. * of this attachment.
  302. * @param string $language The language of the attachment
  303. * @param string $location The RFC 2557.4 location of the attachment
  304. *
  305. * @return mixed true on success or PEAR_Error object
  306. * @access public
  307. */
  308. function addAttachment($file,
  309. $c_type = 'application/octet-stream',
  310. $name = '',
  311. $isfile = true,
  312. $encoding = 'base64',
  313. $disposition = 'attachment',
  314. $charset = '',
  315. $language = '',
  316. $location = '')
  317. {
  318. $filedata = ($isfile === true) ? $this->_file2str($file)
  319. : $file;
  320. if ($isfile === true) {
  321. // Force the name the user supplied, otherwise use $file
  322. $filename = (strlen($name)) ? $name : $file;
  323. } else {
  324. $filename = $name;
  325. }
  326. if (!strlen($filename)) {
  327. $msg = "The supplied filename for the attachment can't be empty";
  328. $err = PEAR::raiseError($msg);
  329. return $err;
  330. }
  331. $filename = basename($filename);
  332. if (PEAR::isError($filedata)) {
  333. return $filedata;
  334. }
  335. $this->_parts[] = array(
  336. 'body' => $filedata,
  337. 'name' => $filename,
  338. 'c_type' => $c_type,
  339. 'encoding' => $encoding,
  340. 'charset' => $charset,
  341. 'language' => $language,
  342. 'location' => $location,
  343. 'disposition' => $disposition
  344. );
  345. return true;
  346. }
  347. /**
  348. * Get the contents of the given file name as string
  349. *
  350. * @param string $file_name path of file to process
  351. *
  352. * @return string contents of $file_name
  353. * @access private
  354. */
  355. function &_file2str($file_name)
  356. {
  357. if (!is_readable($file_name)) {
  358. $err = PEAR::raiseError('File is not readable ' . $file_name);
  359. return $err;
  360. }
  361. if (!$fd = fopen($file_name, 'rb')) {
  362. $err = PEAR::raiseError('Could not open ' . $file_name);
  363. return $err;
  364. }
  365. $filesize = filesize($file_name);
  366. if ($filesize == 0) {
  367. $cont = "";
  368. } else {
  369. if ($magic_quote_setting = get_magic_quotes_runtime()) {
  370. set_magic_quotes_runtime(0);
  371. }
  372. $cont = fread($fd, $filesize);
  373. if ($magic_quote_setting) {
  374. set_magic_quotes_runtime($magic_quote_setting);
  375. }
  376. }
  377. fclose($fd);
  378. return $cont;
  379. }
  380. /**
  381. * Adds a text subpart to the mimePart object and
  382. * returns it during the build process.
  383. *
  384. * @param mixed &$obj The object to add the part to, or
  385. * null if a new object is to be created.
  386. * @param string $text The text to add.
  387. *
  388. * @return object The text mimePart object
  389. * @access private
  390. */
  391. function &_addTextPart(&$obj, $text)
  392. {
  393. $params['content_type'] = 'text/plain';
  394. $params['encoding'] = $this->_build_params['text_encoding'];
  395. $params['charset'] = $this->_build_params['text_charset'];
  396. if (is_object($obj)) {
  397. $ret = $obj->addSubpart($text, $params);
  398. return $ret;
  399. } else {
  400. $ret = new Mail_mimePart($text, $params);
  401. return $ret;
  402. }
  403. }
  404. /**
  405. * Adds a html subpart to the mimePart object and
  406. * returns it during the build process.
  407. *
  408. * @param mixed &$obj The object to add the part to, or
  409. * null if a new object is to be created.
  410. *
  411. * @return object The html mimePart object
  412. * @access private
  413. */
  414. function &_addHtmlPart(&$obj)
  415. {
  416. $params['content_type'] = 'text/html';
  417. $params['encoding'] = $this->_build_params['html_encoding'];
  418. $params['charset'] = $this->_build_params['html_charset'];
  419. if (is_object($obj)) {
  420. $ret = $obj->addSubpart($this->_htmlbody, $params);
  421. return $ret;
  422. } else {
  423. $ret = new Mail_mimePart($this->_htmlbody, $params);
  424. return $ret;
  425. }
  426. }
  427. /**
  428. * Creates a new mimePart object, using multipart/mixed as
  429. * the initial content-type and returns it during the
  430. * build process.
  431. *
  432. * @return object The multipart/mixed mimePart object
  433. * @access private
  434. */
  435. function &_addMixedPart()
  436. {
  437. $params = array();
  438. $params['content_type'] = 'multipart/mixed';
  439. //Create empty multipart/mixed Mail_mimePart object to return
  440. $ret = new Mail_mimePart('', $params);
  441. return $ret;
  442. }
  443. /**
  444. * Adds a multipart/alternative part to a mimePart
  445. * object (or creates one), and returns it during
  446. * the build process.
  447. *
  448. * @param mixed &$obj The object to add the part to, or
  449. * null if a new object is to be created.
  450. *
  451. * @return object The multipart/mixed mimePart object
  452. * @access private
  453. */
  454. function &_addAlternativePart(&$obj)
  455. {
  456. $params['content_type'] = 'multipart/alternative';
  457. if (is_object($obj)) {
  458. return $obj->addSubpart('', $params);
  459. } else {
  460. $ret = new Mail_mimePart('', $params);
  461. return $ret;
  462. }
  463. }
  464. /**
  465. * Adds a multipart/related part to a mimePart
  466. * object (or creates one), and returns it during
  467. * the build process.
  468. *
  469. * @param mixed &$obj The object to add the part to, or
  470. * null if a new object is to be created
  471. *
  472. * @return object The multipart/mixed mimePart object
  473. * @access private
  474. */
  475. function &_addRelatedPart(&$obj)
  476. {
  477. $params['content_type'] = 'multipart/related';
  478. if (is_object($obj)) {
  479. return $obj->addSubpart('', $params);
  480. } else {
  481. $ret = new Mail_mimePart('', $params);
  482. return $ret;
  483. }
  484. }
  485. /**
  486. * Adds an html image subpart to a mimePart object
  487. * and returns it during the build process.
  488. *
  489. * @param object &$obj The mimePart to add the image to
  490. * @param array $value The image information
  491. *
  492. * @return object The image mimePart object
  493. * @access private
  494. */
  495. function &_addHtmlImagePart(&$obj, $value)
  496. {
  497. $params['content_type'] = $value['c_type'];
  498. $params['encoding'] = 'base64';
  499. $params['disposition'] = 'inline';
  500. $params['dfilename'] = $value['name'];
  501. $params['cid'] = $value['cid'];
  502. $ret = $obj->addSubpart($value['body'], $params);
  503. return $ret;
  504. }
  505. /**
  506. * Adds an attachment subpart to a mimePart object
  507. * and returns it during the build process.
  508. *
  509. * @param object &$obj The mimePart to add the image to
  510. * @param array $value The attachment information
  511. *
  512. * @return object The image mimePart object
  513. * @access private
  514. */
  515. function &_addAttachmentPart(&$obj, $value)
  516. {
  517. $params['dfilename'] = $value['name'];
  518. $params['encoding'] = $value['encoding'];
  519. if ($value['charset']) {
  520. $params['charset'] = $value['charset'];
  521. }
  522. if ($value['language']) {
  523. $params['language'] = $value['language'];
  524. }
  525. if ($value['location']) {
  526. $params['location'] = $value['location'];
  527. }
  528. $params['content_type'] = $value['c_type'];
  529. $params['disposition'] = isset($value['disposition']) ?
  530. $value['disposition'] : 'attachment';
  531. $ret = $obj->addSubpart($value['body'], $params);
  532. return $ret;
  533. }
  534. /**
  535. * Returns the complete e-mail, ready to send using an alternative
  536. * mail delivery method. Note that only the mailpart that is made
  537. * with Mail_Mime is created. This means that,
  538. * YOU WILL HAVE NO TO: HEADERS UNLESS YOU SET IT YOURSELF
  539. * using the $xtra_headers parameter!
  540. *
  541. * @param string $separation The separation etween these two parts.
  542. * @param array $build_params The Build parameters passed to the
  543. * &get() function. See &get for more info.
  544. * @param array $xtra_headers The extra headers that should be passed
  545. * to the &headers() function.
  546. * See that function for more info.
  547. * @param bool $overwrite Overwrite the existing headers with new.
  548. *
  549. * @return string The complete e-mail.
  550. * @access public
  551. */
  552. function getMessage(
  553. $separation = null,
  554. $build_params = null,
  555. $xtra_headers = null,
  556. $overwrite = false
  557. )
  558. {
  559. if ($separation === null) {
  560. $separation = MAIL_MIME_CRLF;
  561. }
  562. $body = $this->get($build_params);
  563. $head = $this->txtHeaders($xtra_headers, $overwrite);
  564. $mail = $head . $separation . $body;
  565. return $mail;
  566. }
  567. /**
  568. * Builds the multipart message from the list ($this->_parts) and
  569. * returns the mime content.
  570. *
  571. * @param array $build_params Build parameters that change the way the email
  572. * is built. Should be associative. Can contain:
  573. * head_encoding - What encoding to use for the headers.
  574. * Options: quoted-printable or base64
  575. * Default is quoted-printable
  576. * text_encoding - What encoding to use for plain text
  577. * Options: 7bit, 8bit,
  578. * base64, or quoted-printable
  579. * Default is 7bit
  580. * html_encoding - What encoding to use for html
  581. * Options: 7bit, 8bit,
  582. * base64, or quoted-printable
  583. * Default is quoted-printable
  584. * 7bit_wrap - Number of characters before text is
  585. * wrapped in 7bit encoding
  586. * Default is 998
  587. * html_charset - The character set to use for html.
  588. * Default is iso-8859-1
  589. * text_charset - The character set to use for text.
  590. * Default is iso-8859-1
  591. * head_charset - The character set to use for headers.
  592. * Default is iso-8859-1
  593. *
  594. * @return string The mime content
  595. * @access public
  596. */
  597. function &get($build_params = null)
  598. {
  599. if (isset($build_params)) {
  600. while (list($key, $value) = each($build_params)) {
  601. $this->_build_params[$key] = $value;
  602. }
  603. }
  604. if (isset($this->_headers['From'])){
  605. $domain = @strstr($this->_headers['From'],'@');
  606. //Bug #11381: Illegal characters in domain ID
  607. $domain = str_replace(array("<", ">", "&", "(", ")", " ", "\"", "'"), "", $domain);
  608. $domain = urlencode($domain);
  609. foreach($this->_html_images as $i => $img){
  610. $this->_html_images[$i]['cid'] = $this->_html_images[$i]['cid'] . $domain;
  611. }
  612. }
  613. if (count($this->_html_images) AND isset($this->_htmlbody)) {
  614. foreach ($this->_html_images as $key => $value) {
  615. $regex = array();
  616. $regex[] = '#(\s)((?i)src|background|href(?-i))\s*=\s*(["\']?)' .
  617. preg_quote($value['name'], '#') . '\3#';
  618. $regex[] = '#(?i)url(?-i)\(\s*(["\']?)' .
  619. preg_quote($value['name'], '#') . '\1\s*\)#';
  620. $rep = array();
  621. $rep[] = '\1\2=\3cid:' . $value['cid'] .'\3';
  622. $rep[] = 'url(\1cid:' . $value['cid'] . '\2)';
  623. $this->_htmlbody = preg_replace($regex, $rep, $this->_htmlbody);
  624. $this->_html_images[$key]['name'] =
  625. basename($this->_html_images[$key]['name']);
  626. }
  627. }
  628. $null = null;
  629. $attachments = count($this->_parts) ? true : false;
  630. $html_images = count($this->_html_images) ? true : false;
  631. $html = strlen($this->_htmlbody) ? true : false;
  632. $text = (!$html AND strlen($this->_txtbody)) ? true : false;
  633. switch (true) {
  634. case $text AND !$attachments:
  635. $message =& $this->_addTextPart($null, $this->_txtbody);
  636. break;
  637. case !$text AND !$html AND $attachments:
  638. $message =& $this->_addMixedPart();
  639. for ($i = 0; $i < count($this->_parts); $i++) {
  640. $this->_addAttachmentPart($message, $this->_parts[$i]);
  641. }
  642. break;
  643. case $text AND $attachments:
  644. $message =& $this->_addMixedPart();
  645. $this->_addTextPart($message, $this->_txtbody);
  646. for ($i = 0; $i < count($this->_parts); $i++) {
  647. $this->_addAttachmentPart($message, $this->_parts[$i]);
  648. }
  649. break;
  650. case $html AND !$attachments AND !$html_images:
  651. if (isset($this->_txtbody)) {
  652. $message =& $this->_addAlternativePart($null);
  653. $this->_addTextPart($message, $this->_txtbody);
  654. $this->_addHtmlPart($message);
  655. } else {
  656. $message =& $this->_addHtmlPart($null);
  657. }
  658. break;
  659. case $html AND !$attachments AND $html_images:
  660. $message =& $this->_addRelatedPart($null);
  661. if (isset($this->_txtbody)) {
  662. $alt =& $this->_addAlternativePart($message);
  663. $this->_addTextPart($alt, $this->_txtbody);
  664. $this->_addHtmlPart($alt);
  665. } else {
  666. $this->_addHtmlPart($message);
  667. }
  668. for ($i = 0; $i < count($this->_html_images); $i++) {
  669. $this->_addHtmlImagePart($message, $this->_html_images[$i]);
  670. }
  671. break;
  672. case $html AND $attachments AND !$html_images:
  673. $message =& $this->_addMixedPart();
  674. if (isset($this->_txtbody)) {
  675. $alt =& $this->_addAlternativePart($message);
  676. $this->_addTextPart($alt, $this->_txtbody);
  677. $this->_addHtmlPart($alt);
  678. } else {
  679. $this->_addHtmlPart($message);
  680. }
  681. for ($i = 0; $i < count($this->_parts); $i++) {
  682. $this->_addAttachmentPart($message, $this->_parts[$i]);
  683. }
  684. break;
  685. case $html AND $attachments AND $html_images:
  686. $message =& $this->_addMixedPart();
  687. if (isset($this->_txtbody)) {
  688. $alt =& $this->_addAlternativePart($message);
  689. $this->_addTextPart($alt, $this->_txtbody);
  690. $rel =& $this->_addRelatedPart($alt);
  691. } else {
  692. $rel =& $this->_addRelatedPart($message);
  693. }
  694. $this->_addHtmlPart($rel);
  695. for ($i = 0; $i < count($this->_html_images); $i++) {
  696. $this->_addHtmlImagePart($rel, $this->_html_images[$i]);
  697. }
  698. for ($i = 0; $i < count($this->_parts); $i++) {
  699. $this->_addAttachmentPart($message, $this->_parts[$i]);
  700. }
  701. break;
  702. }
  703. if (isset($message)) {
  704. $output = $message->encode();
  705. $this->_headers = array_merge($this->_headers,
  706. $output['headers']);
  707. $body = $output['body'];
  708. return $body;
  709. } else {
  710. $ret = false;
  711. return $ret;
  712. }
  713. }
  714. /**
  715. * Returns an array with the headers needed to prepend to the email
  716. * (MIME-Version and Content-Type). Format of argument is:
  717. * $array['header-name'] = 'header-value';
  718. *
  719. * @param array $xtra_headers Assoc array with any extra headers.
  720. * Optional.
  721. * @param bool $overwrite Overwrite already existing headers.
  722. *
  723. * @return array Assoc array with the mime headers
  724. * @access public
  725. */
  726. function &headers($xtra_headers = null, $overwrite = false)
  727. {
  728. // Content-Type header should already be present,
  729. // So just add mime version header
  730. $headers['MIME-Version'] = '1.0';
  731. if (isset($xtra_headers)) {
  732. $headers = array_merge($headers, $xtra_headers);
  733. }
  734. if ($overwrite) {
  735. $this->_headers = array_merge($this->_headers, $headers);
  736. } else {
  737. $this->_headers = array_merge($headers, $this->_headers);
  738. }
  739. $encodedHeaders = $this->_encodeHeaders($this->_headers);
  740. return $encodedHeaders;
  741. }
  742. /**
  743. * Get the text version of the headers
  744. * (usefull if you want to use the PHP mail() function)
  745. *
  746. * @param array $xtra_headers Assoc array with any extra headers.
  747. * Optional.
  748. * @param bool $overwrite Overwrite the existing heaers with new.
  749. *
  750. * @return string Plain text headers
  751. * @access public
  752. */
  753. function txtHeaders($xtra_headers = null, $overwrite = false)
  754. {
  755. $headers = $this->headers($xtra_headers, $overwrite);
  756. $ret = '';
  757. foreach ($headers as $key => $val) {
  758. $ret .= "$key: $val" . MAIL_MIME_CRLF;
  759. }
  760. return $ret;
  761. }
  762. /**
  763. * Sets the Subject header
  764. *
  765. * @param string $subject String to set the subject to.
  766. *
  767. * @return void
  768. * @access public
  769. */
  770. function setSubject($subject)
  771. {
  772. $this->_headers['Subject'] = $subject;
  773. }
  774. /**
  775. * Set an email to the From (the sender) header
  776. *
  777. * @param string $email The email address to use
  778. *
  779. * @return void
  780. * @access public
  781. */
  782. function setFrom($email)
  783. {
  784. $this->_headers['From'] = $email;
  785. }
  786. /**
  787. * Add an email to the Cc (carbon copy) header
  788. * (multiple calls to this method are allowed)
  789. *
  790. * @param string $email The email direction to add
  791. *
  792. * @return void
  793. * @access public
  794. */
  795. function addCc($email)
  796. {
  797. if (isset($this->_headers['Cc'])) {
  798. $this->_headers['Cc'] .= ", $email";
  799. } else {
  800. $this->_headers['Cc'] = $email;
  801. }
  802. }
  803. /**
  804. * Add an email to the Bcc (blank carbon copy) header
  805. * (multiple calls to this method are allowed)
  806. *
  807. * @param string $email The email direction to add
  808. *
  809. * @return void
  810. * @access public
  811. */
  812. function addBcc($email)
  813. {
  814. if (isset($this->_headers['Bcc'])) {
  815. $this->_headers['Bcc'] .= ", $email";
  816. } else {
  817. $this->_headers['Bcc'] = $email;
  818. }
  819. }
  820. /**
  821. * Since the PHP send function requires you to specifiy
  822. * recipients (To: header) separately from the other
  823. * headers, the To: header is not properly encoded.
  824. * To fix this, you can use this public method to
  825. * encode your recipients before sending to the send
  826. * function
  827. *
  828. * @param string $recipients A comma-delimited list of recipients
  829. *
  830. * @return string Encoded data
  831. * @access public
  832. */
  833. function encodeRecipients($recipients)
  834. {
  835. $input = array("To" => $recipients);
  836. $retval = $this->_encodeHeaders($input);
  837. return $retval["To"] ;
  838. }
  839. /**
  840. * Encodes a header as per RFC2047
  841. *
  842. * @param array $input The header data to encode
  843. * @param array $params Extra build parameters
  844. *
  845. * @return array Encoded data
  846. * @access private
  847. */
  848. function _encodeHeaders($input, $params = array())
  849. {
  850. $build_params = $this->_build_params;
  851. while (list($key, $value) = each($params)) {
  852. $build_params[$key] = $value;
  853. }
  854. //$hdr_name: Name of the heaer
  855. //$hdr_value: Full line of header value.
  856. //$hdr_value_out: The recombined $hdr_val-atoms, or the encoded string.
  857. $useIconv = true;
  858. if (isset($build_params['ignore-iconv'])) {
  859. $useIconv = !$build_params['ignore-iconv'];
  860. }
  861. foreach ($input as $hdr_name => $hdr_value) {
  862. if (preg_match('#([\x80-\xFF]){1}#', $hdr_value)) {
  863. if (function_exists('iconv_mime_encode') && $useIconv) {
  864. $imePrefs = array();
  865. if ($build_params['head_encoding'] == 'base64') {
  866. $imePrefs['scheme'] = 'B';
  867. } else {
  868. $imePrefs['scheme'] = 'Q';
  869. }
  870. $imePrefs['input-charset'] = $build_params['head_charset'];
  871. $imePrefs['output-charset'] = $build_params['head_charset'];
  872. $imePrefs['line-length'] = 74;
  873. $imePrefs['line-break-chars'] = "\r\n"; //Specified in RFC2047
  874. $hdr_value = iconv_mime_encode($hdr_name, $hdr_value, $imePrefs);
  875. $hdr_value = preg_replace("#^{$hdr_name}\:\ #", "", $hdr_value);
  876. } elseif ($build_params['head_encoding'] == 'base64') {
  877. //Base64 encoding has been selected.
  878. //Base64 encode the entire string
  879. $hdr_value = base64_encode($hdr_value);
  880. //Generate the header using the specified params and dynamicly
  881. //determine the maximum length of such strings.
  882. //75 is the value specified in the RFC. The first -2 is there so
  883. //the later regexp doesn't break any of the translated chars.
  884. //The -2 on the first line-regexp is to compensate for the ": "
  885. //between the header-name and the header value
  886. $prefix = '=?' . $build_params['head_charset'] . '?B?';
  887. $suffix = '?=';
  888. $maxLength = 75 - strlen($prefix . $suffix) - 2;
  889. $maxLength1stLine = $maxLength - strlen($hdr_name) - 2;
  890. //We can cut base4 every 4 characters, so the real max
  891. //we can get must be rounded down.
  892. $maxLength = $maxLength - ($maxLength % 4);
  893. $maxLength1stLine = $maxLength1stLine - ($maxLength1stLine % 4);
  894. $cutpoint = $maxLength1stLine;
  895. $hdr_value_out = $hdr_value;
  896. $output = "";
  897. while ($hdr_value_out) {
  898. //Split translated string at every $maxLength
  899. $part = substr($hdr_value_out, 0, $cutpoint);
  900. $hdr_value_out = substr($hdr_value_out, $cutpoint);
  901. $cutpoint = $maxLength;
  902. //RFC 2047 specifies that any split header should
  903. //be seperated by a CRLF SPACE.
  904. if ($output) {
  905. $output .= "\r\n ";
  906. }
  907. $output .= $prefix . $part . $suffix;
  908. }
  909. $hdr_value = $output;
  910. } else {
  911. //quoted-printable encoding has been selected
  912. //Fix for Bug #10298, Ota Mares <om@viazenetti.de>
  913. //Check if there is a double quote at beginning or end of
  914. //the string to prevent that an open or closing quote gets
  915. //ignored because it is encapsuled by an encoding pre/suffix.
  916. //Remove the double quote and set the specific prefix or
  917. //suffix variable so that we can concat the encoded string and
  918. //the double quotes back together to get the intended string.
  919. $quotePrefix = $quoteSuffix = '';
  920. if ($hdr_value{0} == '"') {
  921. $hdr_value = substr($hdr_value, 1);
  922. $quotePrefix = '"';
  923. }
  924. if ($hdr_value{strlen($hdr_value)-1} == '"') {
  925. $hdr_value = substr($hdr_value, 0, -1);
  926. $quoteSuffix = '"';
  927. }
  928. //Generate the header using the specified params and dynamicly
  929. //determine the maximum length of such strings.
  930. //75 is the value specified in the RFC. The -2 is there so
  931. //the later regexp doesn't break any of the translated chars.
  932. //The -2 on the first line-regexp is to compensate for the ": "
  933. //between the header-name and the header value
  934. $prefix = '=?' . $build_params['head_charset'] . '?Q?';
  935. $suffix = '?=';
  936. $maxLength = 75 - strlen($prefix . $suffix) - 2 - 1;
  937. $maxLength1stLine = $maxLength - strlen($hdr_name) - 2;
  938. $maxLength = $maxLength - 1;
  939. //Replace all special characters used by the encoder.
  940. $search = array('=', '_', '?', ' ');
  941. $replace = array('=3D', '=5F', '=3F', '_');
  942. $hdr_value = str_replace($search, $replace, $hdr_value);
  943. //Replace all extended characters (\x80-xFF) with their
  944. //ASCII values.
  945. $hdr_value = preg_replace('#([\x80-\xFF])#e',
  946. '"=" . strtoupper(dechex(ord("\1")))',
  947. $hdr_value);
  948. //This regexp will break QP-encoded text at every $maxLength
  949. //but will not break any encoded letters.
  950. $reg1st = "|(.{0,$maxLength1stLine}[^\=][^\=])|";
  951. $reg2nd = "|(.{0,$maxLength}[^\=][^\=])|";
  952. //Fix for Bug #10298, Ota Mares <om@viazenetti.de>
  953. //Concat the double quotes and encoded string together
  954. $hdr_value = $quotePrefix . $hdr_value . $quoteSuffix;
  955. $hdr_value_out = $hdr_value;
  956. $realMax = $maxLength1stLine + strlen($prefix . $suffix);
  957. if (strlen($hdr_value_out) >= $realMax) {
  958. //Begin with the regexp for the first line.
  959. $reg = $reg1st;
  960. $output = "";
  961. while ($hdr_value_out) {
  962. //Split translated string at every $maxLength
  963. //But make sure not to break any translated chars.
  964. $found = preg_match($reg, $hdr_value_out, $matches);
  965. //After this first line, we need to use a different
  966. //regexp for the first line.
  967. $reg = $reg2nd;
  968. //Save the found part and encapsulate it in the
  969. //prefix & suffix. Then remove the part from the
  970. //$hdr_value_out variable.
  971. if ($found) {
  972. $part = $matches[0];
  973. $len = strlen($matches[0]);
  974. $hdr_value_out = substr($hdr_value_out, $len);
  975. } else {
  976. $part = $hdr_value_out;
  977. $hdr_value_out = "";
  978. }
  979. //RFC 2047 specifies that any split header should
  980. //be seperated by a CRLF SPACE
  981. if ($output) {
  982. $output .= "\r\n ";
  983. }
  984. $output .= $prefix . $part . $suffix;
  985. }
  986. $hdr_value_out = $output;
  987. } else {
  988. $hdr_value_out = $prefix . $hdr_value_out . $suffix;
  989. }
  990. $hdr_value = $hdr_value_out;
  991. }
  992. }
  993. $input[$hdr_name] = $hdr_value;
  994. }
  995. return $input;
  996. }
  997. /**
  998. * Set the object's end-of-line and define the constant if applicable.
  999. *
  1000. * @param string $eol End Of Line sequence
  1001. *
  1002. * @return void
  1003. * @access private
  1004. */
  1005. function _setEOL($eol)
  1006. {
  1007. $this->_eol = $eol;
  1008. if (!defined('MAIL_MIME_CRLF')) {
  1009. define('MAIL_MIME_CRLF', $this->_eol, true);
  1010. }
  1011. }
  1012. } // End of class