示例#1
0
文件: functions.php 项目: MrWnn/cacti
function ping_mail_server($host, $port, $user, $password, $timeout = 5, $secure = 'none')
{
    global $config;
    include_once $config['base_path'] . '/lib/PHPMailer/PHPMailerAutoload.php';
    //Create a new SMTP instance
    $smtp = new SMTP();
    if ($secure != 'tls' && $secure != 'none') {
        $smtp->SMTPSecure = $secure;
        if (substr_count($host, ':') == 0) {
            $host = $secure . '://' . $host;
        }
    }
    //Enable connection-level debug output
    $smtp->do_debug = 0;
    //$smtp->do_debug = SMTP::DEBUG_LOWLEVEL;
    $results = true;
    try {
        //Connect to an SMTP server
        if ($smtp->connect($host, $port, $timeout)) {
            //Say hello
            if ($smtp->hello(gethostbyname(gethostname()))) {
                //Put your host name in here
                //Authenticate
                if ($smtp->authenticate($user, $password)) {
                    $results = true;
                } else {
                    throw new Exception('Authentication failed: ' . $smtp->getLastReply());
                }
            } else {
                throw new Exception('HELO failed: ' . $smtp->getLastReply());
            }
        } else {
            throw new Exception('Connect failed');
        }
    } catch (Exception $e) {
        $results = 'SMTP error: ' . $e->getMessage();
    }
    //Whatever happened, close the connection.
    $smtp->quit(true);
    return $results;
}
//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');
require '../PHPMailerAutoload.php';
//Create a new SMTP instance
$smtp = new SMTP();
//Enable connection-level debug output
$smtp->do_debug = SMTP::DEBUG_CONNECTION;
try {
    //Connect to an SMTP server
    if ($smtp->connect('lima.pegasushosting.nl', 995)) {
        //Say hello
        if ($smtp->hello('localhost')) {
            //Put your host name in here
            //Authenticate
            if ($smtp->authenticate('username', 'password')) {
                echo "Connected ok!";
            } else {
                throw new Exception('Authentication failed: ' . $smtp->getLastReply());
            }
        } else {
            throw new Exception('HELO failed: ' . $smtp->getLastReply());
        }
    } else {
        throw new Exception('Connect failed');
    }
} catch (Exception $e) {
    echo 'SMTP error: ' . $e->getMessage(), "\n";
}
//Whatever happened, close the connection.
$smtp->quit(true);
示例#3
0
 public function checkSmtp()
 {
     //Create a new SMTP instance
     $smtp = new \SMTP();
     //Enable connection-level debug output
     $smtp->do_debug = \SMTP::DEBUG_CONNECTION;
     try {
         //Connect to an SMTP server
         if ($smtp->connect($this->getSmtpServer()->getSmtpHost(), $this->getSmtpServer()->getSmtpPort())) {
             //Say hello
             if ($smtp->hello($this->smtp->getSmtpHost())) {
                 //Put your host name in here
                 //Authenticate
                 dump($this->getSmtpServer()->getSmtpUsername());
                 dump($this->getSmtpServer()->getSmtpPassword());
                 if ($smtp->authenticate($this->getSmtpServer()->getSmtpUsername(), $this->getSmtpServer()->getSmtpPassword())) {
                     return true;
                 } else {
                     throw new \Exception('Authentication failed: ' . $smtp->getLastReply());
                 }
             } else {
                 throw new \Exception('HELO failed: ' . $smtp->getLastReply());
             }
         } else {
             throw new \Exception('Connect failed');
         }
     } catch (\Exception $e) {
         throw new \Exception('SMTP error: ' . $e->getMessage());
     }
     //Whatever happened, close the connection.
     $smtp->quit(true);
 }