You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mime.php 55KB

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