Issues send emails in localhost but not in liveserver [duplicate] - php

This question already has answers here:
SMTP Connect() failed. Message was not sent.Mailer error: SMTP Connect() failed
(15 answers)
Closed 4 years ago.
I have an issue with PHPMailer.
First i have integrated phpmailer and email are sent without any issues in localhost and live server. Since yesterday, i have an issues to send email in localhost but not in live server. The error i got when i send the email in localhost are
Email could not be sent to the recently approved member. ErrorSMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
This is the same code to send the emails both in live server and localhost.
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'someone#gmail.com'; // SMTP username
$mail->Password = $pw; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 25; // TCP port to connect to
$mail->setFrom('someone#gmail.com', 'Reboot E-Magazine');
$mail->addAddress($email); // Add a recipient
$mail->Subject = 'Article Confirmation';
$mail->Body =
'Reboot Magazine
--------------------------------------------------------------------
Your article was not been approved due to certain reasons.
Please ensure the article are well-written and relevant to the topic.
--------------------------------------------------------------------
Sincerely,
Riyaz Ahmad
Admin of Reboot E-magazine';
if(!$mail->send())
{
$error = "Email could not be sent. Error". $mail->ErrorInfo;
}
else
{
$msg = "Email has been sent to notify the member";
}

I believe it was already answered here:
SMTP Connect() failed. Message was not sent.Mailer error: SMTP Connect() failed
Commenting
$mail->isSMTP();
usualy fixes this issue.

Related

PHPMailer Software caused connection abort

I am trying to send emails from a local XAMPP instance on my Mac running Sierra 10.12 using PHPMailer. When I go to send the test email utilising Gmail's SMTP server(s), I get a '53' software caused connection abort error as seen below:
2017-04-30 02:59:56 SMTP ERROR: Failed to connect to server: Software caused connection abort (53) 2017-04-30 02:59:56 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
I have tried their troubleshooting guide and found nothing else relating to this issue, so I assume it is pretty infrequent.
The code below is based on PHPMailer's Gmail Example which is strange because it is throwing the error. I have obviously hidden my emails and password for StackOverflow:
date_default_timezone_set('Etc/UTC');
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer; // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com";
$mail->Port = 587; // or 587
$mail->IsHTML(true);
$mail->Username = "email#gmail.com";
$mail->Password = "mypassword";
$mail->SetFrom("email#gmail.com");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("myotheremail#hotmail.com");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}
Any help would be greatly appreciated.
Cheers
For those who stumble upon this thread:
Thanks to Sahil, the issue was being caused by a setting I was using incorrectly. Instead of $mail->SMTPSecure = 'ssl'; I should've been using $mail->SMTPSecure = 'tls';.
Another issue I discovered was when this was resolved, the mail was taking an extremely long time to send/getting stuck on load which caused a timeout. This was fixed by changing $mail->IsSMTP(); to $mail->IsMail();.
Cheers

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

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.

Sending Mail using PHP : SMTP ERROR

