PHPMailer cannot send email

My project contains an email sending function which is PHPMailer

. It works well for sending email from my local server, but today it stops sending email 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 now it shows 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

a php.ini


This is my code:

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




source to share

2 answers

This is usually reported as a PHPMailer issue (and there are many duplicates of this question), but it almost always depends on a local DNS failure, firewall blocking, or other network issues on your local network.

Make sure you are using the latest PHPMailer first .

Do not use SSL on port 465, it has been deprecated since 1998 and is only used by Microsoft products that did not receive the note; use TLS on port 587 instead:

$mail->SMTPSecure = 'tls';
$mail->Host = '';
$mail->Port = 587;


or more succinctly:

$mail->Host = 'tls://';


You can connect to this by running some commands on your server (you need packages dnsutils

and telnet

). First DNS check works:

dig +short


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


Then try installing telnet on the host on the correct port:

telnet 587


This should give you something like this:

Connected to
Escape character is '^]'.
220 ESMTP ex2sm16805587wjd.30 - gsmtp


(Enter quit

to get out of this).

If any of these don't work, PHPMailer won't work . So fix your network and then try again. If you are not in control of your own firewall or DNS, you may have to raise a support ticket with your ISP to fix this. If they are not fixed, you will need to replace your ISP.

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

$mail->SMTPDebug = 4;




A simple google search showed this forum -



All Articles