PHPMailer 将 Gmail 用作 SMTP 服务器。无法连接到 SMTP 主机。邮件程序错误 : SMTP Error: Could not connect to SMTP host

标签 php ssl smtp gmail phpmailer

我正在尝试使用 phpMailer 通过电子邮件向用户发送确认消息。我的代码是这样的:

<?php
include("class.phpmailer.php");
include("class.smtp.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "ssl://smtp.gmail.com"; // specify main and backup server
$mail->Port = 465; // set the port to use
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "sender@gmail.com"; // your SMTP username or your gmail username
$mail->Password = "mypasswrord"; // your SMTP password or your gmail password
$from = "webmaster@example.com"; // Reply to this email
$to="receiver@yahoo.com"; // Recipients email ID
$name="Jersey Name"; // Recipient's name
$mail->From = $from;
$mail->FromName = "Webmaster"; // Name to indicate where the email came from when the recepient received
$mail->AddAddress($to,$name);
$mail->AddReplyTo($from,"Webmaster");
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = "Sending Email From Php Using Gmail";
$mail->Body = "This Email Send through phpmailer, This is the HTML BODY "; //HTML Body
$mail->AltBody = "This is the body when user views in plain text format"; //Text Body
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>

我已经在 php.ini 中启用了 ssl。

PS> sender@gmail.com 是掩码邮箱,保护隐私。但我确实在那部分放了一个真实的电子邮件地址

最佳答案

在你的 php.ini 中确保你已经取消注释行

extension=php_openssl.dll

关于PHPMailer 将 Gmail 用作 SMTP 服务器。无法连接到 SMTP 主机。邮件程序错误 : SMTP Error: Could not connect to SMTP host,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6168882/

相关文章:

ios - 是否可以将 Voip 推送证书和 APN 推送证书连接成一个证书以用于常规推送或 Voip 推送?

.net - 使用传输安全和证书的 WCF 服务出现 SSL 错误。验证

ssl - WWW::Curl 返回错误 77 但命令行的 Curl 成功

linux - sendmail 452 4.5.3 收件人太多

php - 有没有办法像 PHP 一样自动将表单的输出转换为数组,但使用 jQuery/Javascript?

php - PHP如何发起GET/POST/PUT/DELETE请求并判断请求类型?

Php MySQL 更新一个字段并等待 12 小时

php - 检索数据 HTML 并将其更新到 mysql

smtp - 使用 SMTP 验证来验证电子邮件地址是一种好的做法吗?

python - 使用来自带有 html 正文的 gmail 帐户的 SMTP python 的电子邮件