Connection test with PHPMailer

3

Working with PHPMailer at one time, I'm now inserting you into a more complex Content Manager and through the panel the person can insert multiple emails to be the PHPMailer Sender in several situations (for example: contact email, password recovery, confirmation of registration ...), then in the manager the person can modify the access data of the email (host, email, password and port) and save, but when saving wanted to do a validation and only accept emails with the data that is working!

I needed a method of doing a configuration test!

Someone knows this method inside PHPMailer, that it just does a connection test, but does not send any email in the test act.

The code I'm trying to use is this:

    // Inclui o arquivo class.phpmailer.php localizado na pasta phpmailer
    require_once("phpmailer/class.phpmailer.php");
    require_once("phpmailer/class.smtp.php");

    $host = "mx1.weblink.com.br";
    $porta = 587;
    $email = "[email protected]";
    $senha = "teste123";

    $smtp = new SMTP;

    if ( !$smtp->connect($host, $porta) ) {
        // erro ao conectar
        echo 'Erro ao conectar o SMTP';
    }

    if ( !$smtp->startTLS() ) {
        // erro ao iniciar TLS
        echo 'Erro ao iniciar o TLS';
    }

    if ( !$smtp->authenticate($email, $senha) ) {
        // erro ao autenticar o usuário
        echo 'Erro ao autenticar o usuário de e-mail e senha';
    }

The host, and the access data are real, I created to do these tests myself!

    
asked by anonymous 01.04.2016 / 10:23

1 answer

1

You can use PHPMailer's SMTP class to check the connection:

require 'PHPMailerAutoload.php';

$smtp = new SMTP;

if ( !$smtp->connect( 'host', 'porta' ) ) {
    // erro ao conectar
}

if ( !$smtp->startTLS() ) {
    // erro ao iniciar TLS
}

// Necessário enviar o comando EHLO após iniciar o TLS,
// caso contrário não será possível autenticar.
if ( !$smtp->hello(gethostname()) ) {
    // erro ao enviar o comando EHLO
}

if ( !$smtp->authenticate( 'usuario', 'senha' ) ) {
    // erro ao autenticar o usuário
}

source: link

    
01.04.2016 / 17:50