Problem for sending Email with PHPMAILER using a GMAIL account for sending

Asked

Viewed 518 times

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";
}

Upshot

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.

1 answer

0

Friend,

Configure Postfix which is easier:

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 a few questions. Just select the server as Internet Site and for FQDN something like mail.dominio.com.br

Open the Postfix configuration file:

vim /etc/postfix/main.cf

Insert the following lines into 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 Gmail user and password information:

vim /etc/postfix/sasl_passwd

Add the following line to sasl_passwd:

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

*You do not need to inform @gmail.com about 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 performed a Postfix re-start:

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 unlock the captcha google, access: https://www.google.com/accounts/DisplayUnlockCaptcha

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: http://novustec.com.br/artigos/configurando_postfix_gmail_smtp

Browser other questions tagged

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