PHPMailer Software caused connection abort - php

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

Related

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

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.

Unable to send PHPMailer through gmail account from IIS on local machine

I'm running Windows 10 Pro with IIS hosting PHP 5.6.11. In the past, an older version of PHPMail worked perfectly for sending emails via gmail in this type of windows/IIS/PHP setup. At some point in the past it quit working. So now I'm using the very latest PHPMail library as of January 6, 2018. Here is my simple example taken mostly from this gmail example.
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'src/Exception.php';
require 'src/PHPMailer.php';
require 'src/SMTP.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Host = "smtp.gmail.com";
$mail->Port = 587; // TLS only
$mail->SMTPSecure = "tls"; // ssl is depracated
$mail->SMTPAuth = true;
$mail->Username = $smtpUsername;
$mail->Password = $smtpPassword;
$mail->setFrom($emailFrom, $emailFromName);
$mail->addAddress($emailTo, $emailToName);
$mail->Subject = 'PHPMailer GMail SMTP test';
$mail->msgHTML("test body");
$mail->AltBody = 'HTML messaging not supported';
if(!$mail->send()){
echo "Mailer Error: " . $mail->ErrorInfo;
}else{
echo "Message sent!";
}
Here is the culprit:
Connection failed. Error #2: stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages:error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed [.....\PHPMailer\src\SMTP.php line 405]
How can I solve this?
UPDATE: I found the true source of the problem and updated the error message in this post, but I don't know how to solve it.
There are many case you have to check like
open SSL in enable using you php ini
you have to check your firewall that you are successfully able to access gmail server.
etc
you can find more solution and cases here
Change below value in your code.
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPDebug = 2; //Get Error Details
Hope that's work.
I have tried everything that have been written on Stackoverflow but no luck. At last I omitted the line:
$mail->isSMTP();
Of course it is not a solution but in my case I had to solve the problem and it just did it. Now I'm using php's original mail() function. By the way I tried to use Gmail and Hotmail SMTP services. I don't want anyone to spend so much time on this like me.
Everything was solved when I installed a correct certificate in IIS.

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();

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