Failed to connect to server: Connection refused (111)
<p>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</p>

<p>SMTP ERROR: Failed to connect to server: Connection refused (111)
SMTP connect() failed. <a href="https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting" rel="noreferrer">https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting</a>
Mailer Error: SMTP connect() failed. <a href="https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting" rel="noreferrer">https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting</a></p>

<p>My Code is here below</p>

<pre><code>&lt;?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-&gt;isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail-&gt;SMTPDebug = 2;
//Ask for HTML-friendly debug output
$mail-&gt;Debugoutput = 'html';
//Set the hostname of the mail server
$mail-&gt;Host = "smtp.gmail.com";
//Set the SMTP port number - likely to be 25, 465 or 587
$mail-&gt;Port = 587;
//Whether to use SMTP authentication
$mail-&gt;SMTPAuth = true;
//Username to use for SMTP authentication
$mail-&gt;Username = "mzubim@gmail.com";
$mail-&gt;Password = "password";
//Set who the message is to be sent from
$mail-&gt;setFrom('mzubim@gmail.com', 'Zubair Mushtaq');
//Set an alternative reply-to address
$mail-&gt;addReplyTo('replyto@gmail.com', 'Secure Developer');
//Set who the message is to be sent to
$mail-&gt;addAddress('abulogics@gmail.com', 'Abulogicss');
//Set the subject line
$mail-&gt;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-&gt;msgHTML("convert HTML into a basic plain-text alternative body");
//Replace the plain text body with one created manually
$mail-&gt;AltBody = 'This is a plain-text message body';

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