我正在尝试使用PHPMailer通过PHP发送电子邮件,目前,我正在localhost尝试测试一切是否正常,但无论我做什么或更改它都无法与Gmail SMTP连接。 / p>
我已经看到很多解决方案和教程,我怎么能在这里实现这一点,这就是我所拥有的:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;
require 'assets/PHPMailer/src/Exception.php';
require 'assets/PHPMailer/src/PHPMailer.php';
require 'assets/PHPMailer/src/SMTP.php';
$mail = new PHPMailer;
// DADOS DO FORM
$name = $_POST["name"];
$email = $_POST["email"];
$msg = $_POST["message"];
$date = date('d-m-Y H:i:s');
$bodyContent = "<h2>Contato de <strong>" . $name . "</strong></h2><br /><h3>E-mail para resposta: <strong>" . $email . "</strong><h3><br /><br /><h5>" . $msg . "</h5><br /><br />Contato enviado em " . $date;
try{
echo '<script>console.log("ENTROU TRY")</script>';
$mail->IsSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->SMTPDebug = 2;
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->Username = 'my-gmail-email@gmail.com';
$mail->Password = '***********';
$mail->CharSet = 'utf-8';
$mail->From = "my-gmail-email@gmail.com";
$mail->FromName = "Myself";
$mail->AddReplyTo($email, $name);
$mail->AddAddress("my-receiving@email.com", "PneuCar");
$mail->IsHTML(true);
$mail->Subject = 'Contato pelo site.';
$mail->Body = $bodyContent;
if(!$mail->send()) {
// IT LOGS SEND TRUE
echo '<script>console.log("SEND TRUE")</script>';
echo '<script type="text/javascript">alert("Contato enviado com sucesso! Aguarde um retorno.");</script>';
//header("location:javascript://history.go(-1)");
} else {
echo '<script>console.log("SEND FALSE")</script>';
//header("location:javascript://history.go(-1)");
echo '<script type="text/javascript">alert("'. $envio .'")</script>';
}
} catch (Exception $e) {
echo '<script>console.log("ENTROU CATCH")</script>';
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
?>
我已尝试使用SSL和TLS的端口465,同时尝试了587。我允许不太安全的应用程序。我已在Chrome上登录我的Gmail帐户。我正在用Apache提供我的文件。我使用的PHPMailer文件直接从他们的Github下载。在提交表单后,我在控制台上得到了这个:
2017-11-01 12:02:09 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP n50sm292637qtc.44 - gsmtp
2017-11-01 12:02:09 CLIENT -> SERVER: EHLO localhost
2017-11-01 12:02:09 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [187.116.118.221]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2017-11-01 12:02:09 CLIENT -> SERVER: STARTTLS
2017-11-01 12:02:09 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
SMTP Error: Could not connect to SMTP host.
2017-11-01 12:02:09 CLIENT -> SERVER: QUIT
2017-11-01 12:02:09
2017-11-01 12:02:09
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
我已经在wiki上搜索任何解决方案,但没有找到任何解决方案。
有人能帮我弄清楚我的代码有问题吗?
当我使用此代码检查连接是否超时时
$checkconn = fsockopen('smtp.gmail.com', 587, $errno, $errstr, 5);
if(!$checkconn){
echo "($errno) $errstr";
} else {
echo 'ok';
}
它返回'ok'。
答案 0 :(得分:0)
打开localhost防火墙上的端口(此邮件脚本所在的位置)