Test connection with Phpmailer

Asked

Viewed 1,470 times

3

I’ve been working with Phpmailer for a while, now I’m inserting it into a more complex Content Manager and through the panel one can insert several emails to be the Phpmailer Sender in several situations (for example: contact email, password recovery, registration confirmation...), so in the manager the person can modify the email access data (host, email, password and port) and save, but when saving wanted to do a validation and only accept emails with the data that are working!

I needed a method to do a configuration test!

Does anyone know this method within Phpmailer, let it just do a connection test, but do not send any email in the act of testing.

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 same tests!

1 answer

1


You can use the class SMTP Phpmailer 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: https://github.com/PHPMailer/PHPMailer/blob/master/examples/smtp_check.phps

  • 1

    I’ll test that then, thank you!

  • Marcos, it validates tls and connect, but every time it arrives at authenticate() always error. If I use the same data in normal sending it sends the email.

  • @Gabrielgarcia can update your question with the code you’re using to test the connection?

  • I edited the question!

  • @Gabrielgarcia tested the location and if you don’t send the EHLO command you can’t authenticate. I updated my response with the code to send the EHLO command after starting TLS. PS: Don’t forget to remove this test user

  • Solved my problems Marcos, show! Thank you very much!

  • @Gabrielgarcia :)

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.