Well I'll show you the way I did using gmail, PHPMailer and WampServer.
1st enable ssl_module
in apache. For Enable, open the file httpd.conf
of apache and look for the following line in the #LoadModule ssl_module modules/mod_ssl.so
file, remove the #
to enable.
2º Enable the following extensions in php.ini php_openssl
, php_sockets
and php_smtp
(if it has), in my case it does not. To enable extensions look for them in php.ini and remove ;
from the front. The extensions are thus in php.ini ;extension=php_openssl.dll
, ;extension=php_sockets.dll
.
3rd Download PHPMailer in the GitHub , unzip it and take the following classes:
4ºCode.
require_once('class.phpmailer.php');//chamaaclassedeondevocêacolocou.$mail=newPHPMailer();//instanciaaclassePHPMailer$mail->IsSMTP();//configuraçãodogmail$mail->Port='465';//portausadapelogmail.$mail->Host='smtp.gmail.com';$mail->IsHTML(true);$mail->Mailer='smtp';$mail->SMTPSecure='ssl';//configuraçãodousuáriodogmail$mail->SMTPAuth=true;$mail->Username='[email protected]';//usuariogmail.$mail->Password='suasenhadogmail';//senhadoemail.$mail->SingleTo=true;//configuraçãodoemailaverenviado.$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 rounded the code above I returned the following error:
SMTP Error: Could not authenticate
.
To solve it I went in my email and found the following message from gmail.
Inotherwords,gmailblockedmyconnectionattemptfromlocalhost.
Toavoidthiserror,goto security settings from gmail and went to the
Iaccessedthesettingsandactivatedasintheimagebelow
and I tried to resend the email from localhost again, I sent it to myself.
AndnowI'vesentittoanotheraccountofmine.
This was the way I did to send email through localhost.
OBS:
I'm using WampServer, I believe it works on any other server, it's just knowing where the server puts the file httpd
apache and php.ini
, and enable modules and extensions.
OBS 2:
PHPMailer classes go in your project.
My response was based on this tutorial .