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

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