2 /***************************************************************************
5 begin : Sunday Aug. 12, 2001
6 copyright : (C) 2001 The phpBB Group
7 email : support@phpbb.com
9 $Id: emailer.php,v 1.15.2.34 2003/07/26 11:41:35 acydburn Exp $
11 ***************************************************************************/
13 /***************************************************************************
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 ***************************************************************************/
23 // The emailer class has support for attaching files, that isn't implemented
24 // in the 2.0 release but we can probable find some way of using it in a future
29 var $msg, $subject, $extra_headers;
30 var $addresses, $reply_to, $from;
33 var $tpl_msg = array();
35 function emailer($use_smtp)
38 $this->use_smtp = $use_smtp;
39 $this->reply_to = $this->from = '';
42 // Resets all the data (address, template file, etc etc to default
45 $this->addresses = array();
46 $this->vars = $this->msg = $this->extra_headers = '';
49 // Sets an email address to send to
50 function email_address($address)
52 $this->addresses['to'] = trim($address);
57 $this->addresses['cc'][] = trim($address);
60 function bcc($address)
62 $this->addresses['bcc'][] = trim($address);
65 function replyto($address)
67 $this->reply_to = trim($address);
70 function from($address)
72 $this->from = trim($address);
75 // set up subject for mail
76 function set_subject($subject = '')
78 $this->subject = trim(preg_replace('#[\n\r]+#s', '', $subject));
81 // set up extra mail headers
82 function extra_headers($headers)
84 $this->extra_headers .= trim($headers) . "\n";
87 function use_template($template_file, $template_lang = '')
89 global $board_config, $phpbb_root_path;
91 if (trim($template_file) == '')
93 message_die(GENERAL_ERROR, 'No template file set', '', __LINE__, __FILE__);
96 if (trim($template_lang) == '')
98 $template_lang = $board_config['default_lang'];
101 if (empty($this->tpl_msg[$template_lang . $template_file]))
103 $tpl_file = $phpbb_root_path . 'language/lang_' . $template_lang . '/email/' . $template_file . '.tpl';
105 if (!@file_exists(@phpbb_realpath($tpl_file)))
107 $tpl_file = $phpbb_root_path . 'language/lang_' . $board_config['default_lang'] . '/email/' . $template_file . '.tpl';
109 if (!@file_exists(@phpbb_realpath($tpl_file)))
111 message_die(GENERAL_ERROR, 'Could not find email template file :: ' . $template_file, '', __LINE__, __FILE__);
115 if (!($fd = @fopen($tpl_file, 'r')))
117 message_die(GENERAL_ERROR, 'Failed opening template file :: ' . $tpl_file, '', __LINE__, __FILE__);
120 $this->tpl_msg[$template_lang . $template_file] = fread($fd, filesize($tpl_file));
124 $this->msg = $this->tpl_msg[$template_lang . $template_file];
130 function assign_vars($vars)
132 $this->vars = (empty($this->vars)) ? $vars : $this->vars . $vars;
135 // Send the mail out to the recipients set previously in var $this->address
138 global $board_config, $lang, $phpEx, $phpbb_root_path, $db;
140 // Escape all quotes, else the eval will fail.
141 $this->msg = str_replace ("'", "\'", $this->msg);
142 $this->msg = preg_replace('#\{([a-z0-9\-_]*?)\}#is', "' . $\\1 . '", $this->msg);
146 while (list($key, $val) = each($this->vars))
151 eval("\$this->msg = '$this->msg';");
155 while (list($key, $val) = each($this->vars))
160 // We now try and pull a subject from the email body ... if it exists,
161 // do this here because the subject may contain a variable
164 if (preg_match('#^(Subject:(.*?))$#m', $this->msg, $match))
166 $this->subject = (trim($match[2]) != '') ? trim($match[2]) : (($this->subject != '') ? $this->subject : 'No Subject');
167 $drop_header .= '[\r\n]*?' . phpbb_preg_quote($match[1], '#');
171 $this->subject = (($this->subject != '') ? $this->subject : 'No Subject');
174 if (preg_match('#^(Charset:(.*?))$#m', $this->msg, $match))
176 $this->encoding = (trim($match[2]) != '') ? trim($match[2]) : trim($lang['ENCODING']);
177 $drop_header .= '[\r\n]*?' . phpbb_preg_quote($match[1], '#');
181 $this->encoding = trim($lang['ENCODING']);
184 if ($drop_header != '')
186 $this->msg = trim(preg_replace('#' . $drop_header . '#s', '', $this->msg));
189 $to = $this->addresses['to'];
191 $cc = (count($this->addresses['cc'])) ? implode(', ', $this->addresses['cc']) : '';
192 $bcc = (count($this->addresses['bcc'])) ? implode(', ', $this->addresses['bcc']) : '';
195 $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" : '');
197 // Send message ... removed $this->encode() from subject for time being
198 if ( $this->use_smtp )
200 if ( !defined('SMTP_INCLUDED') )
202 include($phpbb_root_path . 'includes/smtp.' . $phpEx);
205 $result = smtpmail($to, $this->subject, $this->msg, $this->extra_headers);
209 $empty_to_header = ($to == '') ? TRUE : FALSE;
210 $to = ($to == '') ? (($board_config['sendmail_fix']) ? ' ' : 'Undisclosed-recipients:;') : $to;
212 $result = @mail($to, $this->subject, preg_replace("#(?<!\r)\n#s", "\n", $this->msg), $this->extra_headers);
214 if (!$result && !$board_config['sendmail_fix'] && $empty_to_header)
218 $sql = "UPDATE " . CONFIG_TABLE . "
219 SET config_value = '1'
220 WHERE config_name = 'sendmail_fix'";
221 if (!$db->sql_query($sql))
223 message_die(GENERAL_ERROR, 'Unable to update config table', '', __LINE__, __FILE__, $sql);
226 $board_config['sendmail_fix'] = 1;
227 $result = @mail($to, $this->subject, preg_replace("#(?<!\r)\n#s", "\n", $this->msg), $this->extra_headers);
234 message_die(GENERAL_ERROR, 'Failed sending email :: ' . (($this->use_smtp) ? 'SMTP' : 'PHP') . ' :: ' . $result, '', __LINE__, __FILE__);
240 // Encodes the given string for proper display for this encoding ... nabbed
241 // from php.net and modified. There is an alternative encoding method which
242 // may produce lesd output but it's questionable as to its worth in this
244 function encode($str)
246 if ($this->encoding == '')
251 // define start delimimter, end delimiter and spacer
253 $start = "=?$this->encoding?B?";
254 $spacer = "$end\r\n $start";
256 // determine length of encoded text within chunks and ensure length is even
257 $length = 75 - strlen($start) - strlen($end);
258 $length = floor($length / 2) * 2;
260 // encode the string and split it into chunks with spacers after each chunk
261 $str = chunk_split(base64_encode($str), $length, $spacer);
263 // remove trailing spacer and add start and end delimiters
264 $str = preg_replace('#' . phpbb_preg_quote($spacer, '#') . '$#', '', $str);
266 return $start . $str . $end;
270 // Attach files via MIME.
272 function attachFile($filename, $mimetype = "application/octet-stream", $szFromAddress, $szFilenameToDisplay)
275 $mime_boundary = "--==================_846811060==_";
277 $this->msg = '--' . $mime_boundary . "\nContent-Type: text/plain;\n\tcharset=\"" . $lang['ENCODING'] . "\"\n\n" . $this->msg;
281 $filename = $mime_filename;
282 $encoded = $this->encode_file($filename);
285 $fd = fopen($filename, "r");
286 $contents = fread($fd, filesize($filename));
288 $this->mimeOut = "--" . $mime_boundary . "\n";
289 $this->mimeOut .= "Content-Type: " . $mimetype . ";\n\tname=\"$szFilenameToDisplay\"\n";
290 $this->mimeOut .= "Content-Transfer-Encoding: quoted-printable\n";
291 $this->mimeOut .= "Content-Disposition: attachment;\n\tfilename=\"$szFilenameToDisplay\"\n\n";
293 if ( $mimetype == "message/rfc822" )
295 $this->mimeOut .= "From: ".$szFromAddress."\n";
296 $this->mimeOut .= "To: ".$this->emailAddress."\n";
297 $this->mimeOut .= "Date: ".date("D, d M Y H:i:s") . " UT\n";
298 $this->mimeOut .= "Reply-To:".$szFromAddress."\n";
299 $this->mimeOut .= "Subject: ".$this->mailSubject."\n";
300 $this->mimeOut .= "X-Mailer: PHP/".phpversion()."\n";
301 $this->mimeOut .= "MIME-Version: 1.0\n";
304 $this->mimeOut .= $contents."\n";
305 $this->mimeOut .= "--" . $mime_boundary . "--" . "\n";
308 // added -- to notify email client attachment is done
311 function getMimeHeaders($filename, $mime_filename="")
313 $mime_boundary = "--==================_846811060==_";
317 $filename = $mime_filename;
320 $out = "MIME-Version: 1.0\n";
321 $out .= "Content-Type: multipart/mixed;\n\tboundary=\"$mime_boundary\"\n\n";
322 $out .= "This message is in MIME format. Since your mail reader does not understand\n";
323 $out .= "this format, some or all of this message may not be legible.";
329 // Split string by RFC 2045 semantics (76 chars per line, end with \r\n).
331 function myChunkSplit($str)
334 $len = strlen($stmp);
341 $out .= substr($stmp, 0, 76) . "\r\n";
342 $stmp = substr($stmp, 76);
347 $out .= $stmp . "\r\n";
356 // Split the specified file up into a string and return it
358 function encode_file($sourcefile)
360 if (is_readable(phpbb_realpath($sourcefile)))
362 $fd = fopen($sourcefile, "r");
363 $contents = fread($fd, filesize($sourcefile));
364 $encoded = $this->myChunkSplit(base64_encode($contents));