Example #1
0
 function connect($host = null, $port = null, $user = null, $pass = null, $vssl = null, $tout = null, $name = null, $context = null, $login = null, $debug = null)
 {
     if (!FUNC4::is_debug($debug)) {
         $debug = debug_backtrace();
     }
     global $_RESULT;
     $_RESULT = $err = array();
     $_smtp = new SMTP4();
     if ($port == null) {
         $port = $_smtp->PORT;
     }
     if ($tout == null) {
         $tout = $_smtp->TOUT;
     }
     if (!is_string($host)) {
         $err[] = 'invalid host type';
     } else {
         $host = strtolower(trim($host));
         if (!($host != '' && ($host == 'localhost' || FUNC4::is_ipv4($host) || FUNC4::is_hostname($host, true, $debug)))) {
             $err[] = 'invalid host value';
         }
     }
     if (!(is_int($port) && $port > 0)) {
         $err[] = 'invalid port value';
     }
     if ($user != null) {
         if (!is_string($user)) {
             $err[] = 'invalid username type';
         } else {
             if (($user = FUNC4::str_clear($user)) == '') {
                 $err[] = 'invalid username value';
             }
         }
     }
     if ($pass != null) {
         if (!is_string($pass)) {
             $err[] = 'invalid password type';
         } else {
             if (($pass = FUNC4::str_clear($pass)) == '') {
                 $err[] = 'invalid password value';
             }
         }
     }
     if ($user != null && $pass == null || $user == null && $pass != null) {
         $err[] = 'invalid username/password combination';
     }
     if ($vssl != null) {
         if (!is_string($vssl)) {
             $err[] = 'invalid ssl version type';
         } else {
             $vssl = strtolower($vssl);
             if (!($vssl == 'tls' || $vssl == 'ssl' || $vssl == 'sslv2' || $vssl == 'sslv3')) {
                 $err[] = 'invalid ssl version value';
             }
         }
     }
     if (!(is_int($tout) && $tout > 0)) {
         $err[] = 'invalid timeout value';
     }
     if ($name != null) {
         if (!is_string($name)) {
             $err[] = 'invalid name type';
         } else {
             $name = strtolower(trim($name));
             if (!($name != '' && ($name == 'localhost' || FUNC4::is_ipv4($name) || FUNC4::is_hostname($name, true, $debug)))) {
                 $err[] = 'invalid name value';
             }
         }
     } else {
         $name = '127.0.0.1';
     }
     if ($context != null && !is_resource($context)) {
         $err[] = 'invalid context type';
     }
     if ($login != null) {
         $login = strtolower(trim($login));
         if (!($login == 'login' || $login == 'plain' || $login == 'cram-md5')) {
             $err[] = 'invalid authentication type value';
         }
     }
     if (count($err) > 0) {
         FUNC4::trace($debug, implode(', ', $err));
     } else {
         $ret = false;
         $prt = $vssl == null ? 'tcp' : $vssl;
         $conn = $context == null ? fsockopen($prt . '://' . $host, $port, $errno, $errstr, $tout) : fsockopen($prt . '://' . $host, $port, $errno, $errstr, $tout, $context);
         if (!$conn) {
             $_RESULT[101] = $errstr;
         } else {
             if (!socket_set_timeout($conn, $_smtp->COUT)) {
                 $_RESULT[102] = 'could not set socket timeout';
             } else {
                 if (!SMTP4::_cres($conn, $resp, 220, null, $debug)) {
                     $_RESULT[103] = $resp;
                 } else {
                     $continue = true;
                     if (!SMTP4::ehlo($conn, $name, $debug)) {
                         $continue = SMTP4::helo($conn, $name, $debug);
                     }
                     if ($continue) {
                         if ($user == null) {
                             $ret = true;
                         } else {
                             if ($login != null) {
                                 $ret = SMTP4::auth($conn, $user, $pass, $login, $debug);
                             } else {
                                 list($code, $arr) = each($_RESULT);
                                 $auth['default'] = $auth['login'] = $auth['plain'] = $auth['cram-md5'] = false;
                                 foreach ($arr as $line) {
                                     if (substr($line, 0, strlen('250-AUTH ')) == '250-AUTH ') {
                                         foreach (explode(' ', substr($line, strlen('250-AUTH '))) as $type) {
                                             $type = strtolower(trim($type));
                                             if ($type == 'login' || $type == 'plain' || $type == 'cram-md5') {
                                                 $auth[$type] = true;
                                             }
                                         }
                                     } else {
                                         if (substr($line, 0, strlen('250 AUTH=')) == '250 AUTH=') {
                                             $expl = explode(' ', strtolower(trim(substr($line, strlen('250 AUTH=')))), 2);
                                             if ($expl[0] == 'login' || $expl[0] == 'plain' || $expl[0] == 'cram-md5') {
                                                 $auth['default'] = $expl[0];
                                             }
                                         }
                                     }
                                 }
                                 if ($auth['default']) {
                                     $ret = SMTP4::auth($conn, $user, $pass, $auth['default'], $debug);
                                 }
                                 if (!$ret && $auth['login'] && $auth['default'] != 'login') {
                                     $ret = SMTP4::auth($conn, $user, $pass, 'login', $debug);
                                 }
                                 if (!$ret && $auth['plain'] && $auth['default'] != 'plain') {
                                     $ret = SMTP4::auth($conn, $user, $pass, 'plain', $debug);
                                 }
                                 if (!$ret && $auth['cram-md5'] && $auth['default'] != 'cram-md5') {
                                     $ret = SMTP4::auth($conn, $user, $pass, 'cram-md5', $debug);
                                 }
                                 if (!$ret && !$auth['login'] && $auth['default'] != 'login') {
                                     $ret = SMTP4::auth($conn, $user, $pass, 'login', $debug);
                                 }
                                 if (!$ret && !$auth['plain'] && $auth['default'] != 'plain') {
                                     $ret = SMTP4::auth($conn, $user, $pass, 'plain', $debug);
                                 }
                                 if (!$ret && !$auth['cram-md5'] && $auth['default'] != 'cram-md5') {
                                     $ret = SMTP4::auth($conn, $user, $pass, 'cram-md5', $debug);
                                 }
                             }
                         }
                     }
                 }
             }
         }
         if (!$ret) {
             if (is_resource($conn)) {
                 SMTP4::disconnect($conn, $debug);
             }
             $conn = false;
         }
         return $conn;
     }
 }
