Could not connect to SMTP host. Message could not be sent - php

When I sent email from server then it gives me two error -
SMTP Error: Could not connect to SMTP host. Message could not be sent.
Mailer Error: SMTP Error: Could not connect to SMTP host.
I found many hints on another answers on Stack overflow but doesn't work. I tried port no 465/587/65. Even below code works proper on my local system If i set port 587. But in server, It doesn't work.
require('class.phpmailer.php');
require('class.smtp.php');
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPSecure = "ssl";
$mail->SMTPAuth = true;
$mail->Host = "smtp.gmail.com";
$mail->Username = "FromEmailId";
$mail->Password = "Password";
$mail->Port = 465;
$mail->From = "FromEmailId";
$mail->AddAddress("ToEmail");
$mail->IsHTML(true);
$mail->Subject = "Here is the subject";
$mail->Body = "This is the HTML message body <b>in bold!</b>";
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";

Try phpmailer in debug mode to check the error
$mail->SMTPDebug = 3; // Enable verbose debug output

First logout the your gmail account
then open this url
use this yrl
click the continue button
Next
change the port and SMTPsecure
$mail->SMTPSecure = "tls";
$mail->Port = 587;

The Gmail Help :
Still can't send mail?
If you tried configuring your SMTP server on port 465 (with SSL/TLS) and port 587 (with STARTTLS), but are still having trouble sending mail, try configuring your SMTP to use port 25 (with SSL/TLS).

This question failed to do a basic search and missed a vital piece of information later mentioned in comments: the OP is using GoDaddy. GoDaddy is well known to block outbound SMTP, so any suggestions relating to switching port numbers or security protocols will not help. You need to read other questions on GoDaddy's mail handling, and read their support documentation, which will push you in the direction of using GoDaddy's mail servers (using securehosting domains) or via localhost as a relay to the same. All of this is also covered in the PHPMailer troubleshooting guide.
In future, search before posting, as per the SO guidelines.

Related

Troubleshooting PHPMailer with Bluehost

Trying to set up PHPMailer for a site I have hosted on Bluehost and after a full day of researching and troubleshooting I just can not get it working.
I'm a beginner so apologies in advance for the newbie question, but I've been reading everything I can find (including this and this, as well as the PHPMailer docs) to solve this but can't seem to get mine set up correctly. Any guidance, thoughts on what I'm doing wrong or ways to debug this are much appreciated.
This is what I've found regarding Bluehost SMTP.
Secure SSL/TLS Settings (Recommended)
Username Your email address: john#example.com
Password The password for that email account.
Incoming Server mail.example.com*
Incoming Port 993 (IMAP) or 995 (POP3)
Outgoing Server mail.example.com*
Outgoing Port 465 (SMTP)
Authentication Password
*Replace example.com with your domain name.
Below is what I'm using in my file (personal info removed).
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
//Load Composer's autoloader
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = 2;
$mail->isSMTP();
$mail->Host = 'mail.MYDOMAIN.com';
$mail->SMTPAuth = true;
$mail->Username = 'MYEMAIL#MYDOMAIN.com';
$mail->Password = 'MYEMAILPASSWORD';
$mail->SMTPSecure = 'tls';
$mail->Port = 465;
//Recipients
$mail->setFrom('MYEMAIL#MYDOMAIN.com');
$mail->addAddress('MYEMAIL#MYDOMAIN.com');
//Content
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
I've installed PHPMailer using Composer so the autoload.php file (and the phpmailer folder) are in a 'vendor' folder located in the same directory as the file that contains the code shown above.
After uploading to my Bluehost server, when I try to display the webpage that has this code in the browser, I get this HTTP ERROR 500 screenshot and of course no email sent.
Something that's covered in many of the examples and the docs is what combinations of encryption and port settings will work.
You have Port = 465 and SMTPSecure = 'tls'; that won't work. Either change Port to 587, or SMTPSecure to 'ssl' (but not both!). As it stands, you're trying to open a connection to a port that expects implicit TLS while using a protocol that expects to need to make it explicit with STARTTLS, and that's not going to work.

Why is Amazon SES SMTP only working on port 443?

