Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to connect to server: Connection refused (111)

Tags:

phpmailer

I am using PHPMailer to send the smtp emails. If I use the same setting in a WordPress site, it works fine. But my priority is to use in a custom php page. And there, it is giving the following errors

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

My Code is here below

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

require 'emails/PHPMailer/PHPMailerAutoload.php';

//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = "smtp.gmail.com";
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 587;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication
$mail->Username = "[email protected]";
$mail->Password = "password";
//Set who the message is to be sent from
$mail->setFrom('[email protected]', 'Zubair Mushtaq');
//Set an alternative reply-to address
$mail->addReplyTo('[email protected]', 'Secure Developer');
//Set who the message is to be sent to
$mail->addAddress('[email protected]', 'Abulogicss');
//Set the subject line
$mail->Subject = 'PHPMailer SMTP test';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML("convert HTML into a basic plain-text alternative body");
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';

//send the message, check for errors
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}
like image 704
Zubair Mushtaq Avatar asked Jun 27 '16 18:06

Zubair Mushtaq


People also ask

How do you fix SMTP error failed to connect to server connection refused 111?

Some of them are getting the error 'SMTP ERROR: Failed to connect to server: Connection refused (111) error'. To fix this error you would need to disable SMTP Restrictions in your server. This feature prevents users from bypassing the mail server to send mail, a common practice used by spammers.

How do I fix SMTP failed to connect to server?

Change the server's SMTP restrictions. Then, add the specified website user to the list of users who are authorized to initiate outbound SMTP connections. Edit PHPMailer's configuration settings, such as host and port. Correct DNS resolution for the mail server.

Why is SMTP not working?

If you get errors in the command, then SMTP is not working because the application cannot connect to the SMTP server. There are different causes for this, such as firewall configurations, VPN restrictions or DNS settings. For more help on this issue, contact your cloud provider.

Can't connect to SMTP host?

There are many popular cases for the failure of SMTP connection in PHPMailer and lack of SSL is one of that too. There might be a case, that the Open SSL extension is not enabled in your php. ini which is creating the connection problem. So, once you enable the extension=php_openssl.


1 Answers

try this its working fine

<?php
    include "emails/PHPMailer/PHPMailerAutoload.php"; 

    //Create a new PHPMailer instance
    $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);
    //Username to use for SMTP authentication
    $mail->Username = "@gmail.com";
    $mail->Password = "";
    //Set who the message is to be sent from
    $mail->setFrom('[email protected]', 'Zubair Mushtaq');
    //Set an alternative reply-to address
    $mail->addReplyTo('[email protected]', 'Secure Developer');
    //Set who the message is to be sent to
    $mail->addAddress('[email protected]', 'Abulogicss');
    //Set the subject line
    $mail->Subject = 'PHPMailer SMTP test';
    //Read an HTML message body from an external file, convert referenced images to embedded,
    //convert HTML into a basic plain-text alternative body
    $mail->msgHTML("convert HTML into a basic plain-text alternative body");
    //Replace the plain text body with one created manually
    $mail->AltBody = 'This is a plain-text message body';

    //send the message, check for errors
    if (!$mail->send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
        echo "Message sent!";
    }

enter image description here

like image 74
Aswad Shaik Avatar answered Nov 28 '22 02:11

Aswad Shaik