summaryrefslogtreecommitdiff
path: root/mail.class
blob: 08006cca1f2fdc7ed56dc77d4e2a45f48b798bc2 (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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
<?php
/*
**	$Id$
**
**	mail.class - Create a mail object, add header and content and send it
**
**	ITools - the Internet Tools Library
**
**	Copyright (C) 1995-2003 by the ITools Authors.
**	This program is free software; you can redistribute it and/or
**	modify it under the terms of either the GNU General Public License
**	or the GNU Lesser General Public License, as published by the Free
**	Software Foundation. See http://www.gnu.org/licenses/ for details.
*/

define('IT_MAIL_PLAIN', 0);
define('IT_MAIL_HTML', 1);

/* Return codes of it_mail::check_email() */
define('IT_MAIL_CHECKEMAIL_INVALID', 0);
define('IT_MAIL_CHECKEMAIL_MAILBOXFULL', 5);
define('IT_MAIL_CHECKEMAIL_OK', 10);

/**
 * Construct an email message with headers, body (plaintext and/or HTML) and
 * send it. Also provides utility function to check email address validity.<br>
 * <em>Example:</em><br>
 * <code><nobr>
 * if (!it_mail::check_email($to))<br>
 * &nbsp;&nbsp;&nbsp;&nbsp;die("Invalid email address '$to'\n");<br>
 * <br>
 * $mail = new it_mail(array('To' => $to&#44; 'From' => 'itools@gna.ch', 'Subject' => 'Example'));<br>
 * $mail->add_body('Plain text message body');<br>
 * $mail->add_body('HTML message with &lt;a href="http://gna.ch/"&gt;link&lt;/a&gt;', IT_MAIL_HTML);<br>
 * $mail->send();<br>
 * </nobr></code>
 */
class it_mail
{
	var $header_names = array();
	var $header_values = array();
	var $body = array();
	var $attachments = array();

	var $to = array();
	var $subject = "";
	var $cc = array();
	var $bcc = array();
	var $flags = "";
	var $charset = 'iso-8859-1';

/**
 * Construct a new email message. Headers and body can be added later.
 * Note: Headers To, Cc, Bcc can be arrays
 * @param $headers Array of headers for this email, e.g. From, To and Subject
 */
function it_mail($headers = "")
{
	if (is_array($headers))
	{
		foreach ($headers as $header => $value)
			$this->add_header($header, $value);
	}
}


/**
 * Add header line to the this email message. Can be called repeatedly.
 * Note: Headers To, Cc, Bcc can be arrays
 * @param $header Header to be added, e.g. Cc, Bcc or X-My-Header
 * @param $value Value of header, e.g. itools@gna.ch for Bcc header
 */
function add_header($header, $value)
{
	$value = $this->header_escape($value);

	switch ($header)
	{
		case 'To':
			if (is_array($value))
			{
				foreach ($value as $val)
					$this->to[] = $val;
			}
			else
				$this->to[] = $value;
			break;

		case 'Subject':
			$this->subject .= $value;
			break;

		case 'Cc':
			if (is_array($value))
			{
				foreach ($value as $val)
					$this->cc[] = $val;
			}
			else
				$this->cc[] = $value;
			break;

		case 'Bcc':
			if (is_array($value))
			{
				foreach ($value as $val)
					$this->bcc[] = $val;
			}
			else
				$this->bcc[] = $value;
			break;

		case 'charset':
			$this->charset = $value;
			break;

		case 'Errors-To':
			$this->flags = "-f " . escapeshellarg(preg_replace('/.*<([^>]+)>.*/', '$1', $value));
			/* FALLTHROUGH */
		default:
			$this->header_names[] = $header;
			$this->header_values[] = $value;
			break;
	}
}


/**
 * Add body part to this email message. Can be called repeatedly.
 * @param $text Text to be added to email message
 * @param $type Type of text, one of IT_MAIL_PLAIN (default) or IT_MAIL_HTML
 */
function add_body($text, $type = IT_MAIL_PLAIN)
{
	switch ($type)
	{
		case IT_MAIL_PLAIN:
		case IT_MAIL_HTML:
			$this->body[$type] .= $text;
			break;

		default:
			internal_error("it_mail::add_body invalid type $type");
			break;
	}
}


/**
 * Add attachment to this email message. Can be called repeatedly.
 * @param $data Data to be attached
 * @param $name Name of attached file
 * @param $mimetype MIME-Type of attached file
 * @return 'cid:'.Content-ID of this attachment
 */
function add_attachment($data, $mimetype = "application/octet-stream", $name = '')
{
	if ($name == '')
		$name = 'Attachment' . (count($this->attachments) + 1);

	$cid = md5(uniqid(rand()));
	$this->attachments[] = array ('mimetype' => $mimetype, 'data' => $data, 'encode' => $encode, 'name' => $name, 'cid' => $cid);

	return 'cid:'.$cid;
}


/**
 * Add file attachment to this email message. Can be called repeatedly.
 * @param $filename File to be attached
 * @param $name Name of attached file as stored in mail
 * @param $mimetype MIME-Type of attached file
 */
function add_file($filename, $mimetype = "application/octet-stream", $name = '')
{
	if ($name == '')
		$name = basename($filename);

	if ($file = @fopen($filename, "r"))
	{
		if ($data = fread($file, @filesize($filename)))
			$result = $this->add_attachment($data, $mimetype, $name);

		fclose($file);
	}

	return $result;
}


/**
 * Send this email message
 * @return True if mail was sent
 */
function send()
{
	$to = join(",", $this->to);
	$headers = array();

	if (count($this->cc) > 0)
		$headers[] = "Cc: " . join(",", $this->cc);

	if (count($this->bcc) > 0)
		$headers[] = "Bcc: " . join(",", $this->bcc);

	for ($i = 0; $i < count($this->header_names); $i++)
		$headers[] = $this->header_names[$i] . ': ' . $this->header_values[$i];


	/* Automatically add doctype if none given */
	if ($this->body[IT_MAIL_HTML] && !eregi('^<!doctype', $this->body[IT_MAIL_HTML]))
	{
		$this->body[IT_MAIL_HTML] = '<!doctype html public "-//w3c//dtd html 4.01 transitional//en">' . "\n" . $this->body[IT_MAIL_HTML];
	}


	$headers[] = "MIME-Version: 1.0";

	if ($this->attachments)
	{
		/* Attachments need multipart MIME mail */
		$boundary1 = md5(uniqid(rand()));
		$mixedtype = "Content-Type: multipart/mixed; boundary=\"$boundary1\"";

		$headers[] = $mixedtype;
		$text .= "This is a multi-part message in MIME format.";
		$text .= "\n--$boundary1\n";
	}

	/* Headers for plain and HTML content */
	$plaintype = "Content-Type: text/plain; charset=".$this->charset."\nContent-Transfer-Encoding: 8bit";
	$htmltype = "Content-Type: text/html; charset=".$this->charset."\nContent-Transfer-Encoding: 8bit";

	if ($this->body[IT_MAIL_PLAIN])
	{
		if ($this->body[IT_MAIL_HTML])
		{
			$boundary2 = md5(uniqid(rand()));
			$alternativetype = "Content-Type: multipart/alternative; boundary=\"$boundary2\"";

			/* Plain and HTML */
			if ($this->attachments)
				$text .= "$alternativetype\n\n";
			else
				$headers[] = $alternativetype;

			$text .= "--$boundary2\n$plaintype\n\n";
			$text .= $this->body[IT_MAIL_PLAIN];
			$text .= "\n--$boundary2\n$htmltype\n\n";
			$text .= $this->body[IT_MAIL_HTML];
			$text .= "\n--$boundary2--\n";
		}
		else
		{
			/* Just plain */
			if ($this->attachments)
				$text .= "$plaintype\n\n";
			else
				$headers[] = $plaintype;

			$text .= $this->body[IT_MAIL_PLAIN];
		}
	}
	else if ($this->body[IT_MAIL_HTML])
	{
		/* Just HTML */
		if ($this->attachments)
		{
			if (strstr($this->body[IT_MAIL_HTML], "cid:"))
			{
				$boundary3 = md5(uniqid(rand()));
				$text .= "Content-Type: multipart/related; boundary=\"$boundary3\"\n\n--$boundary3\n";
			}

			$text .= "$htmltype\n\n";
		}
		else
			$headers[] = $htmltype;

		$text .= $this->body[IT_MAIL_HTML];
	}

	if ($this->attachments)
	{
		$text .= "\n";
		$boundary = $boundary3 ? $boundary3 : $boundary1;

		foreach ($this->attachments as $attachment)
		{
			$text .= "\n--$boundary\nContent-Type: {$attachment['mimetype']}; name=\"{$attachment['name']}\"\nContent-Transfer-Encoding: base64\nContent-ID: <{$attachment['cid']}>\nContent-Disposition: inline; filename=\"{$attachment['name']}\"\n\n";
			$text .= chunk_split(base64_encode($attachment['data']));
		}

		if ($boundary3)
			$text .= "\n--$boundary3--\n\n";

		$text .= "--$boundary1--\n";
	}

	return mail($to, $this->subject, $text, join("\n", $headers), $this->flags);
}


/**
 * INTERNAL: Escape header line with ?=iso-8859-1? if necessary, e.g. in Subject line
 * @param $string String to be escaped
 * @return String escape suitable for sending in header line
 */
function header_escape($string)
{
	return preg_replace('/[\x80-\xff]/e', "sprintf('=?iso-8859-1?Q?=%02X?=', ord('\\0'))", $string);
}

/**
 * Make string safe to be used in "$fullname <$email>": Remove illegal
 * characters and enclose in double quotes.
 * @param $string Fulle name to be escaped
 * @return String to be safely used in "$fullname <$email>" for To: etc.
 */
function fullname_escape($string)
{
	return '"' . preg_replace('/["\x00-\x1f]/', '', $string) . '"';
}

/**
 * INTERNAL: Send SMTP command and return true if result is 2XX
 * @param $fp Connection to SMTP server opened using fsockopen
 * @param $command Command to send, e.g. HELO gna.ch
 * @param $anwer String containing full answer from SMTP server
 * @return True if SMTP result code is 2XX
 *
 */
function send_smtp_cmd($fp, $cmd, &$answer, $timeoutok = false)
{
	$result = false;
	$answer = '';

	if (!$cmd || (!feof($fp) && fwrite($fp, "$cmd\r\n")))
	{
		while (!feof($fp) && ereg('^(.)..(.?)(.*)$', fgets($fp, 1024), $regs))
		{
			$answer .= $regs[0];

			if ($regs[2] != '-')	/* Multi line response? */
			{
				if ($regs[1] == 2)
					$result = true;

				break;
			}
		}
	}

	/* A timeout can be considered ok for SLOW mail servers */
	if ($answer == '')
		$result = $timeoutok;

	//echo ">>it_mail::send_smtp_cmd " . time() . " '$cmd' => $result: $answer";

	return $result;
}


/**
 * Check if given email address is valid (syntax, MX record). If you set
 * checkmailbox to true it also connects to MX and asks if mailbox is valid.
 * Note: Connecting to the MX can be slow.
 * @param $email email to be check
 * @param $checkmailbox True if you want to connect to MX and check account
 * @return One of IT_MAIL_CHECKEMAIL_INVALID (0), IT_MAIL_CHECKEMAIL_MAILBOXFULL
 * or IT_MAIL_CHECKEMAIL_OK (both != 0).
 */
function check_email($email, $checkmailbox = true)
{
	$result = IT_MAIL_CHECKEMAIL_INVALID;

	/* Check if username starts with www. or not well-formed => reject */
	if (!ereg('^www\.', $email) && eregi('^[a-z0-9&_+.-]+@([a-z0-9.-]+\.[a-z]+)$', $email, $regs))
	{
		$domain = $regs[1];

		if (!getmxrr($domain, $mxhosts, $weights))
		{
			/* If we find no MX we check if domain exists */
			if (gethostbynamel($domain))
			{
				$mxhosts = array($domain);
				$weights = array(1);
			}
		}

		if ($mxhosts)	/* We need at least one valid MX */
		{
			if ($checkmailbox)
			{
				$mx = array();

				for ($i = 0; $i < count($mxhosts); $i++)
					$mx[$mxhosts[$i]] = $weights[$i];

				asort($mx, SORT_NUMERIC);
				$port = getservbyname('smtp', 'tcp');
				$from = 'itools@gna.ch';

				/* Determine domain and email for test, skip if called as it_mail::check_email() */
				if (is_object($this))
				{
					for ($i = 0; $i < count($this->header_names); $i++)
					{
						if ($this->header_names[$i] == 'From')
						{
							$from = $this->header_values[$i];
							break;
						}
					}
				}

				if (eregi('@([a-z0-9.-]+\.[a-z]+)$', $from, $regs))
					$fromdomain = $regs[1];
				else
					$fromdomain = 'gna.ch';

				$finished = false;
				$connected = 0;

				if (function_exists('stream_set_timeout'))
					$timeout = 'stream_set_timeout';
				else
					$timeout = 'socket_set_timeout';

				foreach ($mx as $mxhost => $weight)
				{
					//echo ">>it_mail::check_email: " . time() . " checking $mxhost\n";
					if ($fp = @fsockopen($mxhost, $port, $errno, $errstr, 5))
					{
						$connected++;
						$timeout($fp, 45);
						$answer = '';

						if (it_mail::send_smtp_cmd($fp, '', $answer) && it_mail::send_smtp_cmd($fp, "HELO $fromdomain", $answer) && it_mail::send_smtp_cmd($fp, "MAIL FROM: <$from>", $answer))
						{
							$timeout($fp, 2);
							$timeoutok = ($domain != 'bluewin.ch');

							if (it_mail::send_smtp_cmd($fp, "RCPT TO: <$email>", $answer, $timeoutok))
								$result = IT_MAIL_CHECKEMAIL_OK;
							else if (eregi('quota|full|exceeded storage', $answer))
								$result = IT_MAIL_CHECKEMAIL_MAILBOXFULL;

							/* Do not try other MX if we got this far */
							$finished = true;
						}

						$timeout($fp, 0, 1);

						it_mail::send_smtp_cmd($fp, 'RSET', $answer);

						it_mail::send_smtp_cmd($fp, 'QUIT', $answer);
						fclose($fp);
					}

					if ($finished)
						break;
				}

				/* If we cannot connect to _any_ MX it could be because of temporary overload / virus attack */
				if (!$connected)
					$result = IT_MAIL_CHECKEMAIL_OK;
			}
			else
				$result = IT_MAIL_CHECKEMAIL_OK;
		}
	}

	return $result;
}

}

?>