1 rizwank 1.1 <?php
2 /***************************************************************************
3 emailer.php
4 -------------------
5 begin : Sunday Aug. 12, 2001
6 copyright : (C) 2001 The phpBB Group
7 email : support@phpbb.com
8
9 $Id: emailer.php,v 1.15.2.21 2003/01/15 13:31:53 psotfx Exp $
10
11 ***************************************************************************/
12
13 /***************************************************************************
14 *
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.
19 *
20 ***************************************************************************/
21
22 rizwank 1.1 //
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
25 // release
26 //
27 class emailer
28 {
29 var $tpl_file;
30 var $use_smtp;
31 var $msg;
32 var $mimeOut;
33 var $arrPlaceHolders = array(); // an associative array that has the key = placeHolderName and val = placeHolderValue.
34 var $subject, $extra_headers, $address;
35
36 function emailer($use_smtp)
37 {
38 $this->use_smtp = $use_smtp;
39 $this->tpl_file = NULL;
40 $this->address = NULL;
41 $this->msg = '';
42 $this->mimeOut = '';
43 rizwank 1.1 }
44
45 //
46 // Resets all the data (address, template file, etc etc to default
47 //
48 function reset()
49 {
50 $this->tpl_file = '';
51 $this->address = '';
52 $this->msg = '';
53 $this->memOut = '';
54 $this->vars = '';
55 }
56
57 //
58 // Sets an email address to send to
59 //
60 function email_address($address)
61 {
62 $this->address = '';
63 $this->address .= $address;
64 rizwank 1.1 }
65
66 //
67 // set up subject for mail
68 //
69 function set_subject($subject = '')
70 {
71 $this->subject = trim(preg_replace('#[\n\r]+#s', '', $subject));
72 }
73
74 //
75 // set up extra mail headers
76 //
77 function extra_headers($headers)
78 {
79 $this->extra_headers = $headers;
80 }
81
82 function use_template($template_file, $template_lang = '')
83 {
84 global $board_config, $phpbb_root_path;
85 rizwank 1.1
86 if ( $template_lang == '' )
87 {
88 $template_lang = $board_config['default_lang'];
89 }
90
91 $this->tpl_file = @phpbb_realpath($phpbb_root_path . 'language/lang_' . $template_lang . '/email/' . $template_file . '.tpl');
92
93 if ( !file_exists(phpbb_realpath($this->tpl_file)) )
94 {
95 $this->tpl_file = @phpbb_realpath($phpbb_root_path . 'language/lang_' . $board_config['default_lang'] . '/email/' . $template_file . '.tpl');
96
97 if ( !file_exists(phpbb_realpath($this->tpl_file)) )
98 {
99 message_die(GENERAL_ERROR, 'Could not find email template file ' . $template_file, '', __LINE__, __FILE__);
100 }
101 }
102
103 if ( !$this->load_msg() )
104 {
105 message_die(GENERAL_ERROR, 'Could not load email template file ' . $template_file, '', __LINE__, __FILE__);
106 rizwank 1.1 }
107
108 return true;
109 }
110
111 //
112 // Open the template file and read in the message
113 //
114 function load_msg()
115 {
116 if ( $this->tpl_file == NULL )
117 {
118 message_die(GENERAL_ERROR, 'No template file set', '', __LINE__, __FILE__);
119 }
120
121 if ( !($fd = fopen($this->tpl_file, 'r')) )
122 {
123 message_die(GENERAL_ERROR, 'Failed opening template file', '', __LINE__, __FILE__);
124 }
125
126 $this->msg .= fread($fd, filesize($this->tpl_file));
127 rizwank 1.1 fclose($fd);
128
129 return true;
130 }
131
132 function assign_vars($vars)
133 {
134 $this->vars = ( empty($this->vars) ) ? $vars : $this->vars . $vars;
135 }
136
137 function parse_email()
138 {
139 global $lang;
140 @reset($this->vars);
141 while (list($key, $val) = @each($this->vars))
142 {
143 $$key = $val;
144 }
145
146 // Escape all quotes, else the eval will fail.
147 $this->msg = str_replace ("'", "\'", $this->msg);
148 rizwank 1.1 $this->msg = preg_replace('#\{([a-z0-9\-_]*?)\}#is', "' . $\\1 . '", $this->msg);
149
150 eval("\$this->msg = '$this->msg';");
151
152 //
153 // We now try and pull a subject from the email body ... if it exists,
154 // do this here because the subject may contain a variable
155 //
156 $drop_header = "";
157 $match = array();
158 if (preg_match('#^(Subject:(.*?))$#m', $this->msg, $match))
159 {
160 $this->subject = (trim($match[2]) != '') ? trim($match[2]) : (($this->subject != '') ? $this->subject : 'No Subject');
161 $drop_header .= '[\r\n]*?' . phpbb_preg_quote($match[1], '#');
162 }
163 else
164 {
165 $this->subject = (($this->subject != '') ? $this->subject : 'No Subject');
166 }
167
168 if (preg_match('#^(Charset:(.*?))$#m', $this->msg, $match))
169 rizwank 1.1 {
170 $this->encoding = (trim($match[2]) != '') ? trim($match[2]) : trim($lang['ENCODING']);
171 $drop_header .= '[\r\n]*?' . phpbb_preg_quote($match[1], '#');
172 }
173 else
174 {
175 $this->encoding = trim($lang['ENCODING']);
176 }
177
178 if ($drop_header != '')
179 {
180 $this->msg = trim(preg_replace('#' . $drop_header . '#s', '', $this->msg));
181 }
182
183 return true;
184 }
185
186 //
187 // Send the mail out to the recipients set previously in var $this->address
188 //
189 function send()
190 rizwank 1.1 {
191 global $phpEx, $phpbb_root_path;
192
193 if ( $this->address == NULL )
194 {
195 message_die(GENERAL_ERROR, 'No email address set', '', __LINE__, __FILE__);
196 }
197
198 if ( !$this->parse_email() )
199 {
200 return false;
201 }
202
203 //
204 // Add date and encoding type
205 //
206 $universal_extra = "MIME-Version: 1.0\nContent-type: text/plain; charset=" . $this->encoding . "\nContent-transfer-encoding: 8bit\nDate: " . gmdate('D, d M Y H:i:s', time()) . " UT\nX-Priority: 3\nX-MSMail-Priority: Normal\nX-Mailer: PHP\n";
207 $this->extra_headers = $universal_extra . trim($this->extra_headers);
208
209 if ( $this->use_smtp )
210 {
211 rizwank 1.1 if ( !defined('SMTP_INCLUDED') )
212 {
213 include($phpbb_root_path . 'includes/smtp.' . $phpEx);
214 }
215
216 $result = smtpmail($this->address, $this->subject, $this->msg, $this->extra_headers);
217 }
218 else
219 {
220 $result = @mail($this->address, $this->subject, $this->msg, $this->extra_headers);
221 }
222
223 if ( !$result )
224 {
225 message_die(GENERAL_ERROR, 'Failed sending email :: ' . $result, '', __LINE__, __FILE__);
226 }
227
228 return true;
229 }
230
231
232 rizwank 1.1 //
233 // Attach files via MIME.
234 //
235 function attachFile($filename, $mimetype = "application/octet-stream", $szFromAddress, $szFilenameToDisplay)
236 {
237 global $lang;
238 $mime_boundary = "--==================_846811060==_";
239
240 $this->mailMsg = '--' . $mime_boundary . "\nContent-Type: text/plain;\n\tcharset=\"" . $lang['ENCODING'] . "\"\n\n" . $this->mailMsg;
241
242 if ($mime_filename)
243 {
244 $filename = $mime_filename;
245 $encoded = $this->encode_file($filename);
246 }
247
248 $fd = fopen($filename, "r");
249 $contents = fread($fd, filesize($filename));
250
251 $this->mimeOut = "--" . $mime_boundary . "\n";
252 $this->mimeOut .= "Content-Type: " . $mimetype . ";\n\tname=\"$szFilenameToDisplay\"\n";
253 rizwank 1.1 $this->mimeOut .= "Content-Transfer-Encoding: quoted-printable\n";
254 $this->mimeOut .= "Content-Disposition: attachment;\n\tfilename=\"$szFilenameToDisplay\"\n\n";
255
256 if ( $mimetype == "message/rfc822" )
257 {
258 $this->mimeOut .= "From: ".$szFromAddress."\n";
259 $this->mimeOut .= "To: ".$this->emailAddress."\n";
260 $this->mimeOut .= "Date: ".date("D, d M Y H:i:s") . " UT\n";
261 $this->mimeOut .= "Reply-To:".$szFromAddress."\n";
262 $this->mimeOut .= "Subject: ".$this->mailSubject."\n";
263 $this->mimeOut .= "X-Mailer: PHP/".phpversion()."\n";
264 $this->mimeOut .= "MIME-Version: 1.0\n";
265 }
266
267 $this->mimeOut .= $contents."\n";
268 $this->mimeOut .= "--" . $mime_boundary . "--" . "\n";
269
270 return $out;
271 // added -- to notify email client attachment is done
272 }
273
274 rizwank 1.1 function getMimeHeaders($filename, $mime_filename="")
275 {
276 $mime_boundary = "--==================_846811060==_";
277
278 if ($mime_filename)
279 {
280 $filename = $mime_filename;
281 }
282
283 $out = "MIME-Version: 1.0\n";
284 $out .= "Content-Type: multipart/mixed;\n\tboundary=\"$mime_boundary\"\n\n";
285 $out .= "This message is in MIME format. Since your mail reader does not understand\n";
286 $out .= "this format, some or all of this message may not be legible.";
287
288 return $out;
289 }
290
291 //
292 // Split string by RFC 2045 semantics (76 chars per line, end with \r\n).
293 //
294 function myChunkSplit($str)
295 rizwank 1.1 {
296 $stmp = $str;
297 $len = strlen($stmp);
298 $out = "";
299
300 while ($len > 0)
301 {
302 if ($len >= 76)
303 {
304 $out .= substr($stmp, 0, 76) . "\r\n";
305 $stmp = substr($stmp, 76);
306 $len = $len - 76;
307 }
308 else
309 {
310 $out .= $stmp . "\r\n";
311 $stmp = "";
312 $len = 0;
313 }
314 }
315 return $out;
316 rizwank 1.1 }
317
318 //
319 // Split the specified file up into a string and return it
320 //
321 function encode_file($sourcefile)
322 {
323 if (is_readable(phpbb_realpath($sourcefile)))
324 {
325 $fd = fopen($sourcefile, "r");
326 $contents = fread($fd, filesize($sourcefile));
327 $encoded = $this->myChunkSplit(base64_encode($contents));
328 fclose($fd);
329 }
330
331 return $encoded;
332 }
333
334 } // class emailer
335
336 ?>
|