I'm setting up a PHP script that uses PHPMail to send email. The "from" address is already verified on Amazon's console, and I have created the IAM user and SMTP credentials. When creating those, Amazon tells you to use ports 25, 465 or 587. Here's a php example from Amazon's documentation, doesn't use PHPMail but the idea should be the same.
This is my script:
$mail = new PHPMailer;
$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->Timeout = 20;
$mail->CharSet = 'UTF-8';
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'ssl://email-smtp.us-east-1.amazonaws.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'my_username'; // SMTP username
$mail->Password = 'my_password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('my#email.com', 'My email name');
$mail->addAddress('test#test.com', 'Test'); // Add a recipient
$mail->addReplyTo('my#email.com', 'My email name');
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Amazon SES SMTP test with PHPMailer';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
That script like that doesn't work, I get a connection timeout error. However, if I change the port to 443, then it works fine. Why is that? That's not a port listed by Amazon so I'm worried that even though it works now it might give some other problems in the future. Am I missing something here? Port 465 doesn't work either, by the way.
EDIT
Just for clarification, I realize this works using port 443 like I mention, however why isn't this working with the ports they suggest? That's what I'm trying to understand. Is there anything missing in this script? I've also teste without pre-fixing the host with "ssl://" (which is how they show it in their example) and using the suggested ports, to no avail.
TCP port 443 is the standard TCP port that is used for websites which use SSL. your address is
ssl://email-smtp.us-east-1.amazonaws.com
so to me this is working as it should.
The problem was that there was a setting in our CSF Firewall called SMTP_BLOCK that was on. We turned that off and now port 587 works fine (I've had also to remove the ssl:// from the host address).
Maybe this helps someone in the future with the same problem.

SMTP ERROR: Failed to connect to server: Connection timed out (110) with PHPMailer and Outlook SMTP

I am getting SMTP Error with PHP Mailer and Outlook SMTP. I am confused here because it is working fine on localhost with Port number 25 but It is not working on Hosting Server, I have tried all ports with SSL & TLS.
Error : SMTP ERROR: Failed to connect to server: Connection timed out (110)
My Code:
<?php
include("PHPMailer.php");
error_reporting(E_ALL);
ini_set('display_errors', '1');
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "smtp.live.com";
$mail->SMTPDebug = 2;
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Username = "info#neelcomputech.com";
$mail->Password = "password";
$mail->Priority = 1;
$mail->CharSet = 'UTF-8';
$mail->ContentType = 'text/html; charset=utf-8\r\n';
$mail->From = "info#neelcomputech.com";
$mail->FromName = $name;
$mail->AddAddress("info#neelcomputech.com");
$mail->IsHTML(true);
$mail->Subject = "You got Message from Website";
$mail->Body = "testing";
if(!$mail->Send())
{
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
else
{
echo 'success';
}
?>
Please help me to solve this issue. I have Shared Linux Hosting.
I had a similar problem, and it turned out my host (Bluehost) blocked outgoing connections on port 465. I will post the solution here in the hope that helps you. But I'm not expert enough to know if it's the same problem or not.
I found a wonderful how-to which fixed it for me:
In your cPanel DNS Zone editor, find the MX (Mail Exchanger) section, and select 'remote mail exchanger'.
In the cPanel email accounts section, create the appropriate email address (don't skip this)
Don't use "smtp.live.com" as your smtp host. Use the smtp host of your Shared Linux Hosting smtp. I don't know how you will get yours. Mine is boxXXXX.bluehost.com.
Set your username and password to be the same as the email account you just set-up in cPanel.
None of the answers worked for me.
After many hours, I found the problem, but only works for Cpanel/WHM
Login into WHM.
Go to ConfigServer Security & Firewall inside plugins option.
Click on Firewall configuration
Filter by SMTP Settings
Look for SMTP_ALLOWUSER option and add the Cpanel account's username separated by coma
Restart the Firewall.
If you don't have access to WHM ask your provider.
Hope it helps!
My code is correct.
The issue was with Hosting Provider. I contacted them about this and they did some configuration on their Server and it's done.

SMTP -> ERROR: Failed to connect to server: Connection timed out (110) The following From address failed: xxxxx#gmail.com ERROR

In contact form it's working fine in localhost. While hosted it's not working. Showing the error
**"SMTP -> ERROR: Failed to connect to server: Connection timed out (110) The following From address failed: xxxxx#gmail.com ERROR"**
I attached my contact_submit.php code form
include_once('class.phpmailer.php');
$mail->IsSMTP(); //
$mail->Host = "smtp.gmail.com";
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->Username = "xxxx#gmail.com";
$mail->Password = "xxxx#123";
$mail->SMTPSecure = "tls";
$mail->SetFrom($email, $name);
$mail->AddReplyTo($email,$name);
$mail->Subject = "Contact - xxx";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
$mail->MsgHTML($body);
$mail->AddAddress("xxx#gmail.com","xxx");
if(!$mail->Send())
{
echo $mail;
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=contact.php?id='.$id.'&send=success">';
exit;
}
I'm using phpmailer 5.2.1.
I contacted the hosting side, but i'm not getting actual response.
I believe you have to connect to smtp.gmail.com on port 465, not port 587. Also, SSL is required. So, you should have:
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->SMTPSecure = "ssl";
You can increase the time out by prepending your code with:
set_time_limit(3600);
and then specifying the Timeout of the $mail object as such:
$mail->Timeout = 3600;
I had a similar problem, with mail being sent correctly from my local server but not my live one on the internet. It turned out my host (Bluehost) blocked outgoing connections on port 465.
I found a wonderful how-to which fixed it for me:
In your cPanel > Mail, find the MX (MX Entry) section, and select 'remote mail exchanger'.
In the cPanel email accounts section, create the appropriate email address (don't skip this)
Don't use "smtp.live.com" as your smtp host. Use the smtp host of your Shared Linux Hosting smtp. I don't know how you will get yours. Mine is boxXXXX.bluehost.com.
Set your username and password to be the same as the email account you just set-up in cPanel.

Mail not sending with PHPMailer over SSL using SMTP

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.

Categories