Wamp Server how to send an email using a gmail account using a php on the localhost?

Asked

Viewed 480 times

0

I’m having a problem sending an email with user registration validation code from my localhost. On my localhost I have Wamp Server installed configured to use port 80, I have redirected all external access to my host’s IP to port 80 so it is possible to access the server externally for access to the database etc... I did several searches on the internet of how to do this I found tutorials using Sendmail that did not give anything or want an error just said that the email was sent, but when checking the inbox of the email nothing. I found Phpmailer who at least presented an error that was:

SERVER -> CLIENT: SMTP NOTICE: EOF Caught while checking if Connected SMTP error: Unable to perform authentication. SMTP Connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting Error: SMTP Connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

NOTE: I did everything that the tutorials said I even tried to use an example for gmail that comes along with the class when you download on Github. Could someone give me a solution or alternative that is not paid for? Below my php code:

<?php
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->setLanguage('pt');
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';

$host = 'smtp.gmail.com';
$username = '[email protected]';
$password = 'minha_senha';
$port = 465;
$secure = 'ssl';

$from = $username;
$fromName = 'Meu Nome Completo';

$mail->IsSMTP();
$mail->Host = $host;
$mail->SMTPAuth = true;
$mail->Username = $username;
$mail->Password = $password;
$mail->Port = $port;
$mail->SMTPSecure = $secure;

$mail->From = $from;
$mail->FromName = $fromName;
$mail->addReplyTo($from, $fromName);

$mail->addAddress('destinatario@qualquer_dominio.com', 'Nome do Destinatario');

$mail->isHTML(true);
$mail->CharSet = 'utf-8';
$mail->WordWrap = 50;

$mail->Subjet = 'Testando phpmail';
$mail->Body = 'Enviando emails com <b>PHPMailer</b> para <h2>TESTE</h2>';
$mail->AltBody = 'Enviando emails com PHPMailer para TESTE';

$send = $mail->Send();

if($send) {
    echo 'E-mail enviado com sucesso!';
} else {
    echo 'Erro: ' . $mail->ErrorInfo;
}

?>
  • The error is in authentication. Have you tried port 587? The TLS order is a little different on 587. One more detail: nowadays you can not use google accounts via SMTP with user and password, need to use user + token (SASL) instead, unless it is enabled the option to allow "less secure applications" in the account. Better check this out too.

  • Hi tried in every possible way by port 587 appears more errors yet the least error option was to 465 using ssl. Regarding the google account I enabled everything I found in my searches including access by less secure app.

1 answer

1

Your code worked here in my test, the only thing I changed was how to do the include of the Phpmailerautoload.php class.

require './PHPMailer/PHPMailerAutoload.php';

Whereas the Phpmailer directory is in the same directory as my.php file.

I recommend that you use a debug to detect errors, it is easier to understand where the problem is, so you can use the Debug of an IDE, such as Netbeans for example. I recommend the editor Atom, and the php debug plugin, in my case was what worked best.

  • I think it has to do with the quality of the internet connection because I used a better debug and returned this: Connection: Opening to ssl://smtp.gmail.com:465, timeout=300, options=array () Connection: opened SMTP -> get_lines(): $data is "" SMTP -> get_lines(): $str is "" SERVER -> CLIENT: SMTP NOTICE: EOF Caught while checking if Connected Connection: closed SMTP error: Could not perform authentication. SMTP Connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting.

Browser other questions tagged

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