SMTP -> ERROR: Failed to connect to server: (0) in PHP malier - php

Following is my php email code and it works fine still today.
require_once('_lib/class.phpmailer.php');
include 'func/db_connect.php';
error_reporting(E_ALL);
ini_set('display_errors', 1);
function supervisorMail(){
global $error;
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = "*****#gmail.com";
$mail->Password = "*****";
$mail->SetFrom("****#gmail.com", "Employee Leave Management System");
}
But now it does not work without any changing of code and it makes following error.
SMTP -> ERROR: Failed to connect to server: (0) SMTP Error: Could not connect to SMTP host. Message could not be sent. Mailer Error: SMTP Error: Could not connect to SMTP host.
I could not able to find the solution. How can I fixed this.

try this it will be work
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->IsHTML(true);
$mail->Username = "*******#gmail.com";
$mail->Password = "*******";
you have to change in gmail.In Account permissions section, find Access for less secure apps and enable it.

I think you should try by changing the host like this :
$mail->Host = "ssl://smtp.gmail.com";
$mail->Port = 465;
And also set the other paramters like this :
$mail->SetFrom('contact#example.com', 'User');
$mail->AddReplyTo("example#gmail.com', 'Name Here");
$mail->Subject = "Contact us Email";
$mail->MsgHTML($body);
$address = "user#example.com";
$mail->AddAddress($address, "new user");
if(!$mail->Send()) {
echo "Error in Sending email: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}

provide $mail->SMTPDebug=1 itz working [1]: http://i.stack.imgur.com/ZEpjv.png

In my case it was a lack of SSL support in PHP which gave this error.
So I enabled extension=php_openssl.dll
$mail->SMTPDebug = 1; also hinted towards this solution.

For those having the same error:
In my own case, the simple change below solves the problem:
I changed
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
to
$mail->SMTPSecure = 'tls';
You can set it to ssl depending on your SMTP server settings

Related

SMTP connect() failed in server hosting [duplicate]

I need help please
this is my code:
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Port = 587;
$mail->Username = 'some#gmail.com';
$mail->Password = 'somepass';
$mail->addAddress('another#gmail.com', 'Josh Adams');
$mail->Subject = 'PHPMailer GMail SMTP test';
$body = 'This is the HTML message body in bold!';
$mail->MsgHTML($body);
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
and I get this error:
2013-12-11 15:15:02 SMTP ERROR: Failed to connect to server: Network is unreachable (101) SMTP connect() failed. Mailer Error: SMTP connect() failed.
any help please?
You might want to start by isolating this problem to determine whether it's truly a network problem; or whether it's specific to PHP mailer or your code. On your server, from a command prompt, try using telnet to connect to smtp.gmail.com on port 587, like so:
telnet smtp.gmail.com 587
You should see a response from smtp.gmail.com, like so:
Trying 173.194.74.108...
Connected to gmail-smtp-msa.l.google.com.
Escape character is '^]'.
220 mx.google.com ESMTP f19sm71757226qaq.12 - gsmtp
Do you see this, or does the connection attempt hang and eventually time out? If the connection fails, it could mean that your hosting company is blocking outgoing SMTP connections on port 587.
This worked for me:
change:
$mail->SMTPSecure = 'ssl';
$mail->Host = 'smtp.gmail.com';
$mail->Port = '465';
to
$mail->SMTPSecure = 'tls';
$mail->Host = 'smtp.gmail.com';
$mail->Port = '587';
The code below:
$mail->SMTPSecure = 'ssl';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Host = 'ssl://smtp.gmail.com:465';
I was facing the same issues on Godaddy hosting so I spend lots of time and fix it by disabling the SMTP restriction on the server end.
SMTP code is for PHPMailer
$mail = new PHPMailer(true);
try {
//Server settings
//$mail->SMTPDebug = SMTP::DEBUG_SERVER; //Enable verbose debug output
$mail->SMTPDebug = 2; //Enable verbose debug output
//$mail->isSMTP(); //Send using SMTP
$mail->Host = "tls://smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->Username = contact#example.in;
$mail->Password = SMTP_PASSWORD;
$mail->SMTPSecure = "tls";
$mail->Port = 587;
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
//Recipients
$mail->setFrom('contact#example.in', 'Online Order');
$mail->addAddress('test#gmail.com'); //Add a recipient
$mail->addReplyTo('contact#example.in', 'Jewellery');
//Attachments
//$mail->addAttachment('/var/tmp/file.tar.gz'); //Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); //Optional name
//Content
$mail->isHTML(true); //Set email format to HTML
$mail->Subject = 'Order';
$mail->Body = 'This is test email content';
$mail->AltBody = 'This is test email content';
if($mail->send()){
return '1';
}else{
return 'Order email sending failed';
}
change
$mail->SMTPSecure = "tls";
with
$mail->SMTPSecure = 'ssl';

SMTP ERROR: Failed to connect to server: Connection refused (111) on Godaddy server

I want to send mail using phpmailer. I have uploaded my phpmailer files on my Godaddy server. The below code is running in my localhost but not on my server.
2019-02-03 16:54:12 SMTP ERROR: Failed to connect to server: Connection refused (111)
The below is the code for php
<?php
include_once('PHPMailer/src/PHPMailer.php');
include_once('PHPMailer/src/SMTP.php');
$mail = new PHPMailer\PHPMailer\PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 0; // 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 = 465; // or 587,465
$mail->IsHTML(true);
$mail->Username = "mail#gmail.com";
$mail->Password = "password";
$mail->SetFrom("mail#gmail.com");
$mail->Subject = "Test mail";
$mail->Body = "Hello World";
$mail->AddAddress("mail#gmail.com");
if($mail->Send()) {
echo "Message has been sent";
}
?>
use PHPMailer\PHPMailer\PHPMailer; <-- make sure these are not in a function
use PHPMailer\PHPMailer\Exception;
require 'path/src/Exception.php';
require 'path/src/PHPMailer.php';
require 'path/src/SMTP.php';
Use tls and not ssl it can be buggy
$mail->SMTPSecure = "tls";
$mail->Port = 587;
Check out my answer here this will tell you everything you need to know to connect to gmail with phpmailer

PHPMailer SMTP ERROR: Failed to connect to server

<?php
include("class.phpmailer.php");
include("class.smtp.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Mailer = "smtp";
$mail->SMTPDebug = 2;
$mail->Host = "ssl://smtp.gmail.com"; // specify main and backup server
$mail->Port = 587; // set the port to use
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->SMTPSecure = "tls";
$mail->Username = "123#gmail.com"; // SMTP username
$mail->Password = "password"; // SMTP password
$mail->From = "123#gmail.com";
$mail->FromName = "Webmaster";
$mail->AddAddress("asd#hotmail.com");
$mail->AddReplyTo("123#gmail.com", "Webmaster");
$mail->IsHTML(true);
$mail->Subject = "First PHPMailer Message";
$mail->Body = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
$mail->WordWrap = 50;
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
?>
It returns an error
2016-04-01 08:41:43 SMTP ERROR: Failed to connect to server: (0)
2016-04-01 08:41:43 SMTP connect() failed.
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting Message was not sent.Mailer error: SMTP connect() failed.
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
I am hosting this php on my xampp local server. The extension=php_openssl.dll on php.ini is already uncommended.
Your configuration might be wrong. I believe that if you change your host into smtp.gmail.com it might solve your problem.
I noticed that you set security tls but you want to connect with ssl as well.
Change $mail->Host = "ssl://smtp.gmail.com"; into $mail->Host = "smtp.gmail.com"; and security to ssl.
From this answer:
$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 = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "email#gmail.com";
$mail->Password = "password";
$mail->SetFrom("example#gmail.com");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("email#gmail.com");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}

Not able to use phpmailer script for gmail

This is my php script:
<?php
error_reporting(E_STRICT);
date_default_timezone_set('America/Toronto');
require_once('../class.phpmailer.php');
include("../class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 2; // 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 = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "x#gmail.com";
$mail->Password = "1234";
$mail->From = "y#gmail.com";
$mail->Subject = "Need Invitation";
$mail->Body = "invite me!";
$mail->AddAddress("x#gmail.com");
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>
After executing this script i am getting following error:
SMTP -> ERROR: Failed to connect to server: Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP? (1)
I have also ssl enabled:
There are a lot of question which are very similar to this but didn't get any help from there.
Try changing
$mail->Host = "ssl://smtp.gmail.com";
to
$mail->Host = "smtp.gmail.com";
SSL operates at a different level in the network model from HTTP, SMTP, etc.
You may also have to change your SMTPSecure setting from 'ssl' to 'tls'.

PHPMailer Could not Authenticate

I got this error using PHPMailer:
SMTP Error: Could not authenticate. Mailer Error: SMTP Error: Could not authenticate.
and Here is the code :
function send_email($reciever = "e.salamati.taba#gmail.com", $mail_arr = '') { // This function will be upgraded later
$mail = new PHPMailer();
$body = $mail_arr['body'];
//$body = eregi_replace("[\]", '', $body);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPDebug = true;
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$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 GMAIL server
$mail->Username = "user"; // GMAIL username
$mail->Password = "pass"; // GMAIL password
$mail->SetFrom('e.salamati.taba#gmail.com', 'no-reply gmail.com');
$mail->Subject = $mail_arr['subject'];
$mail->MsgHTML($body);
echo "-----------------------";
$address = $reciever;
$mail->AddAddress($address, "e.salamati.taba#gmail.com");
if(! $mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
}
I have also see this, but still got the error, Is there any xampp config or something that cause the problem?
Thanks in advance...

Categories