我已经使用 PHP 创建了一个联系表单,并使用 PHPmailer 类通过 SMTP gmail 设置发送邮件。该表单对于其他邮件 ID 工作正常,但是当我尝试更改设置以通过 SMTP gmail 发送邮件时,收到错误消息“SMTP 错误:无法连接到 SMTP 主机。并且 SMTP -> 错误:连接失败”到服务器:连接被拒绝(111)”..下面是正在使用的代码...
require 'PHPMailer/class.phpmailer.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = 'mmuxhssfdrqnsowp'; // Use the App Password you generated
$mail->SMTPSecure = 'tls';
$mail->From = $_POST['email'];
$mail->FromName = $_POST['name'];
$mail->AddAddress('[email protected]', 'HR');
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->AddAttachment($path);
$mail->Subject = 'Application for Programmer Registration';
$mail->Body = $message;
$mail->SMTPDebug = 2;
使用 SMTP Gmail 发送邮件的设置是否存在任何不匹配。
@venkat 我无法发表评论,这就是我将其发布为答案的原因。你的代码在本地运行吗?如果它有效,那么正如您提到的,请联系 go-daddy 客户支持。我曾经遇到过类似的项目之一的问题。他们启用了一些设置,邮件开始工作。
对于 TLS 加密,端口号为 587,对于 SSL 加密,端口号为 465
为了更好地理解,请检查链接https://kinsta.com/blog/gmail-smtp-server/
我正在使用下面的代码,它对我有用。
require("./PHPMailer-master/src/PHPMailer.php");
require("./PHPMailer-master/src/SMTP.php");
$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 0; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com";
$mail->Port = 587; // or 587
$mail->IsHTML(true);
$mail->Username = "[email protected]";
$mail->Password = "mmuxhssfdrqnsowp";
$mail->SetFrom("[email protected]");
$mail->Subject = "Application for Programmer Registration";
$mail->Body = $message;
$mail->AddAddress("[email protected]", "HR");
$mail->AddAttachment( $path , 'filename' );
$headers = "From: Sender\n";
$headers .= 'Content-Type:text/calendar; Content-Disposition: inline; charset=utf-8;\r\n';
$headers .= "Content-Type: text/plain;charset=\"utf-8\"\r\n"; #EDIT: TYPO
if (!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}
尝试使用以下代码。
require_once 'PHPMailer/class-phpmailer.php';
$mail = new PHPMailer();
$options["ssl"] = array(
"verify_peer" => false,
"verify_peer_name" => false,
"allow_self_signed" => true,
);
$mail->SMTPDebug = false;
$mail->isSMTP();
$mail->Host = 'tls://smtp.gmail.com'; // This line was missing.
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = 'mmuxhssfdrqnsowp';
$mail->SMTPSecure = 'tls';
$mail->Port = '587';
$mail->smtpConnect($options); // try with this line.
$mail->setFrom($_POST['email'], $_POST['name']);
$mail->AddAddress('[email protected]');
$mail->isHTML(true);
$mail->Subject = 'Application for Programmer Registration';
$mail->Body = $message;
if (!$mail->send()) {
$finalResponse = 'Message could not be sent. Mailer Error: ' . $mail->ErrorInfo;
} else {
$finalResponse = 'success';
}