This is what I get when running the PHPMailer script on the production server. PHPMailer version is 5.1
SMTP -> ERROR: Failed to connect to server: Network is unreachable (101)
SMTP Error: Could not connect to SMTP host. Mailer Error: SMTP Error:
Could not connect to SMTP host
PHPMailer Configuration
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->Username = "xxxx#gmail.com";
$mail->Password = "xxxx";
Using this config, it worked perfectly on localhost running XAMPP.
What I Tried
$mail->SMTPSecure = "tls"; // no luck
$mail->Host = "ssl://smtp.gmail.com"; // no luck
$mail->SMTPKeepAlive = true; // no luck
$mail->Port = 587; // no luck
$mail->Port = 25; // no luck
I tried with various combinations of the above but still I got the error message displayed above.
FYI, my localhost is on Windows while the server is on Linux. Don't know if that would cause a problem, but just wanted to put it out there.
Related
My target is to send an email to myself using PHPMailer. I am using Gmail SMTP as a start (I have enabled less secure app access and disabled two factor authentication), and am trying to connect from localhost (using MAMP). This is my code:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer-master/src/Exception.php';
require 'PHPMailer-master/src/PHPMailer.php';
require 'PHPMailer-master/src/SMTP.php';
$mail = new PHPmailer(true);
try {
$mail->SetFrom('myemail','myname');
$mail->AddAddress('myemail','mynickname');
$mail->Body = 'There is a great disturbance in the Force.';
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = TRUE;
$mail->SMTPSecure = 'tls';
$mail->Username = 'myemail#gmail.com';
$mail->Password = 'mypassword';
$mail->Port = 587;
$mail->Send();
}
catch (Exception $e){
echo $e->errorMessage();
}
This gives me a SMTP connect() failed error. I checked the troubleshooting page for this and have found that this error occurs when there is a Firewall or DNS failure, so I have tried disabling the firewall on my computer altogether, but to no avail. What could be the problem?
It is worth noting that I have tried connecting to the smtp server on cmd using telnet smtp.gmail.com 587 and that didn't work either. What could be the problem here?
Edit: I changed $mail->SMTPSecure = 'tls'; and $mail->Port = 587; to $mail->SMTPSecure = 'ssl'; and $mail->Port = 587; respectively it worked. Why does tls not work?
Use
$mail->SMTPDebug = 4;
In the try{ section to help narrow down the errors
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = '********#gmail.com';
$mail->Password = '********';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
When I am using IsSmtp() message does not send it will stop on send.
When I remove IsSmtp() mail send properly but it gives the following error:
escapeshellcmd() has been disabled for security reasons
Just add $mail->isSMTP(); That will avoid using escapeshellcmd().
I've been researching for 3 weeks now on how to configure SMTP using PHPMailer for GoDaddy but none of it works. Tried contacting the GoDaddy support but they haven't replied yet. And I haven't seen any documentation on how to setup SMTP on their server.
I've been changing the Host many times, tested them, but none of it were successful in sending the email. I surpassed the error (using PHPMailer debug) but when I checked the email, I couldn't see any messages being received.
These are the hosts I've tried:
$mail->Host = "smtp.office365.com";
$mail->Host = "smtpout.secureserver.net";
$mail->Host = "relay-hosting.secureserver.net";
$mail->Host = "localhost";
Also, I've tried the settings in PHPMailer troubleshooting, but it won't work.
$mail->isSMTP();
$mail->Host = 'relay-hosting.secureserver.net';
$mail->Port = 25;
$mail->SMTPAuth = false;
$mail->SMTPSecure = false;
My current settings are:
$mail->isSMTP();
$mail->Host = "smtpout.secureserver.net";
$mail->Port = 80;
$mail->SMTPAuth = true;
//Enable SMTP debugging.
$mail->SMTPDebug = 4;
//Provide username and password
$mail->Username = "email#email.com";
$mail->Password = "password";
$mail->From = "email#email.com";
$mail->FromName = "From Name";
$mail->addAddress("email#email.com", "Name");
The email being used is the email created in GoDaddy which is Office 365 Email Essentials. Under Email & Office (Not Workspace Email or from cPanel). I'm using Economy Linux Hosting with cPanel package.
I'm new to GoDaddy so this problem is new to me. In some hosting providers like InMotion, I've never encountered this problem.
Godaddy made few changes the server relay setting. we don't need to separately mention the relay server settings anymore. following changes will works fine with PHPMailer.
$mail = new PHPMailer;
$mail->SMTPDebug = 0;
$mail->isSMTP();
$mail->Host = 'localhost';
$mail->Port = 25;
$mail->ssl = false;
$mail->authentication = false;
This is my configuration
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Host = "gator4102.hostgator.com";
$mail->Port = 465;
$mail->Username = 'test#some.com';
$mail->Password ='jyghvbjyhj';
It throws following error
SMTP -> ERROR: HELO not accepted from server:
SMTP -> NOTICE: EOF caught while checking if connectedLanguage string failed to load: tls
Mail gets delivered though sometimes. Please help.
Change:
$mail->Port = 465;
To:
$mail->Port = 587;
For tls, 587 port is used.
If you are using
$mail->SMTPSecure = "tls";
Then your setting should be as following :
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Host = "gator4102.hostgator.com";
$mail->Port = 587;
$mail->Username = "yourusername#abc.com";
$mail->Password = "yourpassword";
If you get response from server, it's already good enough. This means that you connect to server successfully.
You are probably trying to connect to the port on which another server application is listening. First, check again that you connect to port on which SMTP server is listening and waiting for TLS connection. Try to connect to another server to locate problem. Also, you can try another PHP library.
I am trying to use PHPMailer to send e-mails over SMTP but so far have had no luck. I've gone through a number of SO questions, PHPMailer tutorials and forum posts but still cannot get it to work. I'll document as many of my failed attempts as I can remember to save time, but firstly here is the code I am using:
<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors','On');
require('includes/class.phpmailer.php');
include('includes/class.smtp.php');
$mail = new PHPMailer();
$name = $_POST["name"];
$guests = $_POST["guests"];
$time = $_POST["time"];
$message = "<h1>".$name." has booked a table for ".$guests." at ".$time."</h1>";
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "ssl://smtp.gmail.com"; // SMTP server
$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 26; // set the SMTP port for the GMAIL server
$mail->Username = "myEmail#gmail.com"; // SMTP account username
$mail->Password = "myPassword"; // SMTP account password
$mail->SetFrom('myEmail#gmail.com', 'James Cushing');
$mail->AddReplyTo("myEmail#gmail.com","James Cushing");
$mail->Subject = "PHPMailer Test Subject via smtp, basic with authentication";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
$mail->MsgHTML($message)
$address = "myOtherEmail#me.com";
$mail->AddAddress($address, "James Cushing");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
Firstly, when I run this code now I get two different errors. On my local server I get the error:
SMTP -> ERROR: Failed to connect to server: Operation timed out (60)
The following From address failed: myEmail#gmail.com : Called Mail() without being connected
Mailer Error: The following From address failed: myEmail#gmail.com : Called Mail() without being connected
I get moreorless the same error running the same code on my web server, but the first line is:
SMTP -> ERROR: Failed to connect to server: Network is unreachable (101)
Obviously it's worth pointing out that I'm not using the literal "myEmail#gmail.com" but I've substituted my own email out for this post.
Things I've tried
- Using the iCloud SMTP server
- Using a different port
- Enabling the OpenSSL extension in my php.ini file
- Copying code from various PHPMailer examples
- Using Google's "DisplayUnlockCaptcha" system to enable connections
- Sending to and from different addresses
- Removing the "#gmail.com" from the Username property
- A number of other things I can't remember
This has now been driving me mad for about a day, so if anyone can solve it they will be a hero.
Thanks
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Username = "myemail#gmail.com";
$mail->Password = "**********";
$mail->Port = "465";
That is a working configuration.
try to replace what you have
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: So, the code below should work very well for you.
mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port for the
Firstly, use these settings for Google:
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls"; //edited from tsl
$mail->Username = "myEmail";
$mail->Password = "myPassword";
$mail->Port = "587";
But also, what firewall have you got set up?
If you're filtering out TCP ports 465/995, and maybe 587, you'll need to configure some exceptions or take them off your rules list.
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
I got a similar failure with SMTP whenever my client machine changes network connection (e.g., home vs. office network) and somehow restarting network service (or rebooting the machine) resolves the issue for me. Not sure if this would apply to your case, but just in case.
sudo /etc/init.d/networking restart # for ubuntu
First, Google created the "use less secure accounts method" function:
https://myaccount.google.com/security
Then created the another permission:
https://accounts.google.com/b/0/DisplayUnlockCaptcha
Hope it helps.