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

/oscommerce-2.2rc2/admin/includes/classes/email.php

http://myopensources.googlecode.com/
PHP | 578 lines | 326 code | 88 blank | 164 comment | 126 complexity | 7ae45c368275f6d9fcfea9d845385afe MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /*
  3. $Id: email.php 1739 2007-12-20 00:52:16Z hpdl $
  4. osCommerce, Open Source E-Commerce Solutions
  5. http://www.oscommerce.com
  6. Copyright (c) 2003 osCommerce
  7. Released under the GNU General Public License
  8. mail.php - a class to assist in building mime-HTML eMails
  9. The original class was made by Richard Heyes <richard@phpguru.org>
  10. and can be found here: http://www.phpguru.org
  11. Renamed and Modified by Jan Wildeboer for osCommerce
  12. */
  13. class email {
  14. var $html;
  15. var $text;
  16. var $output;
  17. var $html_text;
  18. var $html_images;
  19. var $image_types;
  20. var $build_params;
  21. var $attachments;
  22. var $headers;
  23. function email($headers = '') {
  24. if ($headers == '') $headers = array();
  25. $this->html_images = array();
  26. $this->headers = array();
  27. if (EMAIL_LINEFEED == 'CRLF') {
  28. $this->lf = "\r\n";
  29. } else {
  30. $this->lf = "\n";
  31. }
  32. /**
  33. * If you want the auto load functionality
  34. * to find other mime-image/file types, add the
  35. * extension and content type here.
  36. */
  37. $this->image_types = array('gif' => 'image/gif',
  38. 'jpg' => 'image/jpeg',
  39. 'jpeg' => 'image/jpeg',
  40. 'jpe' => 'image/jpeg',
  41. 'bmp' => 'image/bmp',
  42. 'png' => 'image/png',
  43. 'tif' => 'image/tiff',
  44. 'tiff' => 'image/tiff',
  45. 'swf' => 'application/x-shockwave-flash');
  46. $this->build_params['html_encoding'] = 'quoted-printable';
  47. $this->build_params['text_encoding'] = '7bit';
  48. $this->build_params['html_charset'] = constant('CHARSET');
  49. $this->build_params['text_charset'] = constant('CHARSET');
  50. $this->build_params['text_wrap'] = 998;
  51. /**
  52. * Make sure the MIME version header is first.
  53. */
  54. $this->headers[] = 'MIME-Version: 1.0';
  55. reset($headers);
  56. while (list(,$value) = each($headers)) {
  57. if (tep_not_null($value)) {
  58. $this->headers[] = $value;
  59. }
  60. }
  61. }
  62. /**
  63. * This function will read a file in
  64. * from a supplied filename and return
  65. * it. This can then be given as the first
  66. * argument of the the functions
  67. * add_html_image() or add_attachment().
  68. */
  69. function get_file($filename) {
  70. $return = '';
  71. if ($fp = fopen($filename, 'rb')) {
  72. while (!feof($fp)) {
  73. $return .= fread($fp, 1024);
  74. }
  75. fclose($fp);
  76. return $return;
  77. } else {
  78. return false;
  79. }
  80. }
  81. /**
  82. * Function for extracting images from
  83. * html source. This function will look
  84. * through the html code supplied by add_html()
  85. * and find any file that ends in one of the
  86. * extensions defined in $obj->image_types.
  87. * If the file exists it will read it in and
  88. * embed it, (not an attachment).
  89. *
  90. * Function contributed by Dan Allen
  91. */
  92. function find_html_images($images_dir) {
  93. // Build the list of image extensions
  94. while (list($key, ) = each($this->image_types)) {
  95. $extensions[] = $key;
  96. }
  97. preg_match_all('/"([^"]+\.(' . implode('|', $extensions).'))"/Ui', $this->html, $images);
  98. for ($i=0; $i<count($images[1]); $i++) {
  99. if (file_exists($images_dir . $images[1][$i])) {
  100. $html_images[] = $images[1][$i];
  101. $this->html = str_replace($images[1][$i], basename($images[1][$i]), $this->html);
  102. }
  103. }
  104. if (tep_not_null($html_images)) {
  105. // If duplicate images are embedded, they may show up as attachments, so remove them.
  106. $html_images = array_unique($html_images);
  107. sort($html_images);
  108. for ($i=0; $i<count($html_images); $i++) {
  109. if ($image = $this->get_file($images_dir . $html_images[$i])) {
  110. $content_type = $this->image_types[substr($html_images[$i], strrpos($html_images[$i], '.') + 1)];
  111. $this->add_html_image($image, basename($html_images[$i]), $content_type);
  112. }
  113. }
  114. }
  115. }
  116. /**
  117. * Adds plain text. Use this function
  118. * when NOT sending html email
  119. */
  120. function add_text($text = '') {
  121. $this->text = tep_convert_linefeeds(array("\r\n", "\n", "\r"), $this->lf, $text);
  122. }
  123. /**
  124. * Adds a html part to the mail.
  125. * Also replaces image names with
  126. * content-id's.
  127. */
  128. function add_html($html, $text = NULL, $images_dir = NULL) {
  129. $this->html = tep_convert_linefeeds(array("\r\n", "\n", "\r"), '<br>', $html);
  130. $this->html_text = tep_convert_linefeeds(array("\r\n", "\n", "\r"), $this->lf, $text);
  131. if (isset($images_dir)) $this->find_html_images($images_dir);
  132. }
  133. /**
  134. * Adds an image to the list of embedded
  135. * images.
  136. */
  137. function add_html_image($file, $name = '', $c_type='application/octet-stream') {
  138. $this->html_images[] = array('body' => $file,
  139. 'name' => $name,
  140. 'c_type' => $c_type,
  141. 'cid' => md5(uniqid(time())));
  142. }
  143. /**
  144. * Adds a file to the list of attachments.
  145. */
  146. function add_attachment($file, $name = '', $c_type='application/octet-stream', $encoding = 'base64') {
  147. $this->attachments[] = array('body' => $file,
  148. 'name' => $name,
  149. 'c_type' => $c_type,
  150. 'encoding' => $encoding);
  151. }
  152. /**
  153. * Adds a text subpart to a mime_part object
  154. */
  155. /* HPDL PHP3 */
  156. // function &add_text_part(&$obj, $text) {
  157. function add_text_part(&$obj, $text) {
  158. $params['content_type'] = 'text/plain';
  159. $params['encoding'] = $this->build_params['text_encoding'];
  160. $params['charset'] = $this->build_params['text_charset'];
  161. if (is_object($obj)) {
  162. return $obj->addSubpart($text, $params);
  163. } else {
  164. return new mime($text, $params);
  165. }
  166. }
  167. /**
  168. * Adds a html subpart to a mime_part object
  169. */
  170. /* HPDL PHP3 */
  171. // function &add_html_part(&$obj) {
  172. function add_html_part(&$obj) {
  173. $params['content_type'] = 'text/html';
  174. $params['encoding'] = $this->build_params['html_encoding'];
  175. $params['charset'] = $this->build_params['html_charset'];
  176. if (is_object($obj)) {
  177. return $obj->addSubpart($this->html, $params);
  178. } else {
  179. return new mime($this->html, $params);
  180. }
  181. }
  182. /**
  183. * Starts a message with a mixed part
  184. */
  185. /* HPDL PHP3 */
  186. // function &add_mixed_part() {
  187. function add_mixed_part() {
  188. $params['content_type'] = 'multipart/mixed';
  189. return new mime('', $params);
  190. }
  191. /**
  192. * Adds an alternative part to a mime_part object
  193. */
  194. /* HPDL PHP3 */
  195. // function &add_alternative_part(&$obj) {
  196. function add_alternative_part(&$obj) {
  197. $params['content_type'] = 'multipart/alternative';
  198. if (is_object($obj)) {
  199. return $obj->addSubpart('', $params);
  200. } else {
  201. return new mime('', $params);
  202. }
  203. }
  204. /**
  205. * Adds a html subpart to a mime_part object
  206. */
  207. /* HPDL PHP3 */
  208. // function &add_related_part(&$obj) {
  209. function add_related_part(&$obj) {
  210. $params['content_type'] = 'multipart/related';
  211. if (is_object($obj)) {
  212. return $obj->addSubpart('', $params);
  213. } else {
  214. return new mime('', $params);
  215. }
  216. }
  217. /**
  218. * Adds an html image subpart to a mime_part object
  219. */
  220. /* HPDL PHP3 */
  221. // function &add_html_image_part(&$obj, $value) {
  222. function add_html_image_part(&$obj, $value) {
  223. $params['content_type'] = $value['c_type'];
  224. $params['encoding'] = 'base64';
  225. $params['disposition'] = 'inline';
  226. $params['dfilename'] = $value['name'];
  227. $params['cid'] = $value['cid'];
  228. $obj->addSubpart($value['body'], $params);
  229. }
  230. /**
  231. * Adds an attachment subpart to a mime_part object
  232. */
  233. /* HPDL PHP3 */
  234. // function &add_attachment_part(&$obj, $value) {
  235. function add_attachment_part(&$obj, $value) {
  236. $params['content_type'] = $value['c_type'];
  237. $params['encoding'] = $value['encoding'];
  238. $params['disposition'] = 'attachment';
  239. $params['dfilename'] = $value['name'];
  240. $obj->addSubpart($value['body'], $params);
  241. }
  242. /**
  243. * Builds the multipart message from the
  244. * list ($this->_parts). $params is an
  245. * array of parameters that shape the building
  246. * of the message. Currently supported are:
  247. *
  248. * $params['html_encoding'] - The type of encoding to use on html. Valid options are
  249. * "7bit", "quoted-printable" or "base64" (all without quotes).
  250. * 7bit is EXPRESSLY NOT RECOMMENDED. Default is quoted-printable
  251. * $params['text_encoding'] - The type of encoding to use on plain text Valid options are
  252. * "7bit", "quoted-printable" or "base64" (all without quotes).
  253. * Default is 7bit
  254. * $params['text_wrap'] - The character count at which to wrap 7bit encoded data.
  255. * Default this is 998.
  256. * $params['html_charset'] - The character set to use for a html section.
  257. * Default is iso-8859-1
  258. * $params['text_charset'] - The character set to use for a text section.
  259. * - Default is iso-8859-1
  260. */
  261. /* HPDL PHP3 */
  262. // function build_message($params = array()) {
  263. function build_message($params = '') {
  264. if ($params == '') $params = array();
  265. if (count($params) > 0) {
  266. reset($params);
  267. while(list($key, $value) = each($params)) {
  268. $this->build_params[$key] = $value;
  269. }
  270. }
  271. if (tep_not_null($this->html_images)) {
  272. reset($this->html_images);
  273. while (list(,$value) = each($this->html_images)) {
  274. $this->html = str_replace($value['name'], 'cid:' . $value['cid'], $this->html);
  275. }
  276. }
  277. $null = NULL;
  278. $attachments = ((tep_not_null($this->attachments)) ? true : false);
  279. $html_images = ((tep_not_null($this->html_images)) ? true : false);
  280. $html = ((tep_not_null($this->html)) ? true : false);
  281. $text = ((tep_not_null($this->text)) ? true : false);
  282. switch (true) {
  283. case (($text == true) && ($attachments == false)):
  284. /* HPDL PHP3 */
  285. // $message =& $this->add_text_part($null, $this->text);
  286. $message = $this->add_text_part($null, $this->text);
  287. break;
  288. case (($text == false) && ($attachments == true) && ($html == false)):
  289. /* HPDL PHP3 */
  290. // $message =& $this->add_mixed_part();
  291. $message = $this->add_mixed_part();
  292. for ($i=0; $i<count($this->attachments); $i++) {
  293. $this->add_attachment_part($message, $this->attachments[$i]);
  294. }
  295. break;
  296. case (($text == true) && ($attachments == true)):
  297. /* HPDL PHP3 */
  298. // $message =& $this->add_mixed_part();
  299. $message = $this->add_mixed_part();
  300. $this->add_text_part($message, $this->text);
  301. for ($i=0; $i<count($this->attachments); $i++) {
  302. $this->add_attachment_part($message, $this->attachments[$i]);
  303. }
  304. break;
  305. case (($html == true) && ($attachments == false) && ($html_images == false)):
  306. if (tep_not_null($this->html_text)) {
  307. /* HPDL PHP3 */
  308. // $message =& $this->add_alternative_part($null);
  309. $message = $this->add_alternative_part($null);
  310. $this->add_text_part($message, $this->html_text);
  311. $this->add_html_part($message);
  312. } else {
  313. /* HPDL PHP3 */
  314. // $message =& $this->add_html_part($null);
  315. $message = $this->add_html_part($null);
  316. }
  317. break;
  318. case (($html == true) && ($attachments == false) && ($html_images == true)):
  319. if (tep_not_null($this->html_text)) {
  320. /* HPDL PHP3 */
  321. // $message =& $this->add_alternative_part($null);
  322. $message = $this->add_alternative_part($null);
  323. $this->add_text_part($message, $this->html_text);
  324. /* HPDL PHP3 */
  325. // $related =& $this->add_related_part($message);
  326. $related = $this->add_related_part($message);
  327. } else {
  328. /* HPDL PHP3 */
  329. // $message =& $this->add_related_part($null);
  330. // $related =& $message;
  331. $message = $this->add_related_part($null);
  332. $related = $message;
  333. }
  334. $this->add_html_part($related);
  335. for ($i=0; $i<count($this->html_images); $i++) {
  336. $this->add_html_image_part($related, $this->html_images[$i]);
  337. }
  338. break;
  339. case (($html == true) && ($attachments == true) && ($html_images == false)):
  340. /* HPDL PHP3 */
  341. // $message =& $this->add_mixed_part();
  342. $message = $this->add_mixed_part();
  343. if (tep_not_null($this->html_text)) {
  344. /* HPDL PHP3 */
  345. // $alt =& $this->add_alternative_part($message);
  346. $alt = $this->add_alternative_part($message);
  347. $this->add_text_part($alt, $this->html_text);
  348. $this->add_html_part($alt);
  349. } else {
  350. $this->add_html_part($message);
  351. }
  352. for ($i=0; $i<count($this->attachments); $i++) {
  353. $this->add_attachment_part($message, $this->attachments[$i]);
  354. }
  355. break;
  356. case (($html == true) && ($attachments == true) && ($html_images == true)):
  357. /* HPDL PHP3 */
  358. // $message =& $this->add_mixed_part();
  359. $message = $this->add_mixed_part();
  360. if (tep_not_null($this->html_text)) {
  361. /* HPDL PHP3 */
  362. // $alt =& $this->add_alternative_part($message);
  363. $alt = $this->add_alternative_part($message);
  364. $this->add_text_part($alt, $this->html_text);
  365. /* HPDL PHP3 */
  366. // $rel =& $this->add_related_part($alt);
  367. $rel = $this->add_related_part($alt);
  368. } else {
  369. /* HPDL PHP3 */
  370. // $rel =& $this->add_related_part($message);
  371. $rel = $this->add_related_part($message);
  372. }
  373. $this->add_html_part($rel);
  374. for ($i=0; $i<count($this->html_images); $i++) {
  375. $this->add_html_image_part($rel, $this->html_images[$i]);
  376. }
  377. for ($i=0; $i<count($this->attachments); $i++) {
  378. $this->add_attachment_part($message, $this->attachments[$i]);
  379. }
  380. break;
  381. }
  382. if ( (isset($message)) && (is_object($message)) ) {
  383. $output = $message->encode();
  384. $this->output = $output['body'];
  385. reset($output['headers']);
  386. while (list($key, $value) = each($output['headers'])) {
  387. $headers[] = $key . ': ' . $value;
  388. }
  389. $this->headers = array_merge($this->headers, $headers);
  390. return true;
  391. } else {
  392. return false;
  393. }
  394. }
  395. /**
  396. * Sends the mail.
  397. */
  398. function send($to_name, $to_addr, $from_name, $from_addr, $subject = '', $headers = '') {
  399. if ((strstr($to_name, "\n") != false) || (strstr($to_name, "\r") != false)) {
  400. return false;
  401. }
  402. if ((strstr($to_addr, "\n") != false) || (strstr($to_addr, "\r") != false)) {
  403. return false;
  404. }
  405. if ((strstr($subject, "\n") != false) || (strstr($subject, "\r") != false)) {
  406. return false;
  407. }
  408. if ((strstr($from_name, "\n") != false) || (strstr($from_name, "\r") != false)) {
  409. return false;
  410. }
  411. if ((strstr($from_addr, "\n") != false) || (strstr($from_addr, "\r") != false)) {
  412. return false;
  413. }
  414. $to = (($to_name != '') ? '"' . $to_name . '" <' . $to_addr . '>' : $to_addr);
  415. $from = (($from_name != '') ? '"' . $from_name . '" <' . $from_addr . '>' : $from_addr);
  416. if (is_string($headers)) {
  417. $headers = explode($this->lf, trim($headers));
  418. }
  419. for ($i=0; $i<count($headers); $i++) {
  420. if (is_array($headers[$i])) {
  421. for ($j=0; $j<count($headers[$i]); $j++) {
  422. if ($headers[$i][$j] != '') {
  423. $xtra_headers[] = $headers[$i][$j];
  424. }
  425. }
  426. }
  427. if ($headers[$i] != '') {
  428. $xtra_headers[] = $headers[$i];
  429. }
  430. }
  431. if (!isset($xtra_headers)) {
  432. $xtra_headers = array();
  433. }
  434. if (EMAIL_TRANSPORT == 'smtp') {
  435. return mail($to_addr, $subject, $this->output, 'From: ' . $from . $this->lf . 'To: ' . $to . $this->lf . implode($this->lf, $this->headers) . $this->lf . implode($this->lf, $xtra_headers));
  436. } else {
  437. return mail($to, $subject, $this->output, 'From: '.$from.$this->lf.implode($this->lf, $this->headers).$this->lf.implode($this->lf, $xtra_headers));
  438. }
  439. }
  440. /**
  441. * Use this method to return the email
  442. * in message/rfc822 format. Useful for
  443. * adding an email to another email as
  444. * an attachment. there's a commented
  445. * out example in example.php.
  446. *
  447. * string get_rfc822(string To name,
  448. * string To email,
  449. * string From name,
  450. * string From email,
  451. * [string Subject,
  452. * string Extra headers])
  453. */
  454. function get_rfc822($to_name, $to_addr, $from_name, $from_addr, $subject = '', $headers = '') {
  455. // Make up the date header as according to RFC822
  456. $date = 'Date: ' . date('D, d M y H:i:s');
  457. $to = (($to_name != '') ? 'To: "' . $to_name . '" <' . $to_addr . '>' : 'To: ' . $to_addr);
  458. $from = (($from_name != '') ? 'From: "' . $from_name . '" <' . $from_addr . '>' : 'From: ' . $from_addr);
  459. if (is_string($subject)) {
  460. $subject = 'Subject: ' . $subject;
  461. }
  462. if (is_string($headers)) {
  463. $headers = explode($this->lf, trim($headers));
  464. }
  465. for ($i=0; $i<count($headers); $i++) {
  466. if (is_array($headers[$i])) {
  467. for ($j=0; $j<count($headers[$i]); $j++) {
  468. if ($headers[$i][$j] != '') {
  469. $xtra_headers[] = $headers[$i][$j];
  470. }
  471. }
  472. }
  473. if ($headers[$i] != '') {
  474. $xtra_headers[] = $headers[$i];
  475. }
  476. }
  477. if (!isset($xtra_headers)) {
  478. $xtra_headers = array();
  479. }
  480. $headers = array_merge($this->headers, $xtra_headers);
  481. return $date . $this->lf . $from . $this->lf . $to . $this->lf . $subject . $this->lf . implode($this->lf, $headers) . $this->lf . $this->lf . $this->output;
  482. }
  483. }
  484. ?>