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

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

问题描述

我的项目包含发送电子邮件的功能,即 PHPMailer 。从本地服务器发送电子邮件很顺利,但是今天停止发送电子邮件,现在显示此错误消息

  SMTP错误:无法连接到SMTP主机。 

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

  SMTP错误:无法连接到服务器: php_network_getaddresses:getaddrinfo failed:请求的名称有效,但没有找到请求的类型的数据。 (0)SMTP错误:无法连接到SMTP主机

我已经有$ code>启用扩展= p​​hp_openssl.dll php.ini 中。



这是我的代码:

  $ mail = new PHPMailer ); 
$ mail-> SMTPSecure ='ssl';
$ mail-> IsSMTP();
$ mail->用户名=myemail@gmail.com; //您的GMail用户名
$ mail-> Password =password;
$ mail-> AddAddress($ email); // recipient email
$ mail-> FromName =username; //可读名称
$ mail-> IsHTML(true);
$ mail-> Subject =title;
$ mail-> Body =Message;
$ mail-> SMTPDebug = 1;
$ mail-> Host =ssl://smtp.gmail.com; // GMail
$ mail-> Port = 465;
$ mail-> IsSMTP(); //使用SMTP
$ mail-> SMTPAuth = true; //打开SMTP认证
$ mail-> From = $ mail->用户名;
if($ mail-> Send()){
} else {
}

谢谢

解决方案

这通常报告为PHPMailer问题(这个问题有很多重复),但是本地网络上的本地DNS故障,防火墙阻止或其他网络问题几乎总是下降。



首先,确保使用最新的PHPMailer



不要在端口465上使用SSL,自1998年以来已经已弃用,并且仅由没有获得备忘录的Microsoft产品使用;在端口587上使用TLS:

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

或更简洁:

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

您可以通过在服务器上运行某些命令来实现您的连接(您将需要 dnsutils telnet 包安装)。首先检查DNS正在工作:

  dig + short smtp.gmail.com 
/ pre>

如果您的DNS正在运行,您将收到以下内容:

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

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

  telnet smtp.gmail.com 587 
pre>

这应该是这样的:

 尝试173.194 .67.109 ... 
连接到gmail-smtp-msa.l.google.com。
转义字符为'^]。
220 mx.google.com ESMTP ex2sm16805587wjd.30 - gsmtp

(输入退出以获取)



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



返回到PHPMailer,您可以通过设置以获得关于连接的低级反馈:

  $ mail-> SMTPDebug = 4; 


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.

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

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

This is my code:

$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 {
}

thanks

解决方案

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.

First, make sure you are using the latest PHPMailer.

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;

or more succinctly:

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

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

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

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

(Enter quit to get out of that).

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.

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

$mail->SMTPDebug = 4;

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

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