KD2 Framework  Diff

Differences From Artifact [ab4815bcc9]:

  • File src/lib/kd2/SMTP.php — part of check-in [09b118a2fd] at 2013-04-12 12:33:08 on branch trunk — SMTP library (user: bohwaz size: 6003)

To Artifact [820842c738]:

  • File src/lib/kd2/SMTP.php — part of check-in [1aded40c2e] at 2013-04-27 09:58:40 on branch trunk — Applied a lot of suggestions and bug corrections reported by @LK, thanks (user: bohwaz size: 7681)

1
2
3
4
5
6
7
8
9
10
11
12
13
..
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
...
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
...
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
...
242
243
244
245
246
247
248
















249
250
251
252
253
254
255
...
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?php
namespace KD2;

/*
	Simple SMTP library for PHP
	Copyright 2012 BohwaZ <http://bohwaz.net/>

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU Lesser General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	any later version.

	This program is distributed in the hope that it will be useful,
................................................................................
	}

	protected function _write($data, $eol = true)
	{
		fputs($this->conn, $data . ($eol ? self::EOL : ''));
	}

	public function __construct($server, $port, $username = null, $password = null, $secure = self::NONE)
	{
		$this->server = $secure == self::SSL ? 'ssl://' . $server : $server;
		$this->port = $port;
		$this->username = $username;
		$this->password = $password;
		$this->secure = (int)$secure;
		$this->servername = isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : gethostname();
................................................................................
				throw new SMTP_Exception('SMTP AUTH error: '.$this->last_line);
			}
		}

		return true;
	}









	public function send($to, $subject, $message, $headers = array())
	{
		if (is_null($this->conn))
		{
			$this->connect();
			$this->authenticate();
		}


























		if (!isset($headers['Date']))
		{
			$headers['Date'] = date(DATE_RFC822);
		}

		$headers['To'] = $to;
		$headers['Subject'] = '=?UTF-8?B?'.base64_encode($subject).'?=';



		$headers['MIME-Version'] = '1.0';




		$headers['Content-type'] = 'text/plain; charset=UTF-8';


		if (!isset($headers['From']))
		{
			$headers['From'] = 'mail@'.$this->servername;
		}

		$content = '';
................................................................................
			$content .= $name . ': ' . $value . self::EOL;
		}

		$content = trim($content) . self::EOL . self::EOL . $message . self::EOL;
		$content = preg_replace("#(?<!\r)\n#si", self::EOL, $content);
		$content = wordwrap($content, 998, self::EOL, true);


		list($from) = self::extractEmailAddresses($headers['From']);


		$to = self::extractEmailAddresses($headers['To']);


		if (isset($headers['Cc']))
		{
			$to = array_merge(self::extractEmailAddresses($headers['Cc']));



		}

		if (isset($headers['Bcc']))
		{
			$to = array_merge(self::extractEmailAddresses($headers['Bcc']));



		}

		$this->_write('MAIL FROM: <'.$from.'>');
		$this->_read();

		foreach ($to as $dest)
		{
................................................................................
	 * addresses it can find out of it.
	 * This is not perfect as it won't handle addresses like "uncommon,email"@email.tld
	 * because of the comma, but FILTER_VALIDATE_EMAIL doesn't accept it as an email address either
	 * (though it's perfectly valid if you follow the RFC).
	 */
	public static function extractEmailAddresses($str)
	{
















		$str = explode(',', $str);
		$out = array();

		foreach ($str as $s)
		{
			$s = trim($s);
			if (preg_match('/(?:([\'"]).*?\1\s*)?<([^>]*)>/', $s, $match) && filter_var(trim($match[2]), FILTER_VALIDATE_EMAIL))
................................................................................
			}
			elseif (filter_var($s, FILTER_VALIDATE_EMAIL))
			{
				$out[] = $s;
			}
			else
			{
				echo "$s\n";
			}
		}

		return $out;
	}
}

?>





|







 







|







 







>
>
>
>
>
>
>
>








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>





<

>
>
>
|
>
>
>
>
|
>







 







>


>
|
>



|
>
>
>




|
>
>
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







|








1
2
3
4
5
6
7
8
9
10
11
12
13
..
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
...
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
...
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
...
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
...
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<?php
namespace KD2;

