简体   繁体   English

phpmailer SMTP connect() 在本地主机上失败

[英]phpmailer SMTP connect() failed on localhost

I'm running php5.6 on a localhost using xampp, and I'm trying to send a message using phpmailer, but it gives me this error : SMTP connect() failed.我正在使用 xampp 在本地主机上运行 php5.6,并且我正在尝试使用 phpmailer 发送消息,但它给了我这个错误:SMTP connect() 失败。 Here's my code:这是我的代码:

require("Photo Gallery/includes/PHPMailer/PHPMailerAutoload.php");
$mail = new PHPMailer();  
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Username = "aldemeery@gmail.com";
$mail->Password = "*********";
$mail->From     = "aldemeery@gmail.com";
$mail->AddAddress("aldemeery@gmail.com");
$mail->Subject  = "Subject";
$mail->Body     = "Hi!";
if(!$mail->Send()) {
    echo 'Message was not sent.';
    echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent.';
}

I know there are tens of questions out there having the same title, but believe me none of them worked for me.Please help, I'v been trying to solve this all day long!我知道有几十个问题具有相同的标题,但相信我没有一个对我有用。请帮助,我一整天都在努力解决这个问题!

OK...so i'v made some changes to php.ini, my gmail account, and my PHP script...and then everything worked just fine....so here is what i changed:好的...所以我对 php.ini、我的 gmail 帐户和我的 PHP 脚本进行了一些更改...然后一切正常....所以这是我更改的内容:

  • In php.ini: I removed the semicolon in front of "sendmail_from = postmaster@localhost" and restarted xampp.在 php.ini 中:我删除了“sendmail_from = postmaster@localhost”前面的分号并重新启动了 xampp。
  • In my gmail account: in the security settings , I turned on "Allow less secure apps" .在我的 Gmail 帐户中:在安全设置中,我打开了“允许安全性较低的应用程序”
  • And finally in my PHP script: I added two functions:最后在我的 PHP 脚本中:我添加了两个函数:
    • "date_default_timezone_set('Etc/UTC')" "date_default_timezone_set('Etc/UTC')"
    • "gethostbyname('ssl://smtp.gmail.com')" as a value for " $mail->Host" “gethostbyname('ssl://smtp.gmail.com')”作为“$mail->Host”的值

And here is my modified php script这是我修改后的 php 脚本

date_default_timezone_set('Etc/UTC');
require '../PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = gethostbyname('ssl://smtp.gmail.com');
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->Username = "username@gmail.com";
$mail->Password = "***********";
$mail->setFrom('username@gmail.com', 'First Last');
$mail->addAddress('username@yahoo.com', 'First Last');
$mail->Subject = 'PHPMailer SMTP test';
$mail->AltBody = 'This is a plain-text message body';
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}

check your antivirus apps, in my case "Avast Anti Virus" block it.检查您的防病毒应用程序,在我的情况下“Avast Anti Virus”阻止它。 then when i disabled it, the error gone.然后当我禁用它时,错误消失了。

Have you gone into your GMail account and enabled the feature to allow for less secure apps to access it? 您是否已进入GMail帐户并启用了该功能以允许安全性较低的应用程序访问它? If not, check this out: 如果没有,请检查以下内容:

https://support.google.com/accounts/answer/6010255?hl=en https://support.google.com/accounts/answer/6010255?hl=zh_CN

Alternatively, try this as your SMTP: 或者,尝试将此作为您的SMTP:

ssl://smtp.googlemail.com ssl://smtp.googlemail.com

I have found the above to be the most useful. 我发现以上内容最有用。

You could also try and include the file instead of require it, thus try changing, 您也可以尝试添加文件而不是添加文件,从而尝试更改,

require("Photo Gallery/includes/PHPMailer/PHPMailerAutoload.php");

include 'Photo Gallery/includes/PHPMailer/PHPMailerAutoload.php';

This might work. 这可能有效。 If nothing above works, try moving the entire library to the root folder of your project. 如果以上方法均无效,请尝试将整个库移至项目的根文件夹。 It might be the space in your URL that is causing the issue. 可能是您网址中的空格引起了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM