PHPMailer cannot send email

主宰稳场 提交于 2019-12-17 17:16:06

问题


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


回答1:


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;



回答2:


A simple google search revealed this forum - http://forums.devshed.com/php-development-5/unable-to-find-the-socket-transport-ssl-667689.html



来源:https://stackoverflow.com/questions/25321030/phpmailer-cannot-send-email

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!