aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/includes/emailer.php
blob: b93e61f03c6cda97fae271022eccd1d2b6a9a74e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<?php
/***************************************************************************
                                emailer.php
                             -------------------
    begin                : Sunday Aug. 12, 2001
    copyright            : (C) 2001 The phpBB Group
    email                : support@phpbb.com

    $Id$

***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

//
// The emailer class has support for attaching files, that isn't implemented
// in the 2.0 release but we can probable find some way of using it in a future
// release
//
class emailer
{
	var $tpl_file;
	var $use_smtp;
	var $msg;
	var $mimeOut;
	var $arrPlaceHolders = array();	// an associative array that has the key = placeHolderName and val = placeHolderValue.
	var $subject, $extra_headers, $address;

	function emailer($use_smtp)
	{
		$this->use_smtp = $use_smtp;
		$this->tpl_file = NULL;
		$this->address = NULL;
 		$this->msg = "";
	   $this->mimeOut = "";
	}

	//
	// Resets all the data (address, template file, etc etc to default
	//
	function reset()
	{
		$this->tpl_file = "";
		$this->address = "";
		$this->msg = "";
		$this->memOut = "";
		$this->vars = "";
	}

	//
	// Sets an email address to send to
	//
	function email_address($address)
	{

		$success = true;

		$this->address = '';
		$this->address .= $address;

		return $success;
	}

	//
	// set up subject for mail
	//
	function set_subject($subject)
	{
		$this->subject = $subject;
	}

	//
	// set up extra mail headers
	//
	function extra_headers($headers)
	{
		$this->extra_headers = $headers;
	}

	function use_template($template_file, $template_lang = "")
	{
		global $board_config, $phpbb_root_path;

		if( $template_lang == "" )
		{
			$template_lang = $board_config['default_lang'];
		}

		$template_file = $phpbb_root_path . "language/lang_" . $template_lang . "/email/" . $template_file . ".tpl";
		if( !file_exists($template_file) )
		{
			message_die(GENERAL_ERROR, "Couldn't find template file: $template_file", "", __LINE__, __FILE__);
		}
		else
		{
			$this->tpl_file = $template_file;
			if( !$this->load_msg() )
			{
				message_die(GENERAL_ERROR, "Couldn't load template file: $template_file", "", __LINE__, __FILE__);
			}
		}

		return TRUE;
	}

	//
	// Open the template file and read in the message
	//
	function load_msg()
	{
		if ($this->tpl_file == NULL)
		{
			message_die(GENERAL_ERROR, "No template file set", "", __LINE__, __FILE__);
		}
		else
		{
			if(!($fd = fopen($this->tpl_file, 'r')))
			{
				message_die(GENERAL_ERROR, "fopen failed opening template file", "", __LINE__, __FILE__);
			}
			else
			{
				$this->msg .= fread($fd, filesize($this->tpl_file));
				fclose($fd);
			}
		}
		return TRUE;
	}

	function assign_vars($vars)
	{
		if(empty($this->vars))
		{
			$this->vars = $vars;
		}
		else
		{
			$this->vars .= $vars;
		}
	}

	function parse_email()
	{
		@reset($this->vars);
		while (list($key, $val) = @each($this->vars))
		{
			$$key = $val;
		}

		//$this->mailMsg = ereg_replace("<!!( )*([^>]*)( )*!!>", '$this->arrPlaceHolders['."\\2".']', $this->mailMsg);
		// $this->msg = ereg_replace("{( )*([^>]*)( )*}", '$'."\\2", $this->msg);
		$this->msg = preg_replace('#\{([a-z0-9\-_]*?)\}#is', '$'."\\1", $this->msg);

    	// Escape all quotes, else the eval will fail.
		$this->msg = str_replace ("\"", "\\\"", $this->msg);

		eval("\$this->msg = \"$this->msg\";");

		return TRUE;
	}

	//
	// Send the mail out to the recipients set previously in var $this->address
	//
	function send()
	{
		global $phpEx, $phpbb_root_dir;

		if (isset($phpbb_root_dir))
		{
			// we must be in the admin section.
			$phpbb_root_path = $phpbb_root_dir;
		}
		else
		{
			$phpbb_root_path = "./";
		}

		if ($this->address == NULL)
		{
			message_die(GENERAL_ERROR, "No email address set", "", __LINE__, __FILE__);
		}
		else
		{
			if(!$this->parse_email())
			{
				return FALSE;
			}
			if($this->use_smtp)
			{
				include($phpbb_root_path . "includes/smtp.".$phpEx);
				if(!smtpmail($this->address, $this->subject, $this->msg, $this->extra_headers))
				{
					message_die(GENERAL_ERROR, "Sending via SMTP failed", "", __LINE__, __FILE__);
				}
			}
			else
			{
				@mail($this->address, $this->subject, $this->msg, $this->extra_headers);
			}
		}

		return TRUE;
	}


	//
   // Attach files via MIME.
	//
	function attachFile($filename, $mimetype="application/octet-stream", $szFromAddress, $szFilenameToDisplay)
	{
		$mime_boundary = "--==================_846811060==_";

		$this->mailMsg = "--".$mime_boundary."\nContent-Type: text/plain;\n\tcharset=\"iso-8859-1\"\n\n".$this->mailMsg;

		if ($mime_filename)
		{
			$filename = $mime_filename;
			$encoded = $this->encode_file($filename);
		}

		$fd = fopen($filename, "r");
		$contents = fread($fd, filesize($filename));

		$this->mimeOut = "--" . $mime_boundary . "\n";
		$this->mimeOut .= "Content-Type: " . $mimetype . ";\n\tname=\"$szFilenameToDisplay\"\n";
		$this->mimeOut .= "Content-Transfer-Encoding: quoted-printable\n";
		$this->mimeOut .= "Content-Disposition: attachment;\n\tfilename=\"$szFilenameToDisplay\"\n\n";

		if ($mimetype == "message/rfc822")
		{
			$this->mimeOut .= "From: ".$szFromAddress."\n";
			$this->mimeOut .= "To: ".$this->emailAddress."\n";
			$this->mimeOut .= "Date: ".date("D, d M Y G:i:s ").$this->getTimeZoneInEmailFormat()."\n";
			$this->mimeOut .= "Reply-To:".$szFromAddress."\n";
			$this->mimeOut .= "Subject: ".$this->mailSubject."\n";
			$this->mimeOut .= "X-Mailer: PHP/".phpversion()."\n";
			$this->mimeOut .= "MIME-Version: 1.0\n";
		}

		$this->mimeOut .= $contents."\n";
		$this->mimeOut .= "--" . $mime_boundary . "--" . "\n";

		return $out;
		// added -- to notify email client attachment is done
	}

	function getMimeHeaders($filename, $mime_filename="")
	{
		$mime_boundary = "--==================_846811060==_";

		if ($mime_filename)
		{
			$filename = $mime_filename;
		}

		$out = "MIME-Version: 1.0\n";
		$out .= "Content-Type: multipart/mixed;\n\tboundary=\"$mime_boundary\"\n\n";
		$out .= "This message is in MIME format. Since your mail reader does not understand\n";
		$out .= "this format, some or all of this message may not be legible.";

		return $out;
	}

	//
   // Split string by RFC 2045 semantics (76 chars per line, end with \r\n).
	//
	function myChunkSplit($str)
	{
		$stmp = $str;
		$len = strlen($stmp);
		$out = "";

		while ($len > 0)
		{
			if ($len >= 76)
			{
				$out .= substr($stmp, 0, 76) . "\r\n";
				$stmp = substr($stmp, 76);
				$len = $len - 76;
			}
			else
			{
				$out .= $stmp . "\r\n";
				$stmp = "";
				$len = 0;
			}
		}
		return $out;
	}

	//
   // Split the specified file up into a string and return it
	//
	function encode_file($sourcefile)
	{
		if (is_readable($sourcefile))
		{
			$fd = fopen($sourcefile, "r");
			$contents = fread($fd, filesize($sourcefile));
	      $encoded = $this->myChunkSplit(base64_encode($contents));
	      fclose($fd);
		}

		return $encoded;
	}

} // class emailer

?>