/*
	Simple SMTP library for PHP
	Copyright 2012-2013 BohwaZ <http://bohwaz.net/>

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU Lesser General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	any later version.

	This program is distributed in the hope that it will be useful,
................................................................................
	}

	protected function _write($data, $eol = true)
	{
		fputs($this->conn, $data . ($eol ? self::EOL : ''));
	}

	public function __construct($server = 'localhost', $port = 25, $username = null, $password = null, $secure = self::NONE)
	{
		$this->server = $secure == self::SSL ? 'ssl://' . $server : $server;
		$this->port = $port;
		$this->username = $username;
		$this->password = $password;
		$this->secure = (int)$secure;
		$this->servername = isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : gethostname();
................................................................................
				throw new SMTP_Exception('SMTP AUTH error: '.$this->last_line);
			}
		}

		return true;
	}

	/**
	 * Send an email to $to, using $subject as a subject and $message as content
	 * @param  mixed  $to      List of recipients, as an array or a string
	 * @param  string $subject Message subject
	 * @param  string $message Message content
	 * @param  mixed  $headers Additional headers, either as an array of key=>value pairs or a string
	 * @return boolean		   TRUE if success, exception if it fails
	 */
	public function send($to, $subject, $message, $headers = array())
	{
		if (is_null($this->conn))
		{
			$this->connect();
			$this->authenticate();
		}

		// Parse $headers if it's a string
		if (is_string($headers))
		{
			preg_match_all('/^(\\S.*?):(.*?)\\s*(?=^\\S|\\Z)/sm', $headers, $match);
			$headers = array();

			foreach ($match as $header)
			{
				$headers[$header[1]] = $header[2];
			}
		}

		// Normalize headers
		$headers_normalized = array();

		foreach ($headers as $key=>$value)
		{
			$key = preg_replace_callback('/^.|(?<=-)./', function ($m) { return ucfirst($m[0]); }, strtolower(trim($key)));
			$headers_normalized[$key] = $value;
		}

		$headers = $headers_normalized;
		unset($headers_normalized);

		// Set default headers if they are missing
		if (!isset($headers['Date']))
		{
			$headers['Date'] = date(DATE_RFC822);
		}


		$headers['Subject'] = '=?UTF-8?B?'.base64_encode($subject).'?=';

		if (!isset($headers['MIME-Version']))
		{
			$headers['MIME-Version'] = '1.0';
		}

		if (!isset($headers['Content-Type']))
		{
			$headers['Content-Type'] = 'text/plain; charset=UTF-8';
		}

		if (!isset($headers['From']))
		{
			$headers['From'] = 'mail@'.$this->servername;
		}

		$content = '';
................................................................................
			$content .= $name . ': ' . $value . self::EOL;
		}

		$content = trim($content) . self::EOL . self::EOL . $message . self::EOL;
		$content = preg_replace("#(?<!\r)\n#si", self::EOL, $content);
		$content = wordwrap($content, 998, self::EOL, true);

		// Take the first sender address for the smtp
		list($from) = self::extractEmailAddresses($headers['From']);

		// Extract and filter recipients addresses
		$to = self::extractEmailAddresses($to);
		$headers['To'] = implode(', ', $to);

		if (isset($headers['Cc']))
		{
			$headers['Cc'] = self::extractEmailAddresses($headers['Cc']);
			$to = array_merge($to, $headers['Cc']);

			$headers['Cc'] = implode(', ', $headers['Cc']);
		}

		if (isset($headers['Bcc']))
		{
			$headers['Bcc'] = self::extractEmailAddresses($headers['Bcc']);
			$to = array_merge($to, $headers['Bcc']);

			$headers['Bcc'] = implode(', ', $headers['Bcc']);
		}

		$this->_write('MAIL FROM: <'.$from.'>');
		$this->_read();

		foreach ($to as $dest)
		{
................................................................................
	 * addresses it can find out of it.
	 * This is not perfect as it won't handle addresses like "uncommon,email"@email.tld
	 * because of the comma, but FILTER_VALIDATE_EMAIL doesn't accept it as an email address either
	 * (though it's perfectly valid if you follow the RFC).
	 */
	public static function extractEmailAddresses($str)
	{
		if (is_array($str))
		{
			$out = array();

			// Filter invalid email addresses
			foreach ($str as $email)
			{
				if (filter_var($email, FILTER_VALIDATE_EMAIL))
				{
					$out[] = $email;
				}
			}

			return $out;
		}

		$str = explode(',', $str);
		$out = array();

		foreach ($str as $s)
		{
			$s = trim($s);
			if (preg_match('/(?:([\'"]).*?\1\s*)?<([^>]*)>/', $s, $match) && filter_var(trim($match[2]), FILTER_VALIDATE_EMAIL))
................................................................................
			}
			elseif (filter_var($s, FILTER_VALIDATE_EMAIL))
			{
				$out[] = $s;
			}
			else
			{
				// unrecognized, skip
			}
		}

		return $out;
	}
}

?>