Просмотр файла includes/emailer.php

Размер файла: 8.41Kb
  1. <?php
  2. /***************************************************************************
  3. * mides.ru
  4. * -------------------
  5. ***************************************************************************/
  6. class emailer
  7. {
  8. var $msg, $subject, $extra_headers;
  9. var $addresses, $reply_to, $from;
  10. var $use_smtp;
  11.  
  12. var $tpl_msg = array();
  13.  
  14. function emailer($use_smtp)
  15. {
  16. $this->reset();
  17. $this->use_smtp = $use_smtp;
  18. $this->reply_to = $this->from = '';
  19. }
  20.  
  21. function reset()
  22. {
  23. $this->addresses = array();
  24. $this->vars = $this->msg = $this->extra_headers = '';
  25. }
  26.  
  27. function email_address($address)
  28. {
  29. $this->addresses['to'] = trim($address);
  30. }
  31.  
  32. function cc($address)
  33. {
  34. $this->addresses['cc'][] = trim($address);
  35. }
  36.  
  37. function bcc($address)
  38. {
  39. $this->addresses['bcc'][] = trim($address);
  40. }
  41.  
  42. function replyto($address)
  43. {
  44. $this->reply_to = trim($address);
  45. }
  46.  
  47. function from($address)
  48. {
  49. $this->from = trim($address);
  50. }
  51.  
  52. function set_subject($subject = '')
  53. {
  54. $subject = u2w($subject);
  55. $this->subject = trim(preg_replace('#[\n\r]+#s', '', $subject));
  56. }
  57.  
  58. function extra_headers($headers)
  59. {
  60. $this->extra_headers .= trim($headers) . "\n";
  61. }
  62.  
  63. function use_template($template_file, $template_lang = '')
  64. {
  65. global $board_config, $phpbb_root_path;
  66.  
  67. if (trim($template_file) == '')
  68. {
  69. message_die(GENERAL_ERROR, 'No template file set', '', __LINE__, __FILE__);
  70. }
  71.  
  72. if (trim($template_lang) == '')
  73. {
  74. $template_lang = $board_config['default_lang'];
  75. }
  76.  
  77. if (empty($this->tpl_msg[$template_lang . $template_file]))
  78. {
  79. $tpl_file = $phpbb_root_path . 'templates/email/' . $template_file . '.tpl';
  80.  
  81. if (!@file_exists(@phpbb_realpath($tpl_file)))
  82. {
  83. $tpl_file = $phpbb_root_path . 'templates/email/' . $template_file . '.tpl';
  84.  
  85. if (!@file_exists(@phpbb_realpath($tpl_file)))
  86. {
  87. message_die(GENERAL_ERROR, 'Could not find email template file :: ' . $template_file, '', __LINE__, __FILE__);
  88. }
  89. }
  90.  
  91. if (!($fd = @fopen($tpl_file, 'r')))
  92. {
  93. message_die(GENERAL_ERROR, 'Failed opening template file :: ' . $tpl_file, '', __LINE__, __FILE__);
  94. }
  95.  
  96. $this->tpl_msg[$template_lang . $template_file] = fread($fd, filesize($tpl_file));
  97. fclose($fd);
  98. }
  99.  
  100. $this->msg = $this->tpl_msg[$template_lang . $template_file];
  101.  
  102. return true;
  103. }
  104.  
  105. function assign_vars($vars)
  106. {
  107. $this->vars = (empty($this->vars)) ? $vars : $this->vars . $vars;
  108. }
  109.  
  110. function send()
  111. {
  112. global $board_config, $lang, $phpEx, $phpbb_root_path, $db;
  113.  
  114. $this->msg = str_replace ("'", "\'", $this->msg);
  115. $this->msg = preg_replace('#\{([a-z0-9\-_]*?)\}#is', "' . $\\1 . '", $this->msg);
  116.  
  117. reset ($this->vars);
  118. while (list($key, $val) = each($this->vars))
  119. {
  120. $$key = u2w($val);
  121. }
  122.  
  123. eval("\$this->msg = '$this->msg';");
  124.  
  125. reset ($this->vars);
  126. while (list($key, $val) = each($this->vars))
  127. {
  128. unset($$key);
  129. }
  130.  
  131. $drop_header = '';
  132. $match = array();
  133. if (preg_match('#^(Subject:(.*?))$#m', $this->msg, $match))
  134. {
  135. $this->subject = (trim($match[2]) != '') ? trim($match[2]) : (($this->subject != '') ? $this->subject : 'No Subject');
  136. $drop_header .= '[\r\n]*?' . preg_quote($match[1], '#');
  137. }
  138. else
  139. {
  140. $this->subject = (($this->subject != '') ? $this->subject : 'No Subject');
  141. }
  142.  
  143. if (preg_match('#^(Charset:(.*?))$#m', $this->msg, $match))
  144. {
  145. $this->encoding = (trim($match[2]) != '') ? trim($match[2]) : trim($lang['ENCODING']);
  146. $drop_header .= '[\r\n]*?' . preg_quote($match[1], '#');
  147. }
  148. else
  149. {
  150. $this->encoding = trim($lang['ENCODING']);
  151. }
  152.  
  153. if ($drop_header != '')
  154. {
  155. $this->msg = trim(preg_replace('#' . $drop_header . '#s', '', $this->msg));
  156. }
  157.  
  158. $to = $this->addresses['to'];
  159.  
  160. $cc = (count($this->addresses['cc'])) ? implode(', ', $this->addresses['cc']) : '';
  161. $bcc = (count($this->addresses['bcc'])) ? implode(', ', $this->addresses['bcc']) : '';
  162.  
  163. $this->extra_headers = (($this->reply_to != '') ? "Reply-to: $this->reply_to\n" : '') . (($this->from != '') ? "From: $this->from\n" : "From: " . $board_config['board_email'] . "\n") . "Return-Path: " . $board_config['board_email'] . "\nMessage-ID: <" . md5(uniqid(time())) . "@" . $board_config['server_name'] . ">\nMIME-Version: 1.0\nContent-type: text/plain; charset=" . $this->encoding . "\nContent-transfer-encoding: 8bit\nDate: " . date('r', time()) . "\nX-Priority: 3\nX-MSMail-Priority: Normal\nX-Mailer: PHP\nX-MimeOLE: Produced By phpBB2\n" . $this->extra_headers . (($cc != '') ? "Cc: $cc\n" : '') . (($bcc != '') ? "Bcc: $bcc\n" : '');
  164.  
  165. if ( $this->use_smtp )
  166. {
  167. if ( !defined('SMTP_INCLUDED') )
  168. {
  169. include($phpbb_root_path . 'includes/smtp.' . $phpEx);
  170. }
  171.  
  172. $result = smtpmail($to, $this->subject, $this->msg, $this->extra_headers);
  173. }
  174. else
  175. {
  176. $empty_to_header = ($to == '') ? TRUE : FALSE;
  177. $to = ($to == '') ? (($board_config['sendmail_fix']) ? ' ' : 'Undisclosed-recipients:;') : $to;
  178. $result = @mail($to, $this->subject, preg_replace("#(?<!\r)\n#s", "\n", $this->msg), $this->extra_headers);
  179. if (!$result && !$board_config['sendmail_fix'] && $empty_to_header)
  180. {
  181. $to = ' ';
  182.  
  183. $sql = "UPDATE " . CONFIG_TABLE . "
  184. SET config_value = '1'
  185. WHERE config_name = 'sendmail_fix'";
  186. if (!$db->sql_query($sql))
  187. {
  188. message_die(GENERAL_ERROR, 'Unable to update config table', '', __LINE__, __FILE__, $sql);
  189. }
  190.  
  191. $board_config['sendmail_fix'] = 1;
  192. $result = @mail($to, $this->subject, preg_replace("#(?<!\r)\n#s", "\n", $this->msg), $this->extra_headers);
  193. }
  194. }
  195.  
  196. if (!$result)
  197. {
  198. message_die(GENERAL_ERROR, 'Failed sending email :: ' . (($this->use_smtp) ? 'SMTP' : 'PHP') . ' :: ' . $result, '', __LINE__, __FILE__);
  199. }
  200.  
  201. return true;
  202. }
  203.  
  204. function encode($str)
  205. {
  206. if ($this->encoding == '')
  207. {
  208. return $str;
  209. }
  210.  
  211. $end = "?=";
  212. $start = "=?$this->encoding?B?";
  213. $spacer = "$end\r\n $start";
  214. $length = 75 - strlen($start) - strlen($end);
  215. $length = floor($length / 2) * 2;
  216. $str = chunk_split(base64_encode($str), $length, $spacer);
  217. $str = preg_replace('#' . preg_quote($spacer, '#') . '$#', '', $str);
  218.  
  219. return $start . $str . $end;
  220. }
  221.  
  222. function attachFile($filename, $mimetype = "application/octet-stream", $szFromAddress, $szFilenameToDisplay)
  223. {
  224. global $lang;
  225. $mime_boundary = "--==================_846811060==_";
  226.  
  227. $this->msg = '--' . $mime_boundary . "\nContent-Type: text/plain;\n\tcharset=\"" . $lang['ENCODING'] . "\"\n\n" . $this->msg;
  228.  
  229. if ($mime_filename)
  230. {
  231. $filename = $mime_filename;
  232. $encoded = $this->encode_file($filename);
  233. }
  234.  
  235. $fd = fopen($filename, "r");
  236. $contents = fread($fd, filesize($filename));
  237.  
  238. $this->mimeOut = "--" . $mime_boundary . "\n";
  239. $this->mimeOut .= "Content-Type: " . $mimetype . ";\n\tname=\"$szFilenameToDisplay\"\n";
  240. $this->mimeOut .= "Content-Transfer-Encoding: quoted-printable\n";
  241. $this->mimeOut .= "Content-Disposition: attachment;\n\tfilename=\"$szFilenameToDisplay\"\n\n";
  242.  
  243. if ( $mimetype == "message/rfc822" )
  244. {
  245. $this->mimeOut .= "From: ".$szFromAddress."\n";
  246. $this->mimeOut .= "To: ".$this->emailAddress."\n";
  247. $this->mimeOut .= "Date: ".date("D, d M Y H:i:s") . " UT\n";
  248. $this->mimeOut .= "Reply-To:".$szFromAddress."\n";
  249. $this->mimeOut .= "Subject: ".$this->mailSubject."\n";
  250. $this->mimeOut .= "X-Mailer: PHP/".phpversion()."\n";
  251. $this->mimeOut .= "MIME-Version: 1.0\n";
  252. }
  253.  
  254. $this->mimeOut .= $contents."\n";
  255. $this->mimeOut .= "--" . $mime_boundary . "--" . "\n";
  256.  
  257. return $out;
  258. }
  259.  
  260. function getMimeHeaders($filename, $mime_filename="")
  261. {
  262. $mime_boundary = "--==================_846811060==_";
  263.  
  264. if ($mime_filename)
  265. {
  266. $filename = $mime_filename;
  267. }
  268.  
  269. $out = "MIME-Version: 1.0\n";
  270. $out .= "Content-Type: multipart/mixed;\n\tboundary=\"$mime_boundary\"\n\n";
  271. $out .= "This message is in MIME format. Since your mail reader does not understand\n";
  272. $out .= "this format, some or all of this message may not be legible.";
  273.  
  274. return $out;
  275. }
  276.  
  277. function myChunkSplit($str)
  278. {
  279. $stmp = $str;
  280. $len = strlen($stmp);
  281. $out = "";
  282.  
  283. while ($len > 0)
  284. {
  285. if ($len >= 76)
  286. {
  287. $out .= substr($stmp, 0, 76) . "\r\n";
  288. $stmp = substr($stmp, 76);
  289. $len = $len - 76;
  290. }
  291. else
  292. {
  293. $out .= $stmp . "\r\n";
  294. $stmp = "";
  295. $len = 0;
  296. }
  297. }
  298. return $out;
  299. }
  300.  
  301. function encode_file($sourcefile)
  302. {
  303. if (is_readable(phpbb_realpath($sourcefile)))
  304. {
  305. $fd = fopen($sourcefile, "r");
  306. $contents = fread($fd, filesize($sourcefile));
  307. $encoded = $this->myChunkSplit(base64_encode($contents));
  308. fclose($fd);
  309. }
  310.  
  311. return $encoded;
  312. }
  313.  
  314. }
  315.  
  316. ?>