Problem sending mail with PHPMAILER using a GMAIL account for sending [closed]

2

Code

require("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Charset = 'utf-8';
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = 'senha';
$mail->From = '[email protected]';
$mail->FromName = 'Eu';
$mail->SMTPDebug = 1;
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->Subject = 'Teste';
$mail->AddAddress('[email protected]', 'toemail');
$mail->MsgHTML("Segue em anexo o contrato");
$enviado = $mail->Send();
if ($enviado) {
    echo "ok";
} else {
    echo "erro";
}

Result

SMTP -> ERROR: Failed to connect to server: (0) 
SMTP Error: Could not connect to SMTP host. erro

Account Settings:

Permitir aplicativos menos seguros: https://myaccount.google.com/lesssecureapps
Permitir acesso à sua Conta do Google: https://accounts.google.com/b/0/DisplayUnlockCaptcha
Desativar verificação em duas etapas ou criar uma senha para o e-mail https://support.google.com/accounts/answer/1064203?hl=pt-BR


* Testes:

ssl na porta 465 ou com tls na porta 587 (Falha nos 2),
mesmo com as configurações na conta de envio o erro persiste.
    
asked by anonymous 08.12.2017 / 13:01

1 answer

0

Friend,

Set up the easiest Postfix:

Configuring Postfix with Gmail: Installing essential packages:

sudo apt-get install postfix mailutils libsasl2-2 ca-certificates libsasl2-modules

* If you do not have Postfix installed, the installation wizard will ask you some questions. Just select the server as Internet Site and for FQDN something like mail.domain.com

Open the Postfix configuration file:

vim /etc/postfix/main.cf

Enter the following lines in main.cf:

relayhost = [smtp.gmail.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
smtp_use_tls = yes

Now let's create the files that contain the Gmail user information and password:

vim /etc/postfix/sasl_passwd

Add the following line in sasl_passwd:

[smtp.gmail.com]:587 USERNAME:PASSWORD

* You do not need to provide @ gmail.com on the user, if you use Google App's domain, enter [email protected].

Now let's set the permissions:

sudo chmod 400 /etc/postfix/sasl_passwd
sudo postmap /etc/postfix/sasl_passwd

We perform a Postfix restart:

sudo /etc/init.d/postfix reload
Ready! Now let's go to the Test.

Testing First and very important!

To avoid the following error: Error: "SASL authentication failed; server smtp.gmail.com "

We must unblock the google captcha, go to: link

Now let's send a test email:

echo "Assunto: Testando E-mail" | mail -s "Corpo: Teste." [email protected]
Ready! You should already receive the test email in your inbox!

If you do not receive the email, you can follow the errors in the following log file:

tail -f /var/log/mail.log

Source: link

    
09.12.2017 / 14:09