PHPMailer 无法发送电子邮件 [英] PHPMailer cannot send email

查看:50
本文介绍了PHPMailer 无法发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目包含一个发送电子邮件的功能,是PHPMailer.从我的本地主机服务器发送电子邮件运行良好,但今天停止发送电子邮件,现在显示此错误消息

My project contains a function to send email, being PHPMailer. It runs well to send email from my localhost server, but it stopped sending email today, and now it shows this error message

SMTP Error: Could not connect to SMTP host.

我添加了此代码 $mail->SMTPDebug = 1; 以查看调试错误,现在向我显示此消息:

I added this code $mail->SMTPDebug = 1; to view debug errors and is now showing me this message:

SMTP ERROR: Failed to connect to server: php_network_getaddresses: getaddrinfo failed: The requested name is valid, but no data of the requested type was found.  (0) SMTP Error: Could not connect to SMTP host

我已经在 php.ini 中设置了 enabled extension=php_openssl.dll.

I already have enabled extension=php_openssl.dll in php.ini.

这是我的代码:

$mail = new PHPMailer();
$mail->SMTPSecure = 'ssl';
$mail->IsSMTP();
$mail->Username = "myemail@gmail.com"; // your GMail user name
$mail->Password = "password"; 
$mail->AddAddress($email); // recipients email 
$mail->FromName = "username"; // readable name
$mail->IsHTML(true);
$mail->Subject = "title";
$mail->Body    = " Message"; 
$mail->SMTPDebug = 1; 
$mail->Host = "ssl://smtp.gmail.com"; // GMail
$mail->Port = 465;
$mail->IsSMTP(); // use SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->From = $mail->Username;
if($mail->Send()){
} else {
}

谢谢

推荐答案

这通常被报告为 PHPMailer 问题(并且这个问题有很多重复),但它几乎总是归结为本地 DNS 故障、防火墙阻止或其他您本地网络的网络问题.

This is commonly reported as a PHPMailer problem (and there are many duplicates of this question), but it's almost always down to local DNS failure, firewall blocking or other network issue on your local network.

首先,确保您使用的是最新的 PHPMailer.

First, make sure you are using the latest PHPMailer.

不要在端口 465 上使用 SSL,它已自 1998 年起弃用 并且仅使用由没有得到备忘录的微软产品;改为在端口 587 上使用 TLS:

Don't use SSL on port 465, it's been deprecated since 1998 and is only used by Microsoft products that didn't get the memo; use TLS on port 587 instead:

$mail->SMTPSecure = 'tls';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;

或更简洁:

$mail->Host = 'tls://smtp.gmail.com:587';

您可以通过在服务器上运行一些命令来实现连接(您需要安装 dnsutilstelnet 包).首先检查 DNS 是否正常工作:

You can your connectivity this by running some commands on your server (you will need dnsutils and telnet packages installed). First check DNS is working:

dig +short smtp.gmail.com

如果您的 DNS 正常工作,您将得到类似的信息:

You will get something like this if your DNS is working:

gmail-smtp-msa.l.google.com.
173.194.67.108
173.194.67.109

接下来尝试 telnet 到您需要的端口上的主机:

Next try to telnet to the host on the port you need:

telnet smtp.gmail.com 587

这应该给你这样的东西:

This should give you something like this:

Trying 173.194.67.109...
Connected to gmail-smtp-msa.l.google.com.
Escape character is '^]'.
220 mx.google.com ESMTP ex2sm16805587wjd.30 - gsmtp

(输入 quit 退出).

如果其中任何一个失败,PHPMailer 也将无法工作.所以去修复你的网络,然后再试一次.如果您无法控制自己的防火墙或 DNS,您可能需要向您的 ISP 提出支持请求来解决此问题.如果他们不修复它,您需要更换您的 ISP.

If either of these fail, PHPMailer will not work either. So go fix your network, then try again. If you are not in control of your own firewall or DNS, you probably need to raise a support ticket with your ISP to fix this. If they won't fix it, you need to replace your ISP.

回到PHPMailer,你可以通过设置:

Back in PHPMailer, you can get lower-level feedback on the connection by setting:

$mail->SMTPDebug = 4;

这篇关于PHPMailer 无法发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