I am trying to send mail using php. But it gave me error,
" SMTP ERROR: Failed to connect to server: Connection refused (111)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
"Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting" "
So that I searched alot to find the problem. There I got a solution, that I need to change $mail->IsMail(); from $mail->IsSMTP();
I did it and Mail was sent...
But when I checked my mail,
I got,
"This message may not have been sent by: sender#gmail.com"
Being a developer I understood that An email should not contain such lines or issues.
I want to know, Is it alright if Receiver is showing such line in Email? and if not that What should I do?
I mean what changes I should make in my code.
Here is my php code:
**
date_default_timezone_set('Etc/UTC');
include 'PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->IsSMTP();
// $mail->Mailer = "smtp";
$mail->SMTPDebug = 1;
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "sender#gmail.com";
$mail->Password = 'senderPassword';
$mail->setFrom("sender#gmail.com", 'sender name');
$mail->addReplyTo('sender#gmail.com', '');
$mail->addAddress($receiver, '');
$mail->Subject = 'Welcome';
$mail->Body = 'body';
$mail->AltBody = 'This is a plain-text message body';
if (!$mail->send())
{
return "Mailer Error: " . $mail->ErrorInfo;
}
else
{
return array('flag' => "1");
}
**
isMail and isSMTP use two different sending mechanisms. isMail submits messages through the PHP mail() function, which delivers mail to a local mail server via a sendmail binary. This local mail server then tries to deliver the message to its ultimate recipient. It's possible that this local server will accept a message which is later rejected, and that will be too late for your script to know about.
With isMail:
script -> local mail server -> gmail
With isSMTP:
script -> gmail
With isMail you don't need to authenticate (localhost is usually allowed to relay), and the message is sent from your server to gmail. With isSMTP your message is sent from gmail to gmail, and it does require authentication.
When sending directly through gmail, you need to authenticate with gmail, and that has its own set of problems (that will be why your script is not working) covered thoroughly in the PHPMailer docs, examples and questions here on SO.
When sending via your server, you're saying that you are sending from a gmail user, but it's being sent by your server, not by a server listed in gmail's SPF record. This is forgery, which is why you are seeing the "This message may not have been sent by..." message. It would not say that if you sent from an address in your own domain.
The solution is to fix your gmail authentication and send directly through gmail. Base your code on the gmail example provided with PHPMailer, not the old, obsolete code you're using, and read the docs.
Here is the code which is use for mailing purpose. Try setting the SMTPDebug mode 3 and check the output.
$mail = new PHPMailer;
$mail->SMTPDebug = 3;
$mail->isSMTP();
$mail->Host = 'smtp.office365.com';
$mail->SMTPAuth = true;
$mail->Username = 'xxxxxxxxxxxxxxx';
$mail->Password = 'xxxxxxxxxxxxx';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->From = 'xxxxxxxxxxxxxx';
$mail->FromName = 'xxxxxxxxxxxxxxxxx';
$mail->addAddress(xxxxxxxxxxxxxx);
$mail->addReplyTo('xxxxxxxxxxxxxxxxxxxxxxx', 'xxxxxxxxxxxxxxxxxxxxx');
$mail->isHTML(true);
$mail->Subject = '';
$mail->Body = "";
$mail->send();

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.

PHPmailer can't connect to smtp server

I've been using PHPmailer (https://github.com/Synchro/PHPMailer) to send email through amazon SES for a few months. At some time in the last two weeks it has stopped working, and I haven't touched it. I'm getting an error msg:
SMTP Error: Could not connect to SMTP host.
This is my code.
public function sendEmail($to,$subject,$body){
$mail = new PHPMailer;
$mail->IsSMTP(); // Set mailer to use SMTP
$mail->Host = 'amazonaws....'; // Specify main and backup server
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'mySMTPuname'; // SMTP username
$mail->Password = 'smtpPword'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->From = 'example';
$mail->FromName = 'me';
$mail->AddAddress($to); // Name is optional
$mail->IsHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $body;
return $mail->Send();
}
My amazon account is still upto date and active. Is there any way to print out more detailed error msgs for debugging? Has there been any known issues lately?
Try :
$mail->SMTPDebug = 1;
// if you're using SSL
$mail->SMTPSecure = 'ssl';
// OR use TLS
$mail->SMTPSecure = 'tls';
This is a very old question but I just had the same problem so it may still be relevant to others.
If it stopped working without you changing anything it is probably connected to your hosting company / isp blocking SMTP traffic from your server to other servers.
There are a few topics on this, as multiple hosting companies using Cpanel and also Godaddy have implemented such measures for combating spam.
Try:
$mail->SMTPDebug = 3;
to get the maximum level of detail on the error.
One solution is to use the mail server in the same hosting account (if it blocks SMTP to the outside it probably has an internal service you can use).
To keep using Amazon SES you need to open SMTP traffic on your server.
IF you have control over Cpanel/WHM "tweak settings" you can do it yourself, otherwise you need to ask your hosting provider.
Check this answer for details "Password not accepted from server: 535 Incorrect authentication data" when sending with GMail and phpMailer

Categories