mime.php 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494
  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. * @author Aleksander Machniak <alec@php.net>
  49. * @copyright 2003-2006 PEAR <pear-group@php.net>
  50. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  51. * @version CVS: $Id$
  52. * @link http://pear.php.net/package/Mail_mime
  53. *
  54. * This class is based on HTML Mime Mail class from
  55. * Richard Heyes <richard@phpguru.org> which was based also
  56. * in the mime_mail.class by Tobias Ratschiller <tobias@dnet.it>
  57. * and Sascha Schumann <sascha@schumann.cx>
  58. */
  59. /**
  60. * require PEAR
  61. *
  62. * This package depends on PEAR to raise errors.
  63. */
  64. require_once 'PEAR.php';
  65. /**
  66. * require Mail_mimePart
  67. *
  68. * Mail_mimePart contains the code required to
  69. * create all the different parts a mail can
  70. * consist of.
  71. */
  72. require_once 'Mail/mimePart.php';
  73. /**
  74. * The Mail_Mime class provides an OO interface to create MIME
  75. * enabled email messages. This way you can create emails that
  76. * contain plain-text bodies, HTML bodies, attachments, inline
  77. * images and specific headers.
  78. *
  79. * @category Mail
  80. * @package Mail_Mime
  81. * @author Richard Heyes <richard@phpguru.org>
  82. * @author Tomas V.V. Cox <cox@idecnet.com>
  83. * @author Cipriano Groenendal <cipri@php.net>
  84. * @author Sean Coates <sean@php.net>
  85. * @copyright 2003-2006 PEAR <pear-group@php.net>
  86. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  87. * @version Release: @package_version@
  88. * @link http://pear.php.net/package/Mail_mime
  89. */
  90. class Mail_mime
  91. {
  92. /**
  93. * Contains the plain text part of the email
  94. *
  95. * @var string
  96. * @access private
  97. */
  98. var $_txtbody;
  99. /**
  100. * Contains the html part of the email
  101. *
  102. * @var string
  103. * @access private
  104. */
  105. var $_htmlbody;
  106. /**
  107. * list of the attached images
  108. *
  109. * @var array
  110. * @access private
  111. */
  112. var $_html_images = array();
  113. /**
  114. * list of the attachements
  115. *
  116. * @var array
  117. * @access private
  118. */
  119. var $_parts = array();
  120. /**
  121. * Headers for the mail
  122. *
  123. * @var array
  124. * @access private
  125. */
  126. var $_headers = array();
  127. /**
  128. * Build parameters
  129. *
  130. * @var array
  131. * @access private
  132. */
  133. var $_build_params = array(
  134. // What encoding to use for the headers
  135. // Options: quoted-printable or base64
  136. 'head_encoding' => 'quoted-printable',
  137. // What encoding to use for plain text
  138. // Options: 7bit, 8bit, base64, or quoted-printable
  139. 'text_encoding' => 'quoted-printable',
  140. // What encoding to use for html
  141. // Options: 7bit, 8bit, base64, or quoted-printable
  142. 'html_encoding' => 'quoted-printable',
  143. // The character set to use for html
  144. 'html_charset' => 'ISO-8859-1',
  145. // The character set to use for text
  146. 'text_charset' => 'ISO-8859-1',
  147. // The character set to use for headers
  148. 'head_charset' => 'ISO-8859-1',
  149. // End-of-line sequence
  150. 'eol' => "\r\n",
  151. // Delay attachment files IO until building the message
  152. 'delay_file_io' => false
  153. );
  154. /**
  155. * Constructor function
  156. *
  157. * @param mixed $params Build parameters that change the way the email
  158. * is built. Should be an associative array.
  159. * See $_build_params.
  160. *
  161. * @return void
  162. * @access public
  163. */
  164. function Mail_mime($params = array())
  165. {
  166. // Backward-compatible EOL setting
  167. if (is_string($params)) {
  168. $this->_build_params['eol'] = $params;
  169. } else if (defined('MAIL_MIME_CRLF') && !isset($params['eol'])) {
  170. $this->_build_params['eol'] = MAIL_MIME_CRLF;
  171. }
  172. // Update build parameters
  173. if (!empty($params) && is_array($params)) {
  174. while (list($key, $value) = each($params)) {
  175. $this->_build_params[$key] = $value;
  176. }
  177. }
  178. }
  179. /**
  180. * Set build parameter value
  181. *
  182. * @param string $name Parameter name
  183. * @param string $value Parameter value
  184. *
  185. * @return void
  186. * @access public
  187. * @since 1.6.0
  188. */
  189. function setParam($name, $value)
  190. {
  191. $this->_build_params[$name] = $value;
  192. }
  193. /**
  194. * Get build parameter value
  195. *
  196. * @param string $name Parameter name
  197. *
  198. * @return mixed Parameter value
  199. * @access public
  200. * @since 1.6.0
  201. */
  202. function getParam($name)
  203. {
  204. return isset($this->_build_params[$name]) ? $this->_build_params[$name] : null;
  205. }
  206. /**
  207. * Accessor function to set the body text. Body text is used if
  208. * it's not an html mail being sent or else is used to fill the
  209. * text/plain part that emails clients who don't support
  210. * html should show.
  211. *
  212. * @param string $data Either a string or
  213. * the file name with the contents
  214. * @param bool $isfile If true the first param should be treated
  215. * as a file name, else as a string (default)
  216. * @param bool $append If true the text or file is appended to
  217. * the existing body, else the old body is
  218. * overwritten
  219. *
  220. * @return mixed True on success or PEAR_Error object
  221. * @access public
  222. */
  223. function setTXTBody($data, $isfile = false, $append = false)
  224. {
  225. if (!$isfile) {
  226. if (!$append) {
  227. $this->_txtbody = $data;
  228. } else {
  229. $this->_txtbody .= $data;
  230. }
  231. } else {
  232. $cont = $this->_file2str($data);
  233. if ($this->_isError($cont)) {
  234. return $cont;
  235. }
  236. if (!$append) {
  237. $this->_txtbody = $cont;
  238. } else {
  239. $this->_txtbody .= $cont;
  240. }
  241. }
  242. return true;
  243. }
  244. /**
  245. * Get message text body
  246. *
  247. * @return string Text body
  248. * @access public
  249. * @since 1.6.0
  250. */
  251. function getTXTBody()
  252. {
  253. return $this->_txtbody;
  254. }
  255. /**
  256. * Adds a html part to the mail.
  257. *
  258. * @param string $data Either a string or the file name with the
  259. * contents
  260. * @param bool $isfile A flag that determines whether $data is a
  261. * filename, or a string(false, default)
  262. *
  263. * @return bool True on success
  264. * @access public
  265. */
  266. function setHTMLBody($data, $isfile = false)
  267. {
  268. if (!$isfile) {
  269. $this->_htmlbody = $data;
  270. } else {
  271. $cont = $this->_file2str($data);
  272. if ($this->_isError($cont)) {
  273. return $cont;
  274. }
  275. $this->_htmlbody = $cont;
  276. }
  277. return true;
  278. }
  279. /**
  280. * Get message HTML body
  281. *
  282. * @return string HTML body
  283. * @access public
  284. * @since 1.6.0
  285. */
  286. function getHTMLBody()
  287. {
  288. return $this->_htmlbody;
  289. }
  290. /**
  291. * Adds an image to the list of embedded images.
  292. *
  293. * @param string $file The image file name OR image data itself
  294. * @param string $c_type The content type
  295. * @param string $name The filename of the image.
  296. * Only used if $file is the image data.
  297. * @param bool $isfile Whether $file is a filename or not.
  298. * Defaults to true
  299. * @param string $content_id Desired Content-ID of MIME part
  300. * Defaults to generated unique ID
  301. *
  302. * @return bool True on success
  303. * @access public
  304. */
  305. function addHTMLImage($file,
  306. $c_type='application/octet-stream',
  307. $name = '',
  308. $isfile = true,
  309. $content_id = null
  310. ) {
  311. $bodyfile = null;
  312. if ($isfile) {
  313. // Don't load file into memory
  314. if ($this->_build_params['delay_file_io']) {
  315. $filedata = null;
  316. $bodyfile = $file;
  317. } else {
  318. if ($this->_isError($filedata = $this->_file2str($file))) {
  319. return $filedata;
  320. }
  321. }
  322. $filename = ($name ? $name : $file);
  323. } else {
  324. $filedata = $file;
  325. $filename = $name;
  326. }
  327. if (!$content_id) {
  328. $content_id = preg_replace('/[^0-9a-zA-Z]/', '', uniqid(time(), true));
  329. }
  330. $this->_html_images[] = array(
  331. 'body' => $filedata,
  332. 'body_file' => $bodyfile,
  333. 'name' => $filename,
  334. 'c_type' => $c_type,
  335. 'cid' => $content_id
  336. );
  337. return true;
  338. }
  339. /**
  340. * Adds a file to the list of attachments.
  341. *
  342. * @param string $file The file name of the file to attach
  343. * or the file contents itself
  344. * @param string $c_type The content type
  345. * @param string $name The filename of the attachment
  346. * Only use if $file is the contents
  347. * @param bool $isfile Whether $file is a filename or not. Defaults to true
  348. * @param string $encoding The type of encoding to use. Defaults to base64.
  349. * Possible values: 7bit, 8bit, base64 or quoted-printable.
  350. * @param string $disposition The content-disposition of this file
  351. * Defaults to attachment.
  352. * Possible values: attachment, inline.
  353. * @param string $charset The character set of attachment's content.
  354. * @param string $language The language of the attachment
  355. * @param string $location The RFC 2557.4 location of the attachment
  356. * @param string $n_encoding Encoding of the attachment's name in Content-Type
  357. * By default filenames are encoded using RFC2231 method
  358. * Here you can set RFC2047 encoding (quoted-printable
  359. * or base64) instead
  360. * @param string $f_encoding Encoding of the attachment's filename
  361. * in Content-Disposition header.
  362. * @param string $description Content-Description header
  363. * @param string $h_charset The character set of the headers e.g. filename
  364. * If not specified, $charset will be used
  365. * @param array $add_headers Additional part headers. Array keys can be in form
  366. * of <header_name>:<parameter_name>
  367. *
  368. * @return mixed True on success or PEAR_Error object
  369. * @access public
  370. */
  371. function addAttachment($file,
  372. $c_type = 'application/octet-stream',
  373. $name = '',
  374. $isfile = true,
  375. $encoding = 'base64',
  376. $disposition = 'attachment',
  377. $charset = '',
  378. $language = '',
  379. $location = '',
  380. $n_encoding = null,
  381. $f_encoding = null,
  382. $description = '',
  383. $h_charset = null,
  384. $add_headers = array()
  385. ) {
  386. $bodyfile = null;
  387. if ($isfile) {
  388. // Don't load file into memory
  389. if ($this->_build_params['delay_file_io']) {
  390. $filedata = null;
  391. $bodyfile = $file;
  392. } else {
  393. if ($this->_isError($filedata = $this->_file2str($file))) {
  394. return $filedata;
  395. }
  396. }
  397. // Force the name the user supplied, otherwise use $file
  398. $filename = ($name ? $name : $this->_basename($file));
  399. } else {
  400. $filedata = $file;
  401. $filename = $name;
  402. }
  403. if (!strlen($filename)) {
  404. $msg = "The supplied filename for the attachment can't be empty";
  405. return $this->_raiseError($msg);
  406. }
  407. $this->_parts[] = array(
  408. 'body' => $filedata,
  409. 'body_file' => $bodyfile,
  410. 'name' => $filename,
  411. 'c_type' => $c_type,
  412. 'charset' => $charset,
  413. 'encoding' => $encoding,
  414. 'language' => $language,
  415. 'location' => $location,
  416. 'disposition' => $disposition,
  417. 'description' => $description,
  418. 'add_headers' => $add_headers,
  419. 'name_encoding' => $n_encoding,
  420. 'filename_encoding' => $f_encoding,
  421. 'headers_charset' => $h_charset,
  422. );
  423. return true;
  424. }
  425. /**
  426. * Get the contents of the given file name as string
  427. *
  428. * @param string $file_name Path of file to process
  429. *
  430. * @return string Contents of $file_name
  431. * @access private
  432. */
  433. function _file2str($file_name)
  434. {
  435. // Check state of file and raise an error properly
  436. if (!file_exists($file_name)) {
  437. return $this->_raiseError('File not found: ' . $file_name);
  438. }
  439. if (!is_file($file_name)) {
  440. return $this->_raiseError('Not a regular file: ' . $file_name);
  441. }
  442. if (!is_readable($file_name)) {
  443. return $this->_raiseError('File is not readable: ' . $file_name);
  444. }
  445. // Temporarily reset magic_quotes_runtime and read file contents
  446. if ($magic_quote_setting = get_magic_quotes_runtime()) {
  447. @ini_set('magic_quotes_runtime', 0);
  448. }
  449. $cont = file_get_contents($file_name);
  450. if ($magic_quote_setting) {
  451. @ini_set('magic_quotes_runtime', $magic_quote_setting);
  452. }
  453. return $cont;
  454. }
  455. /**
  456. * Adds a text subpart to the mimePart object and
  457. * returns it during the build process.
  458. *
  459. * @param mixed &$obj The object to add the part to, or
  460. * anything else if a new object is to be created.
  461. * @param string $text The text to add.
  462. *
  463. * @return object The text mimePart object
  464. * @access private
  465. */
  466. function &_addTextPart(&$obj, $text = '')
  467. {
  468. $params['content_type'] = 'text/plain';
  469. $params['encoding'] = $this->_build_params['text_encoding'];
  470. $params['charset'] = $this->_build_params['text_charset'];
  471. $params['eol'] = $this->_build_params['eol'];
  472. if (is_object($obj)) {
  473. $ret = $obj->addSubpart($text, $params);
  474. } else {
  475. $ret = new Mail_mimePart($text, $params);
  476. }
  477. return $ret;
  478. }
  479. /**
  480. * Adds a html subpart to the mimePart object and
  481. * returns it during the build process.
  482. *
  483. * @param mixed &$obj The object to add the part to, or
  484. * anything else if a new object is to be created.
  485. *
  486. * @return object The html mimePart object
  487. * @access private
  488. */
  489. function &_addHtmlPart(&$obj)
  490. {
  491. $params['content_type'] = 'text/html';
  492. $params['encoding'] = $this->_build_params['html_encoding'];
  493. $params['charset'] = $this->_build_params['html_charset'];
  494. $params['eol'] = $this->_build_params['eol'];
  495. if (is_object($obj)) {
  496. $ret = $obj->addSubpart($this->_htmlbody, $params);
  497. } else {
  498. $ret = new Mail_mimePart($this->_htmlbody, $params);
  499. }
  500. return $ret;
  501. }
  502. /**
  503. * Creates a new mimePart object, using multipart/mixed as
  504. * the initial content-type and returns it during the
  505. * build process.
  506. *
  507. * @return object The multipart/mixed mimePart object
  508. * @access private
  509. */
  510. function &_addMixedPart()
  511. {
  512. $params['content_type'] = 'multipart/mixed';
  513. $params['eol'] = $this->_build_params['eol'];
  514. // Create empty multipart/mixed Mail_mimePart object to return
  515. $ret = new Mail_mimePart('', $params);
  516. return $ret;
  517. }
  518. /**
  519. * Adds a multipart/alternative part to a mimePart
  520. * object (or creates one), and returns it during
  521. * the build process.
  522. *
  523. * @param mixed &$obj The object to add the part to, or
  524. * anything else if a new object is to be created.
  525. *
  526. * @return object The multipart/mixed mimePart object
  527. * @access private
  528. */
  529. function &_addAlternativePart(&$obj)
  530. {
  531. $params['content_type'] = 'multipart/alternative';
  532. $params['eol'] = $this->_build_params['eol'];
  533. if (is_object($obj)) {
  534. $ret = $obj->addSubpart('', $params);
  535. } else {
  536. $ret = new Mail_mimePart('', $params);
  537. }
  538. return $ret;
  539. }
  540. /**
  541. * Adds a multipart/related part to a mimePart
  542. * object (or creates one), and returns it during
  543. * the build process.
  544. *
  545. * @param mixed &$obj The object to add the part to, or
  546. * anything else if a new object is to be created
  547. *
  548. * @return object The multipart/mixed mimePart object
  549. * @access private
  550. */
  551. function &_addRelatedPart(&$obj)
  552. {
  553. $params['content_type'] = 'multipart/related';
  554. $params['eol'] = $this->_build_params['eol'];
  555. if (is_object($obj)) {
  556. $ret = $obj->addSubpart('', $params);
  557. } else {
  558. $ret = new Mail_mimePart('', $params);
  559. }
  560. return $ret;
  561. }
  562. /**
  563. * Adds an html image subpart to a mimePart object
  564. * and returns it during the build process.
  565. *
  566. * @param object &$obj The mimePart to add the image to
  567. * @param array $value The image information
  568. *
  569. * @return object The image mimePart object
  570. * @access private
  571. */
  572. function &_addHtmlImagePart(&$obj, $value)
  573. {
  574. $params['content_type'] = $value['c_type'];
  575. $params['encoding'] = 'base64';
  576. $params['disposition'] = 'inline';
  577. $params['filename'] = $value['name'];
  578. $params['cid'] = $value['cid'];
  579. $params['body_file'] = $value['body_file'];
  580. $params['eol'] = $this->_build_params['eol'];
  581. if (!empty($value['name_encoding'])) {
  582. $params['name_encoding'] = $value['name_encoding'];
  583. }
  584. if (!empty($value['filename_encoding'])) {
  585. $params['filename_encoding'] = $value['filename_encoding'];
  586. }
  587. $ret = $obj->addSubpart($value['body'], $params);
  588. return $ret;
  589. }
  590. /**
  591. * Adds an attachment subpart to a mimePart object
  592. * and returns it during the build process.
  593. *
  594. * @param object &$obj The mimePart to add the image to
  595. * @param array $value The attachment information
  596. *
  597. * @return object The image mimePart object
  598. * @access private
  599. */
  600. function &_addAttachmentPart(&$obj, $value)
  601. {
  602. $params['eol'] = $this->_build_params['eol'];
  603. $params['filename'] = $value['name'];
  604. $params['encoding'] = $value['encoding'];
  605. $params['content_type'] = $value['c_type'];
  606. $params['body_file'] = $value['body_file'];
  607. $params['disposition'] = isset($value['disposition']) ?
  608. $value['disposition'] : 'attachment';
  609. // content charset
  610. if (!empty($value['charset'])) {
  611. $params['charset'] = $value['charset'];
  612. }
  613. // headers charset (filename, description)
  614. if (!empty($value['headers_charset'])) {
  615. $params['headers_charset'] = $value['headers_charset'];
  616. }
  617. if (!empty($value['language'])) {
  618. $params['language'] = $value['language'];
  619. }
  620. if (!empty($value['location'])) {
  621. $params['location'] = $value['location'];
  622. }
  623. if (!empty($value['name_encoding'])) {
  624. $params['name_encoding'] = $value['name_encoding'];
  625. }
  626. if (!empty($value['filename_encoding'])) {
  627. $params['filename_encoding'] = $value['filename_encoding'];
  628. }
  629. if (!empty($value['description'])) {
  630. $params['description'] = $value['description'];
  631. }
  632. if (is_array($value['add_headers'])) {
  633. $params['headers'] = $value['add_headers'];
  634. }
  635. $ret = $obj->addSubpart($value['body'], $params);
  636. return $ret;
  637. }
  638. /**
  639. * Returns the complete e-mail, ready to send using an alternative
  640. * mail delivery method. Note that only the mailpart that is made
  641. * with Mail_Mime is created. This means that,
  642. * YOU WILL HAVE NO TO: HEADERS UNLESS YOU SET IT YOURSELF
  643. * using the $headers parameter!
  644. *
  645. * @param string $separation The separation between these two parts.
  646. * @param array $params The Build parameters passed to the
  647. * get() function. See get() for more info.
  648. * @param array $headers The extra headers that should be passed
  649. * to the headers() method.
  650. * See that function for more info.
  651. * @param bool $overwrite Overwrite the existing headers with new.
  652. *
  653. * @return mixed The complete e-mail or PEAR error object
  654. * @access public
  655. */
  656. function getMessage($separation = null, $params = null, $headers = null,
  657. $overwrite = false
  658. ) {
  659. if ($separation === null) {
  660. $separation = $this->_build_params['eol'];
  661. }
  662. $body = $this->get($params);
  663. if ($this->_isError($body)) {
  664. return $body;
  665. }
  666. return $this->txtHeaders($headers, $overwrite) . $separation . $body;
  667. }
  668. /**
  669. * Returns the complete e-mail body, ready to send using an alternative
  670. * mail delivery method.
  671. *
  672. * @param array $params The Build parameters passed to the
  673. * get() method. See get() for more info.
  674. *
  675. * @return mixed The e-mail body or PEAR error object
  676. * @access public
  677. * @since 1.6.0
  678. */
  679. function getMessageBody($params = null)
  680. {
  681. return $this->get($params, null, true);
  682. }
  683. /**
  684. * Writes (appends) the complete e-mail into file.
  685. *
  686. * @param string $filename Output file location
  687. * @param array $params The Build parameters passed to the
  688. * get() method. See get() for more info.
  689. * @param array $headers The extra headers that should be passed
  690. * to the headers() function.
  691. * See that function for more info.
  692. * @param bool $overwrite Overwrite the existing headers with new.
  693. *
  694. * @return mixed True or PEAR error object
  695. * @access public
  696. * @since 1.6.0
  697. */
  698. function saveMessage($filename, $params = null, $headers = null, $overwrite = false)
  699. {
  700. // Check state of file and raise an error properly
  701. if (file_exists($filename) && !is_writable($filename)) {
  702. return $this->_raiseError('File is not writable: ' . $filename);
  703. }
  704. // Temporarily reset magic_quotes_runtime and read file contents
  705. if ($magic_quote_setting = get_magic_quotes_runtime()) {
  706. @ini_set('magic_quotes_runtime', 0);
  707. }
  708. if (!($fh = fopen($filename, 'ab'))) {
  709. return $this->_raiseError('Unable to open file: ' . $filename);
  710. }
  711. // Write message headers into file (skipping Content-* headers)
  712. $head = $this->txtHeaders($headers, $overwrite, true);
  713. if (fwrite($fh, $head) === false) {
  714. return $this->_raiseError('Error writing to file: ' . $filename);
  715. }
  716. fclose($fh);
  717. if ($magic_quote_setting) {
  718. @ini_set('magic_quotes_runtime', $magic_quote_setting);
  719. }
  720. // Write the rest of the message into file
  721. $res = $this->get($params, $filename);
  722. return $res ? $res : true;
  723. }
  724. /**
  725. * Writes (appends) the complete e-mail body into file.
  726. *
  727. * @param string $filename Output file location
  728. * @param array $params The Build parameters passed to the
  729. * get() method. See get() for more info.
  730. *
  731. * @return mixed True or PEAR error object
  732. * @access public
  733. * @since 1.6.0
  734. */
  735. function saveMessageBody($filename, $params = null)
  736. {
  737. // Check state of file and raise an error properly
  738. if (file_exists($filename) && !is_writable($filename)) {
  739. return $this->_raiseError('File is not writable: ' . $filename);
  740. }
  741. // Temporarily reset magic_quotes_runtime and read file contents
  742. if ($magic_quote_setting = get_magic_quotes_runtime()) {
  743. @ini_set('magic_quotes_runtime', 0);
  744. }
  745. if (!($fh = fopen($filename, 'ab'))) {
  746. return $this->_raiseError('Unable to open file: ' . $filename);
  747. }
  748. // Write the rest of the message into file
  749. $res = $this->get($params, $filename, true);
  750. return $res ? $res : true;
  751. }
  752. /**
  753. * Builds the multipart message from the list ($this->_parts) and
  754. * returns the mime content.
  755. *
  756. * @param array $params Build parameters that change the way the email
  757. * is built. Should be associative. See $_build_params.
  758. * @param resource $filename Output file where to save the message instead of
  759. * returning it
  760. * @param boolean $skip_head True if you want to return/save only the message
  761. * without headers
  762. *
  763. * @return mixed The MIME message content string, null or PEAR error object
  764. * @access public
  765. */
  766. function get($params = null, $filename = null, $skip_head = false)
  767. {
  768. if (isset($params)) {
  769. while (list($key, $value) = each($params)) {
  770. $this->_build_params[$key] = $value;
  771. }
  772. }
  773. if (isset($this->_headers['From'])) {
  774. // Bug #11381: Illegal characters in domain ID
  775. if (preg_match('#(@[0-9a-zA-Z\-\.]+)#', $this->_headers['From'], $matches)) {
  776. $domainID = $matches[1];
  777. } else {
  778. $domainID = '@localhost';
  779. }
  780. foreach ($this->_html_images as $i => $img) {
  781. $cid = $this->_html_images[$i]['cid'];
  782. if (!preg_match('#'.preg_quote($domainID).'$#', $cid)) {
  783. $this->_html_images[$i]['cid'] = $cid . $domainID;
  784. }
  785. }
  786. }
  787. if (count($this->_html_images) && isset($this->_htmlbody)) {
  788. foreach ($this->_html_images as $key => $value) {
  789. $regex = array();
  790. $regex[] = '#(\s)((?i)src|background|href(?-i))\s*=\s*(["\']?)' .
  791. preg_quote($value['name'], '#') . '\3#';
  792. $regex[] = '#(?i)url(?-i)\(\s*(["\']?)' .
  793. preg_quote($value['name'], '#') . '\1\s*\)#';
  794. $rep = array();
  795. $rep[] = '\1\2=\3cid:' . $value['cid'] .'\3';
  796. $rep[] = 'url(\1cid:' . $value['cid'] . '\1)';
  797. $this->_htmlbody = preg_replace($regex, $rep, $this->_htmlbody);
  798. $this->_html_images[$key]['name']
  799. = $this->_basename($this->_html_images[$key]['name']);
  800. }
  801. }
  802. $this->_checkParams();
  803. $null = -1;
  804. $attachments = count($this->_parts) > 0;
  805. $html_images = count($this->_html_images) > 0;
  806. $html = strlen($this->_htmlbody) > 0;
  807. $text = !$html && strlen($this->_txtbody);
  808. switch (true) {
  809. case $text && !$attachments:
  810. $message =& $this->_addTextPart($null, $this->_txtbody);
  811. break;
  812. case !$text && !$html && $attachments:
  813. $message =& $this->_addMixedPart();
  814. for ($i = 0; $i < count($this->_parts); $i++) {
  815. $this->_addAttachmentPart($message, $this->_parts[$i]);
  816. }
  817. break;
  818. case $text && $attachments:
  819. $message =& $this->_addMixedPart();
  820. $this->_addTextPart($message, $this->_txtbody);
  821. for ($i = 0; $i < count($this->_parts); $i++) {
  822. $this->_addAttachmentPart($message, $this->_parts[$i]);
  823. }
  824. break;
  825. case $html && !$attachments && !$html_images:
  826. if (isset($this->_txtbody)) {
  827. $message =& $this->_addAlternativePart($null);
  828. $this->_addTextPart($message, $this->_txtbody);
  829. $this->_addHtmlPart($message);
  830. } else {
  831. $message =& $this->_addHtmlPart($null);
  832. }
  833. break;
  834. case $html && !$attachments && $html_images:
  835. // * Content-Type: multipart/alternative;
  836. // * text
  837. // * Content-Type: multipart/related;
  838. // * html
  839. // * image...
  840. if (isset($this->_txtbody)) {
  841. $message =& $this->_addAlternativePart($null);
  842. $this->_addTextPart($message, $this->_txtbody);
  843. $ht =& $this->_addRelatedPart($message);
  844. $this->_addHtmlPart($ht);
  845. for ($i = 0; $i < count($this->_html_images); $i++) {
  846. $this->_addHtmlImagePart($ht, $this->_html_images[$i]);
  847. }
  848. } else {
  849. // * Content-Type: multipart/related;
  850. // * html
  851. // * image...
  852. $message =& $this->_addRelatedPart($null);
  853. $this->_addHtmlPart($message);
  854. for ($i = 0; $i < count($this->_html_images); $i++) {
  855. $this->_addHtmlImagePart($message, $this->_html_images[$i]);
  856. }
  857. }
  858. /*
  859. // #13444, #9725: the code below was a non-RFC compliant hack
  860. // * Content-Type: multipart/related;
  861. // * Content-Type: multipart/alternative;
  862. // * text
  863. // * html
  864. // * image...
  865. $message =& $this->_addRelatedPart($null);
  866. if (isset($this->_txtbody)) {
  867. $alt =& $this->_addAlternativePart($message);
  868. $this->_addTextPart($alt, $this->_txtbody);
  869. $this->_addHtmlPart($alt);
  870. } else {
  871. $this->_addHtmlPart($message);
  872. }
  873. for ($i = 0; $i < count($this->_html_images); $i++) {
  874. $this->_addHtmlImagePart($message, $this->_html_images[$i]);
  875. }
  876. */
  877. break;
  878. case $html && $attachments && !$html_images:
  879. $message =& $this->_addMixedPart();
  880. if (isset($this->_txtbody)) {
  881. $alt =& $this->_addAlternativePart($message);
  882. $this->_addTextPart($alt, $this->_txtbody);
  883. $this->_addHtmlPart($alt);
  884. } else {
  885. $this->_addHtmlPart($message);
  886. }
  887. for ($i = 0; $i < count($this->_parts); $i++) {
  888. $this->_addAttachmentPart($message, $this->_parts[$i]);
  889. }
  890. break;
  891. case $html && $attachments && $html_images:
  892. $message =& $this->_addMixedPart();
  893. if (isset($this->_txtbody)) {
  894. $alt =& $this->_addAlternativePart($message);
  895. $this->_addTextPart($alt, $this->_txtbody);
  896. $rel =& $this->_addRelatedPart($alt);
  897. } else {
  898. $rel =& $this->_addRelatedPart($message);
  899. }
  900. $this->_addHtmlPart($rel);
  901. for ($i = 0; $i < count($this->_html_images); $i++) {
  902. $this->_addHtmlImagePart($rel, $this->_html_images[$i]);
  903. }
  904. for ($i = 0; $i < count($this->_parts); $i++) {
  905. $this->_addAttachmentPart($message, $this->_parts[$i]);
  906. }
  907. break;
  908. }
  909. if (!isset($message)) {
  910. return null;
  911. }
  912. // Use saved boundary
  913. if (!empty($this->_build_params['boundary'])) {
  914. $boundary = $this->_build_params['boundary'];
  915. } else {
  916. $boundary = null;
  917. }
  918. // Write output to file
  919. if ($filename) {
  920. // Append mimePart message headers and body into file
  921. $headers = $message->encodeToFile($filename, $boundary, $skip_head);
  922. if ($this->_isError($headers)) {
  923. return $headers;
  924. }
  925. $this->_headers = array_merge($this->_headers, $headers);
  926. return null;
  927. } else {
  928. $output = $message->encode($boundary, $skip_head);
  929. if ($this->_isError($output)) {
  930. return $output;
  931. }
  932. $this->_headers = array_merge($this->_headers, $output['headers']);
  933. return $output['body'];
  934. }
  935. }
  936. /**
  937. * Returns an array with the headers needed to prepend to the email
  938. * (MIME-Version and Content-Type). Format of argument is:
  939. * $array['header-name'] = 'header-value';
  940. *
  941. * @param array $xtra_headers Assoc array with any extra headers (optional)
  942. * (Don't set Content-Type for multipart messages here!)
  943. * @param bool $overwrite Overwrite already existing headers.
  944. * @param bool $skip_content Don't return content headers: Content-Type,
  945. * Content-Disposition and Content-Transfer-Encoding
  946. *
  947. * @return array Assoc array with the mime headers
  948. * @access public
  949. */
  950. function headers($xtra_headers = null, $overwrite = false, $skip_content = false)
  951. {
  952. // Add mime version header
  953. $headers['MIME-Version'] = '1.0';
  954. // Content-Type and Content-Transfer-Encoding headers should already
  955. // be present if get() was called, but we'll re-set them to make sure
  956. // we got them when called before get() or something in the message
  957. // has been changed after get() [#14780]
  958. if (!$skip_content) {
  959. $headers += $this->_contentHeaders();
  960. }
  961. if (!empty($xtra_headers)) {
  962. $headers = array_merge($headers, $xtra_headers);
  963. }
  964. if ($overwrite) {
  965. $this->_headers = array_merge($this->_headers, $headers);
  966. } else {
  967. $this->_headers = array_merge($headers, $this->_headers);
  968. }
  969. $headers = $this->_headers;
  970. if ($skip_content) {
  971. unset($headers['Content-Type']);
  972. unset($headers['Content-Transfer-Encoding']);
  973. unset($headers['Content-Disposition']);
  974. } else if (!empty($this->_build_params['ctype'])) {
  975. $headers['Content-Type'] = $this->_build_params['ctype'];
  976. }
  977. $encodedHeaders = $this->_encodeHeaders($headers);
  978. return $encodedHeaders;
  979. }
  980. /**
  981. * Get the text version of the headers
  982. * (usefull if you want to use the PHP mail() function)
  983. *
  984. * @param array $xtra_headers Assoc array with any extra headers (optional)
  985. * (Don't set Content-Type for multipart messages here!)
  986. * @param bool $overwrite Overwrite the existing headers with new.
  987. * @param bool $skip_content Don't return content headers: Content-Type,
  988. * Content-Disposition and Content-Transfer-Encoding
  989. *
  990. * @return string Plain text headers
  991. * @access public
  992. */
  993. function txtHeaders($xtra_headers = null, $overwrite = false, $skip_content = false)
  994. {
  995. $headers = $this->headers($xtra_headers, $overwrite, $skip_content);
  996. // Place Received: headers at the beginning of the message
  997. // Spam detectors often flag messages with it after the Subject: as spam
  998. if (isset($headers['Received'])) {
  999. $received = $headers['Received'];
  1000. unset($headers['Received']);
  1001. $headers = array('Received' => $received) + $headers;
  1002. }
  1003. $ret = '';
  1004. $eol = $this->_build_params['eol'];
  1005. foreach ($headers as $key => $val) {
  1006. if (is_array($val)) {
  1007. foreach ($val as $value) {
  1008. $ret .= "$key: $value" . $eol;
  1009. }
  1010. } else {
  1011. $ret .= "$key: $val" . $eol;
  1012. }
  1013. }
  1014. return $ret;
  1015. }
  1016. /**
  1017. * Sets message Content-Type header.
  1018. * Use it to build messages with various content-types e.g. miltipart/raport
  1019. * not supported by _contentHeaders() function.
  1020. *
  1021. * @param string $type Type name
  1022. * @param array $params Hash array of header parameters
  1023. *
  1024. * @return void
  1025. * @access public
  1026. * @since 1.7.0
  1027. */
  1028. function setContentType($type, $params = array())
  1029. {
  1030. $header = $type;
  1031. $eol = !empty($this->_build_params['eol'])
  1032. ? $this->_build_params['eol'] : "\r\n";
  1033. // add parameters
  1034. $token_regexp = '#([^\x21\x23-\x27\x2A\x2B\x2D'
  1035. . '\x2E\x30-\x39\x41-\x5A\x5E-\x7E])#';
  1036. if (is_array($params)) {
  1037. foreach ($params as $name => $value) {
  1038. if ($name == 'boundary') {
  1039. $this->_build_params['boundary'] = $value;
  1040. }
  1041. if (!preg_match($token_regexp, $value)) {
  1042. $header .= ";$eol $name=$value";
  1043. } else {
  1044. $value = addcslashes($value, '\\"');
  1045. $header .= ";$eol $name=\"$value\"";
  1046. }
  1047. }
  1048. }
  1049. // add required boundary parameter if not defined
  1050. if (preg_match('/^multipart\//i', $type)) {
  1051. if (empty($this->_build_params['boundary'])) {
  1052. $this->_build_params['boundary'] = '=_' . md5(rand() . microtime());
  1053. }
  1054. $header .= ";$eol boundary=\"".$this->_build_params['boundary']."\"";
  1055. }
  1056. $this->_build_params['ctype'] = $header;
  1057. }
  1058. /**
  1059. * Sets the Subject header
  1060. *
  1061. * @param string $subject String to set the subject to.
  1062. *
  1063. * @return void
  1064. * @access public
  1065. */
  1066. function setSubject($subject)
  1067. {
  1068. $this->_headers['Subject'] = $subject;
  1069. }
  1070. /**
  1071. * Set an email to the From (the sender) header
  1072. *
  1073. * @param string $email The email address to use
  1074. *
  1075. * @return void
  1076. * @access public
  1077. */
  1078. function setFrom($email)
  1079. {
  1080. $this->_headers['From'] = $email;
  1081. }
  1082. /**
  1083. * Add an email to the To header
  1084. * (multiple calls to this method are allowed)
  1085. *
  1086. * @param string $email The email direction to add
  1087. *
  1088. * @return void
  1089. * @access public
  1090. */
  1091. function addTo($email)
  1092. {
  1093. if (isset($this->_headers['To'])) {
  1094. $this->_headers['To'] .= ", $email";
  1095. } else {
  1096. $this->_headers['To'] = $email;
  1097. }
  1098. }
  1099. /**
  1100. * Add an email to the Cc (carbon copy) header
  1101. * (multiple calls to this method are allowed)
  1102. *
  1103. * @param string $email The email direction to add
  1104. *
  1105. * @return void
  1106. * @access public
  1107. */
  1108. function addCc($email)
  1109. {
  1110. if (isset($this->_headers['Cc'])) {
  1111. $this->_headers['Cc'] .= ", $email";
  1112. } else {
  1113. $this->_headers['Cc'] = $email;
  1114. }
  1115. }
  1116. /**
  1117. * Add an email to the Bcc (blank carbon copy) header
  1118. * (multiple calls to this method are allowed)
  1119. *
  1120. * @param string $email The email direction to add
  1121. *
  1122. * @return void
  1123. * @access public
  1124. */
  1125. function addBcc($email)
  1126. {
  1127. if (isset($this->_headers['Bcc'])) {
  1128. $this->_headers['Bcc'] .= ", $email";
  1129. } else {
  1130. $this->_headers['Bcc'] = $email;
  1131. }
  1132. }
  1133. /**
  1134. * Since the PHP send function requires you to specify
  1135. * recipients (To: header) separately from the other
  1136. * headers, the To: header is not properly encoded.
  1137. * To fix this, you can use this public method to
  1138. * encode your recipients before sending to the send
  1139. * function
  1140. *
  1141. * @param string $recipients A comma-delimited list of recipients
  1142. *
  1143. * @return string Encoded data
  1144. * @access public
  1145. */
  1146. function encodeRecipients($recipients)
  1147. {
  1148. $input = array("To" => $recipients);
  1149. $retval = $this->_encodeHeaders($input);
  1150. return $retval["To"] ;
  1151. }
  1152. /**
  1153. * Encodes headers as per RFC2047
  1154. *
  1155. * @param array $input The header data to encode
  1156. * @param array $params Extra build parameters
  1157. *
  1158. * @return array Encoded data
  1159. * @access private
  1160. */
  1161. function _encodeHeaders($input, $params = array())
  1162. {
  1163. $build_params = $this->_build_params;
  1164. while (list($key, $value) = each($params)) {
  1165. $build_params[$key] = $value;
  1166. }
  1167. foreach ($input as $hdr_name => $hdr_value) {
  1168. if (is_array($hdr_value)) {
  1169. foreach ($hdr_value as $idx => $value) {
  1170. $input[$hdr_name][$idx] = $this->encodeHeader(
  1171. $hdr_name, $value,
  1172. $build_params['head_charset'], $build_params['head_encoding']
  1173. );
  1174. }
  1175. } else {
  1176. $input[$hdr_name] = $this->encodeHeader(
  1177. $hdr_name, $hdr_value,
  1178. $build_params['head_charset'], $build_params['head_encoding']
  1179. );
  1180. }
  1181. }
  1182. return $input;
  1183. }
  1184. /**
  1185. * Encodes a header as per RFC2047
  1186. *
  1187. * @param string $name The header name
  1188. * @param string $value The header data to encode
  1189. * @param string $charset Character set name
  1190. * @param string $encoding Encoding name (base64 or quoted-printable)
  1191. *
  1192. * @return string Encoded header data (without a name)
  1193. * @access public
  1194. * @since 1.5.3
  1195. */
  1196. function encodeHeader($name, $value, $charset, $encoding)
  1197. {
  1198. $mime_part = new Mail_mimePart;
  1199. return $mime_part->encodeHeader(
  1200. $name, $value, $charset, $encoding, $this->_build_params['eol']
  1201. );
  1202. }
  1203. /**
  1204. * Get file's basename (locale independent)
  1205. *
  1206. * @param string $filename Filename
  1207. *
  1208. * @return string Basename
  1209. * @access private
  1210. */
  1211. function _basename($filename)
  1212. {
  1213. // basename() is not unicode safe and locale dependent
  1214. if (stristr(PHP_OS, 'win') || stristr(PHP_OS, 'netware')) {
  1215. return preg_replace('/^.*[\\\\\\/]/', '', $filename);
  1216. } else {
  1217. return preg_replace('/^.*[\/]/', '', $filename);
  1218. }
  1219. }
  1220. /**
  1221. * Get Content-Type and Content-Transfer-Encoding headers of the message
  1222. *
  1223. * @return array Headers array
  1224. * @access private
  1225. */
  1226. function _contentHeaders()
  1227. {
  1228. $attachments = count($this->_parts) ? true : false;
  1229. $html_images = count($this->_html_images) ? true : false;
  1230. $html = strlen($this->_htmlbody) ? true : false;
  1231. $text = (!$html && strlen($this->_txtbody)) ? true : false;
  1232. $headers = array();
  1233. // See get()
  1234. switch (true) {
  1235. case $text && !$attachments:
  1236. $headers['Content-Type'] = 'text/plain';
  1237. break;
  1238. case !$text && !$html && $attachments:
  1239. case $text && $attachments:
  1240. case $html && $attachments && !$html_images:
  1241. case $html && $attachments && $html_images:
  1242. $headers['Content-Type'] = 'multipart/mixed';
  1243. break;
  1244. case $html && !$attachments && !$html_images && isset($this->_txtbody):
  1245. case $html && !$attachments && $html_images && isset($this->_txtbody):
  1246. $headers['Content-Type'] = 'multipart/alternative';
  1247. break;
  1248. case $html && !$attachments && !$html_images && !isset($this->_txtbody):
  1249. $headers['Content-Type'] = 'text/html';
  1250. break;
  1251. case $html && !$attachments && $html_images && !isset($this->_txtbody):
  1252. $headers['Content-Type'] = 'multipart/related';
  1253. break;
  1254. default:
  1255. return $headers;
  1256. }
  1257. $this->_checkParams();
  1258. $eol = !empty($this->_build_params['eol'])
  1259. ? $this->_build_params['eol'] : "\r\n";
  1260. if ($headers['Content-Type'] == 'text/plain') {
  1261. // single-part message: add charset and encoding
  1262. $charset = 'charset=' . $this->_build_params['text_charset'];
  1263. // place charset parameter in the same line, if possible
  1264. // 26 = strlen("Content-Type: text/plain; ")
  1265. $headers['Content-Type']
  1266. .= (strlen($charset) + 26 <= 76) ? "; $charset" : ";$eol $charset";
  1267. $headers['Content-Transfer-Encoding']
  1268. = $this->_build_params['text_encoding'];
  1269. } else if ($headers['Content-Type'] == 'text/html') {
  1270. // single-part message: add charset and encoding
  1271. $charset = 'charset=' . $this->_build_params['html_charset'];
  1272. // place charset parameter in the same line, if possible
  1273. $headers['Content-Type']
  1274. .= (strlen($charset) + 25 <= 76) ? "; $charset" : ";$eol $charset";
  1275. $headers['Content-Transfer-Encoding']
  1276. = $this->_build_params['html_encoding'];
  1277. } else {
  1278. // multipart message: and boundary
  1279. if (!empty($this->_build_params['boundary'])) {
  1280. $boundary = $this->_build_params['boundary'];
  1281. } else if (!empty($this->_headers['Content-Type'])
  1282. && preg_match('/boundary="([^"]+)"/', $this->_headers['Content-Type'], $m)
  1283. ) {
  1284. $boundary = $m[1];
  1285. } else {
  1286. $boundary = '=_' . md5(rand() . microtime());
  1287. }
  1288. $this->_build_params['boundary'] = $boundary;
  1289. $headers['Content-Type'] .= ";$eol boundary=\"$boundary\"";
  1290. }
  1291. return $headers;
  1292. }
  1293. /**
  1294. * Validate and set build parameters
  1295. *
  1296. * @return void
  1297. * @access private
  1298. */
  1299. function _checkParams()
  1300. {
  1301. $encodings = array('7bit', '8bit', 'base64', 'quoted-printable');
  1302. $this->_build_params['text_encoding']
  1303. = strtolower($this->_build_params['text_encoding']);
  1304. $this->_build_params['html_encoding']
  1305. = strtolower($this->_build_params['html_encoding']);
  1306. if (!in_array($this->_build_params['text_encoding'], $encodings)) {
  1307. $this->_build_params['text_encoding'] = '7bit';
  1308. }
  1309. if (!in_array($this->_build_params['html_encoding'], $encodings)) {
  1310. $this->_build_params['html_encoding'] = '7bit';
  1311. }
  1312. // text body
  1313. if ($this->_build_params['text_encoding'] == '7bit'
  1314. && !preg_match('/ascii/i', $this->_build_params['text_charset'])
  1315. && preg_match('/[^\x00-\x7F]/', $this->_txtbody)
  1316. ) {
  1317. $this->_build_params['text_encoding'] = 'quoted-printable';
  1318. }
  1319. // html body
  1320. if ($this->_build_params['html_encoding'] == '7bit'
  1321. && !preg_match('/ascii/i', $this->_build_params['html_charset'])
  1322. && preg_match('/[^\x00-\x7F]/', $this->_htmlbody)
  1323. ) {
  1324. $this->_build_params['html_encoding'] = 'quoted-printable';
  1325. }
  1326. }
  1327. /**
  1328. * PEAR::isError implementation
  1329. *
  1330. * @param mixed $data Object
  1331. *
  1332. * @return bool True if object is an instance of PEAR_Error
  1333. * @access private
  1334. */
  1335. function _isError($data)
  1336. {
  1337. // PEAR::isError() is not PHP 5.4 compatible (see Bug #19473)
  1338. if (is_object($data) && is_a($data, 'PEAR_Error')) {
  1339. return true;
  1340. }
  1341. return false;
  1342. }
  1343. /**
  1344. * PEAR::raiseError implementation
  1345. *
  1346. * @param $message A text error message
  1347. *
  1348. * @return PEAR_Error Instance of PEAR_Error
  1349. * @access private
  1350. */
  1351. function _raiseError($message)
  1352. {
  1353. // PEAR::raiseError() is not PHP 5.4 compatible
  1354. return new PEAR_Error($message);
  1355. }
  1356. } // End of class