Question:
I've been working with PHPMailer for a while, now I'm inserting it into a more complex Content Manager and through the panel the person can insert multiple emails to be the PHPMailer Sender in various situations (for example: contact email, password recovery , registration confirmation…), then in the manager the person can modify the email access data (host, email, password and port) and save, but when saving I wanted to do a validation and only accept emails with the data that are working!
I needed a method to do a setup test!
Does anyone know this method inside PHPMailer, that it just makes a connection test, but doesn't 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 = "teste@gabrielprogramador.com.br";
$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!
Answer:
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: https://github.com/PHPMailer/PHPMailer/blob/master/examples/smtp_check.phps