Well I’ll show you the way I did using gmail, Phpmailer and Wampserver.
1st enable the ssl_module
in apache. To Enable open the file httpd.conf
apache and look for the following line in the file #LoadModule ssl_module modules/mod_ssl.so
, remove the sign #
to enable.
2º Enable the following extensions in php.ini php_openssl
, php_sockets
and php_smtp
(if you do), in my case you do not. To enable extensions look for them in php.ini and remove the ;
front. Extensions are like this in php.ini ;extension=php_openssl.dll
, ;extension=php_sockets.dll
.
3rd Download Phpmailer on Github, unpack it and take the following classes:
4º Encode.
require_once('class.phpmailer.php'); //chama a classe de onde você a colocou.
$mail = new PHPMailer(); // instancia a classe PHPMailer
$mail->IsSMTP();
//configuração do gmail
$mail->Port = '465'; //porta usada pelo gmail.
$mail->Host = 'smtp.gmail.com';
$mail->IsHTML(true);
$mail->Mailer = 'smtp';
$mail->SMTPSecure = 'ssl';
//configuração do usuário do gmail
$mail->SMTPAuth = true;
$mail->Username = '[email protected]'; // usuario gmail.
$mail->Password = 'suasenhadogmail'; // senha do email.
$mail->SingleTo = true;
// configuração do email a ver enviado.
$mail->From = "Mensagem de email, pode vim por uma variavel.";
$mail->FromName = "Nome do remetente.";
$mail->addAddress("[email protected]"); // email do destinatario.
$mail->Subject = "Aqui vai o assunto do email, pode vim atraves de variavel.";
$mail->Body = "Aqui vai a mensagem, que tambem pode vim por variavel.";
if(!$mail->Send())
echo "Erro ao enviar Email:" . $mail->ErrorInfo;
The first time I ran the above code returned me the following error:
SMTP Error: Could not authenticate
.
To resolve it I went in my email and found the following gmail message.
i.e., gmail blocked my connection attempt from localhost.
to avoid this mistake security settings of gmail and went on the
I checked the settings and activated as in the image below
and tried to resend the email from localhost again, sent it to myself.
and now I’ve sent it to another account of mine.
That was the way I did to send email through localhost.
OBS:
I’m using Wampserver, I think it works on any other server, just know where the server puts the file httpd
of the apache and the php.ini
, and enable modules and extensions.
OBS 2:
Phpmailer classes go in your project.
My answer was based on this tutorial.
What mistake happens my dear ?
– gmsantos
No error happens. I refresh the page and only the header that I placed appears.
– I Wanna Know
Makes
var_dump($enviar);
before you close PHP and see what happens.– Jorge B.
Returns false boolean.
– I Wanna Know
one of the problems is that gmail smtp does not work on port 25 where the function will no longer be able to send, always returning bool false
– Otto
I did everything according to the @Otto reply, restarted apache at the end. I use wampstack, put the pasata "sendamil" in C, put inside htdocs, always changing the path in the file "php.ini" and nothing...
– I Wanna Know
I did several tests and no error appears on the page.
– I Wanna Know
When you tried to send with phpmailer, you remembered to activate the extension
php_openssl.dll
in thephp.ini
? Required for authenticated SMTP sending (e.g., gmail on port 587).– Thomas
I remembered, but it was actually bleached when I went to see.
– I Wanna Know
And that example of gmail that comes with phpmailer in the folder
examples
, gives you what mistake?– Thomas
@Thomas I managed to send using the example that comes inside phpmailer itself.
– I Wanna Know
And Voce needed to change the port on your server for that?
– Maria