![]() |
Xataface Email Module 0.2
Email/Mailmerge Module for Xataface
|
00001 <?php 00002 00003 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 00004 * * 00005 * XPertMailer is a PHP Mail Class that can send and read messages in MIME format. * 00006 * This file is part of the XPertMailer package (http://xpertmailer.sourceforge.net/) * 00007 * Copyright (C) 2007 Tanase Laurentiu Iulian * 00008 * * 00009 * This library is free software; you can redistribute it and/or modify it under the * 00010 * terms of the GNU Lesser General Public License as published by the Free Software * 00011 * Foundation; either version 2.1 of the License, or (at your option) any later version. * 00012 * * 00013 * This library is distributed in the hope that it will be useful, but WITHOUT ANY * 00014 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * 00015 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. * 00016 * * 00017 * You should have received a copy of the GNU Lesser General Public License along with * 00018 * this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, * 00019 * Fifth Floor, Boston, MA 02110-1301, USA * 00020 * * 00021 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 00022 00023 if (!class_exists('MIME4')) require_once 'MIME4.php'; 00024 00025 $_RESULT = array(); 00026 00027 class SMTP4 { 00028 00029 var $CRLF = "\r\n"; 00030 var $PORT = 25; 00031 var $TOUT = 30; 00032 var $COUT = 5; 00033 var $BLEN = 1024; 00034 00035 function _cres($conn = null, &$resp, $code1 = null, $code2 = null, $debug = null) { 00036 if (!FUNC4::is_debug($debug)) $debug = debug_backtrace(); 00037 $err = array(); 00038 if (!is_resource($conn)) $err[] = 'invalid resource connection'; 00039 if (!(is_int($code1) && $code1 > 99 && $code1 < 1000)) $err[] = 'invalid 1 code value'; 00040 if ($code2 != null) { 00041 if (!(is_int($code2) && $code2 > 99 && $code2 < 1000)) $err[] = 'invalid 2 code value'; 00042 } 00043 if (count($err) > 0) return FUNC4::trace($debug, implode(', ', $err), 1); 00044 else { 00045 $_smtp = new SMTP4; 00046 $ret = true; 00047 do { 00048 if ($result = fgets($conn, $_smtp->BLEN)) { 00049 $resp[] = $result; 00050 $rescode = substr($result, 0, 3); 00051 if (!($rescode == $code1 || $rescode == $code2)) { 00052 $ret = false; 00053 break; 00054 } 00055 } else { 00056 $resp[] = 'can not read'; 00057 $ret = false; 00058 break; 00059 } 00060 } while ($result[3] == '-'); 00061 return $ret; 00062 } 00063 } 00064 00065 function mxconnect($host = null, $port = null, $tout = null, $name = null, $context = null, $debug = null) { 00066 global $_RESULT; 00067 $_RESULT = array(); 00068 if (!FUNC4::is_debug($debug)) $debug = debug_backtrace(); 00069 if (!is_string($host)) FUNC4::trace($debug, 'invalid host type'); 00070 else { 00071 $host = strtolower(trim($host)); 00072 if (!($host != '' && FUNC4::is_hostname($host, true, $debug))) FUNC4::trace($debug, 'invalid host value'); 00073 } 00074 $res = FUNC4::is_win() ? FUNC4::getmxrr_win($host, $arr, $debug) : getmxrr($host, $arr); 00075 $con = false; 00076 if ($res) { 00077 foreach ($arr as $mx) { 00078 if ($con = SMTP4::connect($mx, $port, null, null, null, $tout, $name, $context, null, $debug)) break; 00079 } 00080 } 00081 if (!$con) $con = SMTP4::connect($host, $port, null, null, null, $tout, $name, $context, null, $debug); 00082 return $con; 00083 } 00084 00085 function connect($host = null, $port = null, $user = null, $pass = null, $vssl = null, $tout = null, $name = null, $context = null, $login = null, $debug = null) { 00086 if (!FUNC4::is_debug($debug)) $debug = debug_backtrace(); 00087 global $_RESULT; 00088 $_RESULT = $err = array(); 00089 $_smtp = new SMTP4; 00090 if ($port == null) $port = $_smtp->PORT; 00091 if ($tout == null) $tout = $_smtp->TOUT; 00092 if (!is_string($host)) $err[] = 'invalid host type'; 00093 else { 00094 $host = strtolower(trim($host)); 00095 if (!($host != '' && ($host == 'localhost' || FUNC4::is_ipv4($host) || FUNC4::is_hostname($host, true, $debug)))) $err[] = 'invalid host value'; 00096 } 00097 if (!(is_int($port) && $port > 0)) $err[] = 'invalid port value'; 00098 if ($user != null) { 00099 if (!is_string($user)) $err[] = 'invalid username type'; 00100 else if (($user = FUNC4::str_clear($user)) == '') $err[] = 'invalid username value'; 00101 } 00102 if ($pass != null) { 00103 if (!is_string($pass)) $err[] = 'invalid password type'; 00104 else if (($pass = FUNC4::str_clear($pass)) == '') $err[] = 'invalid password value'; 00105 } 00106 if (($user != null && $pass == null) || ($user == null && $pass != null)) $err[] = 'invalid username/password combination'; 00107 if ($vssl != null) { 00108 if (!is_string($vssl)) $err[] = 'invalid ssl version type'; 00109 else { 00110 $vssl = strtolower($vssl); 00111 if (!($vssl == 'tls' || $vssl == 'ssl' || $vssl == 'sslv2' || $vssl == 'sslv3')) $err[] = 'invalid ssl version value'; 00112 } 00113 } 00114 if (!(is_int($tout) && $tout > 0)) $err[] = 'invalid timeout value'; 00115 if ($name != null) { 00116 if (!is_string($name)) $err[] = 'invalid name type'; 00117 else { 00118 $name = strtolower(trim($name)); 00119 if (!($name != '' && ($name == 'localhost' || FUNC4::is_ipv4($name) || FUNC4::is_hostname($name, true, $debug)))) $err[] = 'invalid name value'; 00120 } 00121 } else $name = '127.0.0.1'; 00122 if ($context != null && !is_resource($context)) $err[] = 'invalid context type'; 00123 if ($login != null) { 00124 $login = strtolower(trim($login)); 00125 if (!($login == 'login' || $login == 'plain' || $login == 'cram-md5')) $err[] = 'invalid authentication type value'; 00126 } 00127 if (count($err) > 0) FUNC4::trace($debug, implode(', ', $err)); 00128 else { 00129 $ret = false; 00130 $prt = ($vssl == null) ? 'tcp' : $vssl; 00131 $conn = ($context == null) ? fsockopen($prt.'://'.$host, $port, $errno, $errstr, $tout) : fsockopen($prt.'://'.$host, $port, $errno, $errstr, $tout, $context); 00132 if (!$conn) $_RESULT[101] = $errstr; 00133 else if (!socket_set_timeout($conn, $_smtp->COUT)) $_RESULT[102] = 'could not set socket timeout'; 00134 else if (!SMTP4::_cres($conn, $resp, 220, null, $debug)) $_RESULT[103] = $resp; 00135 else { 00136 $continue = true; 00137 if (!SMTP4::ehlo($conn, $name, $debug)) $continue = SMTP4::helo($conn, $name, $debug); 00138 if ($continue) { 00139 if ($user == null) $ret = true; 00140 else if ($login != null) $ret = SMTP4::auth($conn, $user, $pass, $login, $debug); 00141 else { 00142 list($code, $arr) = each($_RESULT); 00143 $auth['default'] = $auth['login'] = $auth['plain'] = $auth['cram-md5'] = false; 00144 foreach ($arr as $line) { 00145 if (substr($line, 0, strlen('250-AUTH ')) == '250-AUTH ') { 00146 foreach (explode(' ', substr($line, strlen('250-AUTH '))) as $type) { 00147 $type = strtolower(trim($type)); 00148 if ($type == 'login' || $type == 'plain' || $type == 'cram-md5') $auth[$type] = true; 00149 } 00150 } else if (substr($line, 0, strlen('250 AUTH=')) == '250 AUTH=') { 00151 $expl = explode(' ', strtolower(trim(substr($line, strlen('250 AUTH=')))), 2); 00152 if ($expl[0] == 'login' || $expl[0] == 'plain' || $expl[0] == 'cram-md5') $auth['default'] = $expl[0]; 00153 } 00154 } 00155 if ($auth['default']) $ret = SMTP4::auth($conn, $user, $pass, $auth['default'], $debug); 00156 if (!$ret && $auth['login'] && $auth['default'] != 'login') $ret = SMTP4::auth($conn, $user, $pass, 'login', $debug); 00157 if (!$ret && $auth['plain'] && $auth['default'] != 'plain') $ret = SMTP4::auth($conn, $user, $pass, 'plain', $debug); 00158 if (!$ret && $auth['cram-md5'] && $auth['default'] != 'cram-md5') $ret = SMTP4::auth($conn, $user, $pass, 'cram-md5', $debug); 00159 if (!$ret && !$auth['login'] && $auth['default'] != 'login') $ret = SMTP4::auth($conn, $user, $pass, 'login', $debug); 00160 if (!$ret && !$auth['plain'] && $auth['default'] != 'plain') $ret = SMTP4::auth($conn, $user, $pass, 'plain', $debug); 00161 if (!$ret && !$auth['cram-md5'] && $auth['default'] != 'cram-md5') $ret = SMTP4::auth($conn, $user, $pass, 'cram-md5', $debug); 00162 } 00163 } 00164 } 00165 if (!$ret) { 00166 if (is_resource($conn)) SMTP4::disconnect($conn, $debug); 00167 $conn = false; 00168 } 00169 return $conn; 00170 } 00171 } 00172 00173 function send($conn = null, $addrs = null, $mess = null, $from = null, $debug = null) { 00174 if (!FUNC4::is_debug($debug)) $debug = debug_backtrace(); 00175 global $_RESULT; 00176 $_RESULT = $err = array(); 00177 if (!is_resource($conn)) $err[] = 'invalid resource connection'; 00178 if (!is_array($addrs)) $err[] = 'invalid to address type'; 00179 else { 00180 $aver = true; 00181 if (count($addrs) > 0) { 00182 foreach ($addrs as $addr) { 00183 if (!FUNC4::is_mail($addr)) { 00184 $aver = false; 00185 break; 00186 } 00187 } 00188 } else $aver = false; 00189 if (!$aver) $err[] = 'invalid to address value'; 00190 } 00191 if (!is_string($mess)) $err[] = 'invalid message value'; 00192 if ($from == null) { 00193 $from = @ini_get('sendmail_from'); 00194 if ($from == '' || !FUNC4::is_mail($from)) $from = (isset($_SERVER['SERVER_ADMIN']) && FUNC4::is_mail($_SERVER['SERVER_ADMIN'])) ? $_SERVER['SERVER_ADMIN'] : 'postmaster@localhost'; 00195 } else { 00196 if (!is_string($from)) $err[] = 'invalid from address type'; 00197 else if (!($from != '' && FUNC4::is_mail($from))) $err[] = 'invalid from address value'; 00198 } 00199 if (count($err) > 0) FUNC4::trace($debug, implode(', ', $err)); 00200 else { 00201 $ret = false; 00202 if (SMTP4::from($conn, $from, $debug)) { 00203 $continue = true; 00204 foreach ($addrs as $dest) { 00205 if (!SMTP4::to($conn, $dest, $debug)) { 00206 $continue = false; 00207 break; 00208 } 00209 } 00210 if ($continue) { 00211 if (SMTP4::data($conn, $mess, $debug)) $ret = SMTP4::rset($conn, $debug); 00212 } 00213 } 00214 return $ret; 00215 } 00216 } 00217 00218 function disconnect($conn = null, $debug = null) { 00219 if (!FUNC4::is_debug($debug)) $debug = debug_backtrace(); 00220 global $_RESULT; 00221 $_RESULT = array(); 00222 if (!is_resource($conn)) return FUNC4::trace($debug, 'invalid resource connection', 1); 00223 else { 00224 $_smtp = new SMTP4; 00225 if (!fwrite($conn, 'QUIT'.$_smtp->CRLF)) $_RESULT[300] = 'can not write'; 00226 else $_RESULT[301] = 'Send QUIT'; 00227 return @fclose($conn); 00228 } 00229 } 00230 00231 function quit($conn = null, $debug = null) { 00232 if (!FUNC4::is_debug($debug)) $debug = debug_backtrace(); 00233 global $_RESULT; 00234 $_RESULT = array(); 00235 $ret = false; 00236 $_smtp = new SMTP4; 00237 if (!is_resource($conn)) FUNC4::trace($debug, 'invalid resource connection'); 00238 else if (!fwrite($conn, 'QUIT'.$_smtp->CRLF)) $_RESULT[302] = 'can not write'; 00239 else { 00240 $_RESULT[303] = ($vget = @fgets($conn, $_smtp->BLEN)) ? $vget : 'can not read'; 00241 $ret = true; 00242 } 00243 return $ret; 00244 } 00245 00246 function helo($conn = null, $host = null, $debug = null) { 00247 if (!FUNC4::is_debug($debug)) $debug = debug_backtrace(); 00248 global $_RESULT; 00249 $_RESULT = $err = array(); 00250 if (!is_resource($conn)) $err[] = 'invalid resource connection'; 00251 if (!is_string($host)) $err[] = 'invalid host type'; 00252 else { 00253 $host = strtolower(trim($host)); 00254 if (!($host != '' && ($host == 'localhost' || FUNC4::is_ipv4($host) || FUNC4::is_hostname($host, true, $debug)))) $err[] = 'invalid host value'; 00255 } 00256 if (count($err) > 0) FUNC4::trace($debug, implode(', ', $err)); 00257 else { 00258 $ret = false; 00259 $_smtp = new SMTP4; 00260 if (!fwrite($conn, 'HELO '.$host.$_smtp->CRLF)) $_RESULT[304] = 'can not write'; 00261 else if (!SMTP4::_cres($conn, $resp, 250, null, $debug)) $_RESULT[305] = $resp; 00262 else { 00263 $_RESULT[306] = $resp; 00264 $ret = true; 00265 } 00266 return $ret; 00267 } 00268 } 00269 00270 function ehlo($conn = null, $host = null, $debug = null) { 00271 if (!FUNC4::is_debug($debug)) $debug = debug_backtrace(); 00272 global $_RESULT; 00273 $_RESULT = $err = array(); 00274 if (!is_resource($conn)) $err[] = 'invalid resource connection'; 00275 if (!is_string($host)) $err[] = 'invalid host type'; 00276 else { 00277 $host = strtolower(trim($host)); 00278 if (!($host != '' && ($host == 'localhost' || FUNC4::is_ipv4($host) || FUNC4::is_hostname($host, true, $debug)))) $err[] = 'invalid host value'; 00279 } 00280 if (count($err) > 0) FUNC4::trace($debug, implode(', ', $err)); 00281 else { 00282 $ret = false; 00283 $_smtp = new SMTP4; 00284 if (!fwrite($conn, 'EHLO '.$host.$_smtp->CRLF)) $_RESULT[307] = 'can not write'; 00285 else if (!SMTP4::_cres($conn, $resp, 250, null, $debug)) $_RESULT[308] = $resp; 00286 else { 00287 $_RESULT[309] = $resp; 00288 $ret = true; 00289 } 00290 return $ret; 00291 } 00292 } 00293 00294 function auth($conn = null, $user = null, $pass = null, $type = null, $debug = null) { 00295 if (!FUNC4::is_debug($debug)) $debug = debug_backtrace(); 00296 global $_RESULT; 00297 $_RESULT = $err = array(); 00298 if (!is_resource($conn)) $err[] = 'invalid resource connection'; 00299 if (!is_string($user)) $err[] = 'invalid username type'; 00300 else if (($user = FUNC4::str_clear($user)) == '') $err[] = 'invalid username value'; 00301 if (!is_string($pass)) $err[] = 'invalid password type'; 00302 else if (($pass = FUNC4::str_clear($pass)) == '') $err[] = 'invalid password value'; 00303 if ($type == null) $type = 'login'; 00304 if (!is_string($type)) $err[] = 'invalid authentication type'; 00305 else { 00306 $type = strtolower(trim($type)); 00307 if (!($type == 'login' || $type == 'plain' || $type == 'cram-md5')) $err[] = 'invalid authentication type value'; 00308 } 00309 if (count($err) > 0) FUNC4::trace($debug, implode(', ', $err)); 00310 else { 00311 $ret = false; 00312 $_smtp = new SMTP4; 00313 if ($type == 'login') { 00314 if (!fwrite($conn, 'AUTH LOGIN'.$_smtp->CRLF)) $_RESULT[310] = 'can not write'; 00315 else if (!SMTP4::_cres($conn, $resp, 334, null, $debug)) $_RESULT[311] = $resp; 00316 else if (!fwrite($conn, base64_encode($user).$_smtp->CRLF)) $_RESULT[312] = 'can not write'; 00317 else if (!SMTP4::_cres($conn, $resp, 334, null, $debug)) $_RESULT[313] = $resp; 00318 else if (!fwrite($conn, base64_encode($pass).$_smtp->CRLF)) $_RESULT[314] = 'can not write'; 00319 else if (!SMTP4::_cres($conn, $resp, 235, null, $debug)) $_RESULT[315] = $resp; 00320 else { 00321 $_RESULT[316] = $resp; 00322 $ret = true; 00323 } 00324 } else if ($type == 'plain') { 00325 if (!fwrite($conn, 'AUTH PLAIN '.base64_encode($user.chr(0).$user.chr(0).$pass).$_smtp->CRLF)) $_RESULT[317] = 'can not write'; 00326 else if (!SMTP4::_cres($conn, $resp, 235, null, $debug)) $_RESULT[318] = $resp; 00327 else { 00328 $_RESULT[319] = $resp; 00329 $ret = true; 00330 } 00331 } else if ($type == 'cram-md5') { 00332 if (!fwrite($conn, 'AUTH CRAM-MD5'.$_smtp->CRLF)) $_RESULT[200] = 'can not write'; 00333 else if (!SMTP4::_cres($conn, $resp, 334, null, $debug)) $_RESULT[201] = $resp; 00334 else { 00335 if (strlen($pass) > 64) $pass = pack('H32', md5($pass)); 00336 if (strlen($pass) < 64) $pass = str_pad($pass, 64, chr(0)); 00337 $pad1 = substr($pass, 0, 64) ^ str_repeat(chr(0x36), 64); 00338 $pad2 = substr($pass, 0, 64) ^ str_repeat(chr(0x5C), 64); 00339 $chal = substr($resp[count($resp)-1], 4); 00340 $innr = pack('H32', md5($pad1.base64_decode($chal))); 00341 if (!fwrite($conn, base64_encode($user.' '.md5($pad2.$innr)).$_smtp->CRLF)) $_RESULT[202] = 'can not write'; 00342 else if (!SMTP4::_cres($conn, $resp, 235, null, $debug)) $_RESULT[203] = $resp; 00343 else { 00344 $_RESULT[204] = $resp; 00345 $ret = true; 00346 } 00347 } 00348 } 00349 return $ret; 00350 } 00351 } 00352 00353 function from($conn = null, $addr = null, $debug = null) { 00354 if (!FUNC4::is_debug($debug)) $debug = debug_backtrace(); 00355 global $_RESULT; 00356 $_RESULT = $err = array(); 00357 if (!is_resource($conn)) $err[] = 'invalid resource connection'; 00358 if (!is_string($addr)) $err[] = 'invalid from address type'; 00359 else if (!($addr != '' && FUNC4::is_mail($addr))) $err[] = 'invalid from address value'; 00360 if (count($err) > 0) FUNC4::trace($debug, implode(', ', $err)); 00361 else { 00362 $ret = false; 00363 $_smtp = new SMTP4; 00364 if (!fwrite($conn, 'MAIL FROM:<'.$addr.'>'.$_smtp->CRLF)) $_RESULT[320] = 'can not write'; 00365 else if (!SMTP4::_cres($conn, $resp, 250, null, $debug)) $_RESULT[321] = $resp; 00366 else { 00367 $_RESULT[322] = $resp; 00368 $ret = true; 00369 } 00370 return $ret; 00371 } 00372 } 00373 00374 function to($conn = null, $addr = null, $debug = null) { 00375 if (!FUNC4::is_debug($debug)) $debug = debug_backtrace(); 00376 global $_RESULT; 00377 $_RESULT = $err = array(); 00378 if (!is_resource($conn)) $err[] = 'invalid resource connection'; 00379 if (!is_string($addr)) $err[] = 'invalid to address type'; 00380 else if (!($addr != '' && FUNC4::is_mail($addr))) $err[] = 'invalid to address value'; 00381 if (count($err) > 0) FUNC4::trace($debug, implode(', ', $err)); 00382 else { 00383 $ret = false; 00384 $_smtp = new SMTP4; 00385 if (!fwrite($conn, 'RCPT TO:<'.$addr.'>'.$_smtp->CRLF)) $_RESULT[323] = 'can not write'; 00386 else if (!SMTP4::_cres($conn, $resp, 250, 251, $debug)) $_RESULT[324] = $resp; 00387 else { 00388 $_RESULT[325] = $resp; 00389 $ret = true; 00390 } 00391 return $ret; 00392 } 00393 } 00394 00395 function data($conn = null, $mess = null, $debug = null) { 00396 if (!FUNC4::is_debug($debug)) $debug = debug_backtrace(); 00397 global $_RESULT; 00398 $_RESULT = $err = array(); 00399 if (!is_resource($conn)) $err[] = 'invalid resource connection'; 00400 if (!(is_string($mess) && $mess != '')) $err[] = 'invalid message value'; 00401 if (count($err) > 0) FUNC4::trace($debug, implode(', ', $err)); 00402 else { 00403 $ret = false; 00404 $_smtp = new SMTP4; 00405 if (!fwrite($conn, 'DATA'.$_smtp->CRLF)) $_RESULT[326] = 'can not write'; 00406 else if (!SMTP4::_cres($conn, $resp, 354, null, $debug)) $_RESULT[327] = $resp; 00407 else { 00408 $continue = true; 00409 foreach (explode($_smtp->CRLF, $mess) as $line) { 00410 if ($line != '' && $line[0] == '.') $line = '.'.$line; 00411 if (!fwrite($conn, $line.$_smtp->CRLF)) { 00412 $_RESULT[328] = 'can not write'; 00413 $continue = false; 00414 break; 00415 } 00416 } 00417 if ($continue) { 00418 if (!fwrite($conn, '.'.$_smtp->CRLF)) $_RESULT[329] = 'can not write'; 00419 else if (!SMTP4::_cres($conn, $resp, 250, null, $debug)) $_RESULT[330] = $resp; 00420 else { 00421 $_RESULT[331] = $resp; 00422 $ret = true; 00423 } 00424 } 00425 } 00426 return $ret; 00427 } 00428 } 00429 00430 function rset($conn = null, $debug = null) { 00431 if (!FUNC4::is_debug($debug)) $debug = debug_backtrace(); 00432 global $_RESULT; 00433 $_RESULT = array(); 00434 $ret = false; 00435 $_smtp = new SMTP4; 00436 if (!is_resource($conn)) FUNC4::trace($debug, 'invalid resource connection'); 00437 else if (!fwrite($conn, 'RSET'.$_smtp->CRLF)) $_RESULT[332] = 'can not write'; 00438 else if (!SMTP4::_cres($conn, $resp, 250, null, $debug)) $_RESULT[333] = $resp; 00439 else { 00440 $_RESULT[334] = $resp; 00441 $ret = true; 00442 } 00443 return $ret; 00444 } 00445 00446 function recv($conn = null, $code1 = null, $code2 = null, $debug = null) { 00447 if (!FUNC4::is_debug($debug)) $debug = debug_backtrace(); 00448 global $_RESULT; 00449 $_RESULT = array(); 00450 $ret = false; 00451 if (!SMTP4::_cres($conn, $resp, $code1, $code2, $debug)) $_RESULT[335] = $resp; 00452 else { 00453 $_RESULT[336] = $resp; 00454 $ret = true; 00455 } 00456 return $ret; 00457 } 00458 00459 } 00460 00461 ?>