php PHPMailer 使用 Gmail 作为 SMTP 服务器。无法连接到 SMTP 主机。邮件程序错误:SMTP 错误:无法连接到 SMTP 主机

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6168882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:33:18  来源:igfitidea点击:

PHPMailer to use Gmail as SMTP server.Could not connect to SMTP host. Mailer Error: SMTP Error: Could not connect to SMTP host

phpsslsmtpgmailphpmailer

提问by phpDeveloperForThesis

i am trying to use phpMailer to send confirmation messages to users via email. my code is this:

我正在尝试使用 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 = "[email protected]"; // your SMTP username or your gmail username
$mail->Password = "mypasswrord"; // your SMTP password or your gmail password
$from = "[email protected]"; // Reply to this email
$to="[email protected]"; // 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";
}
?>

i already enabled ssl in php.ini.

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

PS> [email protected] is a mask email to protect the privacy. but i did put a true email address in that part

PS> [email protected] 是保护隐私的屏蔽邮件。但我确实在那部分放了一个真实的电子邮件地址

回答by jerome

in you php.ini make sure you have uncommented the line with

在您的 php.ini 中,请确保您已取消注释该行

extension=php_openssl.dll

回答by Mridul Kashatria

From here

2) Comment out the following lines of code in class.phpmailer.php

这里

2) 注释掉 class.phpmailer.php 中的以下代码行

/*
if(strstr($hosts[$index], ":"))
list($host, $port) = explode(":",
$hosts[$index]); else 
*/

Try this if you haven't already.

如果你还没有试试这个。

回答by Nemo

This pagehas code the author claims "worked flawlessly".

该页面包含作者声称“完美运行”的代码。

The only real difference I see between his and yours is that his has:

我看到他和你的唯一真正的区别是他有:

$mail->Mailer = "smtp";

I guess I would start with his code exactly to see if it works, and then debug from there.

我想我会从他的代码开始,看看它是否有效,然后从那里进行调试。

回答by macieq

wrong:

错误的:

$mail->Host = "ssl://smtp.gmail.com"; // specify main and backup server

good:

好的:

$mail->Host = "smtp.gmail.com"; // specify main and backup server