php - PHPMailer Error: php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution in LOCALHOST(XAMPP)
Get the solution ↓↓↓First of all, I am using XAMPP on Mac OS and the latest PHP mailer and I know that this might be related to DNS issue, but please read.
This is the full error:
2021-06-06 09:04:03 Connection: opening to smtp.gmail.com:587, timeout=300, options=array()
2021-06-06 09:04:23 Connection failed. Error #2: stream_socket_client(): php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution [/opt/lampp/htdocs/enfu2020/123abc/srcs/PHPMailer/src/SMTP.php line 388]
2021-06-06 09:04:23 Connection failed. Error #2: stream_socket_client(): Unable to connect to smtp.gmail.com:587 (php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution) [/opt/lampp/htdocs/enfu2020/123abc/srcs/PHPMailer/src/SMTP.php line 388]
2021-06-06 09:04:23 SMTP ERROR: Failed to connect to server: php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution (0)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Message could not be sent. Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
And heres' the code:
try {
$mail = new PHPMailer(true);
//Server settings
$mail->isSMTP(); //Send using SMTP
$mail->Host = 'smtp.gmail.com'; //Set the SMTP server to send through
$mail->Port = 587;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->Username = '[email protected]'; // SMTP username
$mail->Password = 'anyPassword'; //SMTP password
$mail->SMTPDebug = 3;
//Recipients
$mail->setFrom('[email protected]', 'My Cool Name');
$mail->AddAddress($email_1, $namatim); // Add a recipient
$mail->AddAddress($email_2, $namatim); // Add a recipient
$mail->AddAddress($email_3, $namatim); // Add a recipient
$mail->addReplyTo('[email protected]', 'My Cool Name');
//Content
$mail->isHTML(true); //Set email format to HTML
$mail->Subject = 'Verification Success';
$mail->Body = 'This is a message';
$mail->send();
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
From the troubleshooting page, I can see that this is indicated as a DNS issue. I suspect this is a problem of my XAMPP's DNS.
I've tried to dodig +short smtp.gmail.com
on my local machine and it works fine BUT when I clicked on Open Terminal button on XAMPP, and tried to run the same command, that command does not work.
I am wondering what config needs to be changed on my php.ini, or anything really (I've added extensions=openssl, but still the same error I got).
I'd really appreciate it if there's anyone out there that has the way to fix this and willing to share it to me as I'm totally clueless right now and been stuck with it for hours.
Thanks.
Answer
Solution:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
You have to Import PHPMailer classes into the global namespace, and These must be at the top of your script, not inside a function.
For use:
$mail = new PHPMailer(true);
You have to use this command inside your parent directory, because your PHPMailer class will be executed from "Vendor" folder.
composer dump-autoload
You can check my code:
https://github.com/mhreza76/php_pondit/blob/main/session_06/public/php-mailer.php
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: your lock file does not contain a compatible set of packages. please run composer update
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.