1
I have a website on my localhost
and would like to be able to send emails. I tried with the function mail()
but I couldn’t. So I found a way using PHPMailer
.
I have the following code
<?php
$oc=$_POST['razao'];
$rua=$_POST['rua'];
$freg=$_POST['freg'];
$texto = "
<h4>Declaração de uma Ocorrência</h4>
<br/>
<br/>
Ocorrência: ".$oc."<br/></br/>
Rua: ".$rua." da freguesia de ".$freg.";
";
require '../mail/PHPMailerAutoload.php';
echo "<script>console.log('".$texto."')</script>";
$mail = new PHPMailer();
$mail->CharSet="utf-8";
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com"; // specify main and backup server
$mail->Username = 'EMAIL'; // SMTP username
$mail->Password = 'PASS'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->From = "[email protected]";
$mail->FromName = "União de Freguesias de Antas e Abade de Vermoim";
$mail->AddAddress("[email protected]", "Bruno Moutinho");
$mail->IsHTML(true);
$mail->Subject = "Declaração de uma Ocorrência";
$mail->Body = $texto;
if(!$mail->Send())
{
echo "<script>console.log('".$mail->ErrorInfo."')</script>";
exit;
}else{
echo "<script>console.log('Sucesso')</script>";
}
?>
All variables get there right. Sending is done by ajax
but I haven’t been able to because it gives me an error in the console: SMTP connect() failed.
.
@LINQ I changed, still not giving
– I_like_trains
Have you already released access to the least secure apps in Gmail? - https://www.google.com/settings/security/lesssecureapps
– Jéf Bueno
@LINQ I did it now, still not working, same mistake
– I_like_trains
Have you tried using your gmail email as the sender?
– Sam
It must be a problem with the ssl certificate (since you have enabled the Less Secure gmail apps). To confirm try changing the debug mode to
$mail->SMTPDebug = 2
. Place this immediately after instantiating Phpmailer. You should see an error related to failure in ssl certificate– Juven_v
@Juven_v I did what you said, I didn’t make that mistake, I made the same mistake.
– I_like_trains
@DVD I did what I said, it makes the same mistake.
– I_like_trains