@@ -1,11 +1,11 @@ + Copyright 2012-2013 BohwaZ 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. @@ -73,11 +73,11 @@ 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) + 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; @@ -166,27 +166,67 @@ } 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['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['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; } @@ -200,22 +240,31 @@ $content = trim($content) . self::EOL . self::EOL . $message . self::EOL; $content = preg_replace("#(?_write('MAIL FROM: <'.$from.'>'); $this->_read(); @@ -244,10 +293,26 @@ * 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) { @@ -260,14 +325,14 @@ { $out[] = $s; } else { - echo "$s\n"; + // unrecognized, skip } } return $out; } } ?>