Example #2
0
 function send($resc = null, $debug = null)
 {
     global $_RESULT;
     $_RESULT = $err = array();
     if (!FUNC4::is_debug($debug)) {
         $debug = debug_backtrace();
     }
     if (is_resource($resc)) {
         $delivery = 'relay';
     } else {
         if ($resc == null) {
             $resc = 'local';
         }
         if (!is_string($resc)) {
             $err[] = 'invalid connection type';
         } else {
             $resc = strtolower(trim($resc));
             if ($resc == 'local' || $resc == 'client' || $resc == 'sendmail' || $resc == 'qmail') {
                 $delivery = $resc;
             } else {
                 $err[] = 'invalid connection value';
             }
         }
     }
     if (count($this->To) == 0) {
         $err[] = 'to mail address is not set';
     }
     if (!isset($this->Subject['content'])) {
         $err[] = 'mail subject is not set';
     }
     if (!(isset($this->Text['content']) || isset($this->Html['content']))) {
         $err[] = 'mail message is not set';
     }
     if (count($err) > 0) {
         FUNC4::trace($debug, implode(', ', $err));
     } else {
         $header['local'] = $header['client'] = array();
         $body = '';
         $from = null;
         if (isset($this->From['address']) && is_string($this->From['address'])) {
             $from = $this->From['address'];
             $hv = 'From: ';
             if (isset($this->From['name'])) {
                 $hn = MIME4::encode_header($this->From['name'], isset($this->From['charset']) ? $this->From['charset'] : null, isset($this->From['encoding']) ? $this->From['encoding'] : null, null, null, $debug);
                 if ($hn == $this->From['name']) {
                     $hn = '"' . str_replace('"', '\\"', $this->From['name']) . '"';
                 }
                 $hv .= $hn . ' <' . $this->From['address'] . '>';
             } else {
                 $hv .= $this->From['address'];
             }
             $header['local'][] = $hv;
             $header['client'][] = $hv;
         }
         $addrs = $arr = array();
         foreach ($this->To as $to) {
             if (isset($to['address']) && FUNC4::is_mail($to['address'], false, $debug)) {
                 $addrs[] = $to['address'];
                 if (isset($to['name'])) {
                     $hn = MIME4::encode_header($to['name'], isset($to['charset']) ? $to['charset'] : null, isset($to['encoding']) ? $to['encoding'] : null, null, null, $debug);
                     if ($hn == $to['name']) {
                         $hn = '"' . str_replace('"', '\\"', $to['name']) . '"';
                     }
                     $arr[] = $hn . ' <' . $to['address'] . '>';
                 } else {
                     $arr[] = $to['address'];
                 }
             }
         }
         if (count($arr) > 0) {
             $to = implode(', ', $arr);
             $header['client'][] = 'To: ' . implode(', ' . $this->_mime->LE . "\t", $arr);
         } else {
             FUNC4::trace($debug, 'to mail address is not set');
         }
         if (count($this->Cc) > 0) {
             $arr = array();
             foreach ($this->Cc as $cc) {
                 if (isset($cc['address']) && FUNC4::is_mail($cc['address'], false, $debug)) {
                     $addrs[] = $cc['address'];
                     if (isset($cc['name'])) {
                         $hn = MIME4::encode_header($cc['name'], isset($cc['charset']) ? $cc['charset'] : null, isset($cc['encoding']) ? $cc['encoding'] : null, null, null, $debug);
                         if ($hn == $cc['name']) {
                             $hn = '"' . str_replace('"', '\\"', $cc['name']) . '"';
                         }
                         $arr[] = $hn . ' <' . $cc['address'] . '>';
                     } else {
                         $arr[] = $cc['address'];
                     }
                 }
             }
             if (count($arr) > 0) {
                 $header['local'][] = 'Cc: ' . implode(', ', $arr);
                 $header['client'][] = 'Cc: ' . implode(', ' . $this->_mime->LE . "\t", $arr);
             }
         }
         $hbcc = '';
         if (count($this->Bcc) > 0) {
             $arr = array();
             foreach ($this->Bcc as $bcc) {
                 if (FUNC4::is_mail($bcc, false, $debug)) {
                     $arr[] = $bcc;
                     $addrs[] = $bcc;
                 }
             }
             if (count($arr) > 0) {
                 $header['local'][] = 'Bcc: ' . implode(', ', $arr);
                 $hbcc = $this->_mime->LE . 'Bcc: ' . implode(', ', $arr);
             }
         }
         $hn = MIME4::encode_header($this->Subject['content'], isset($this->Subject['charset']) ? $this->Subject['charset'] : null, isset($this->Subject['encoding']) ? $this->Subject['encoding'] : null, null, null, $debug);
         $subject = $hn;
         $header['client'][] = 'Subject: ' . $hn;
         if (is_int($this->Priority) || is_string($this->Priority)) {
             $arr = false;
             if ($this->Priority == 1 || $this->Priority == 'high') {
                 $arr = array(1, 'high');
             } else {
                 if ($this->Priority == 3 || $this->Priority == 'normal') {
                     $arr = array(3, 'normal');
                 } else {
                     if ($this->Priority == 5 || $this->Priority == 'low') {
                         $arr = array(5, 'low');
                     }
                 }
             }
             if ($arr) {
                 $header['local'][] = 'X-Priority: ' . $arr[0];
                 $header['local'][] = 'X-MSMail-Priority: ' . $arr[1];
                 $header['client'][] = 'X-Priority: ' . $arr[0];
                 $header['client'][] = 'X-MSMail-Priority: ' . $arr[1];
             }
         }
         $header['client'][] = 'Message-Id: <' . MIME4::unique() . '@xpertmailer.com>';
         if (count($this->Header) > 0) {
             foreach ($this->Header as $harr) {
                 if (isset($harr['name'], $harr['value']) && strlen($harr['name']) >= 2 && FUNC4::is_alpha($harr['name'], true, '-')) {
                     $hn = MIME4::encode_header($harr['value'], isset($harr['charset']) ? $harr['charset'] : null, isset($harr['encoding']) ? $harr['encoding'] : null, null, null, $debug);
                     $header['local'][] = ucfirst($harr['name']) . ': ' . $hn;
                     $header['client'][] = ucfirst($harr['name']) . ': ' . $hn;
                 }
             }
         }
         $text = $html = $att = null;
         if (isset($this->Text['content'])) {
             $text = MIME4::message($this->Text['content'], 'text/plain', null, isset($this->Text['charset']) ? $this->Text['charset'] : null, isset($this->Text['encoding']) ? $this->Text['encoding'] : null, null, null, null, null, $debug);
         }
         if (isset($this->Html['content'])) {
             $html = MIME4::message($this->Html['content'], 'text/html', null, isset($this->Html['charset']) ? $this->Html['charset'] : null, isset($this->Html['encoding']) ? $this->Html['encoding'] : null, null, null, null, null, $debug);
         }
         if (count($this->Attach) > 0) {
             $att = array();
             foreach ($this->Attach as $attach) {
                 if (isset($attach['content'])) {
                     $att[] = MIME4::message($attach['content'], isset($attach['type']) ? $attach['type'] : null, isset($attach['name']) ? $attach['name'] : null, isset($attach['charset']) ? $attach['charset'] : null, isset($attach['encoding']) ? $attach['encoding'] : null, isset($attach['disposition']) ? $attach['disposition'] : null, isset($attach['id']) ? $attach['id'] : null, null, null, $debug);
                 }
             }
             if (count($att) == 0) {
                 $att = null;
             }
         }
         $arr = MIME4::compose($text, $html, $att);
         if ($delivery == 'relay') {
             $res = SMTP4::send($resc, $addrs, implode($this->_mime->LE, $header['client']) . $this->_mime->LE . $arr['header'] . $this->_mime->LE . $this->_mime->LE . $arr['content'], $this->Path != null ? $this->Path : $from, $debug);
             return $this->_result($_RESULT, $res);
         } else {
             if ($delivery == 'local') {
                 $rpath = !FUNC4::is_win() && $this->Path != null ? '-f ' . $this->Path : null;
                 $spath = $this->Path != null ? @ini_set('sendmail_from', $this->Path) : false;
                 $res = mail($to, $subject, $arr['content'], implode($this->_mime->LE, $header['local']) . $this->_mime->LE . $arr['header'], $rpath);
                 if ($spath) {
                     @ini_restore('sendmail_from');
                 }
                 return $this->_result(array(0 => 'send mail local'), $res);
             } else {
                 if ($delivery == 'client') {
                     $group = array();
                     foreach ($addrs as $addr) {
                         $exp = explode('@', $addr);
                         $group[strtolower($exp[1])][] = $addr;
                     }
                     $ret = true;
                     $reg = count($group) == 1;
                     foreach ($group as $domain => $arrs) {
                         $con = SMTP4::mxconnect($domain, $this->Port, $this->Tout, $this->Name, $this->Context, $debug);
                         if ($reg) {
                             $this->_result(array($domain => $_RESULT));
                         }
                         if ($con) {
                             if (!SMTP4::send($con, $arrs, implode($this->_mime->LE, $header['client']) . $this->_mime->LE . $arr['header'] . $this->_mime->LE . $this->_mime->LE . $arr['content'], $this->Path != null ? $this->Path : $from, $debug)) {
                                 $ret = false;
                             }
                             if ($reg) {
                                 $this->_result(array($domain => $_RESULT));
                             }
                             SMTP4::disconnect($con, $debug);
                         } else {
                             $ret = false;
                         }
                     }
                     if (!$reg) {
                         $this->_result(array(0 => 'send mail client'));
                     }
                     return $ret;
                 } else {
                     if ($delivery == 'sendmail' || $delivery == 'qmail') {
                         $ret = false;
                         $comm = ($delivery == 'sendmail' ? $this->SendMail : $this->QMail) . ' -oi' . ($this->Path != null ? ' -f ' . $this->Path : '') . ' -t';
                         if ($con = popen($comm, 'w')) {
                             if (fputs($con, implode($this->_mime->LE, $header['client']) . $hbcc . $this->_mime->LE . $arr['header'] . $this->_mime->LE . $this->_mime->LE . $arr['content'])) {
                                 $res = pclose($con) >> 8 & 0xff;
                                 if ($res == 0) {
                                     $ret = true;
                                     $this->_result(array(0 => 'send mail using "' . ucfirst($delivery) . '" program'));
                                 } else {
                                     $this->_result(array(0 => $res));
                                 }
                             } else {
                                 $this->_result(array(0 => 'can not write'));
                             }
                         } else {
                             $this->_result(array(0 => 'can not write line command'));
                         }
                         return $ret;
                     }
                 }
             }
         }
     }
 }