Failed to connect to server: Connection refused (111)
Asked Answered
M

3

7

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!";
}
Middlemost answered 27/6, 2016 at 18:42 Comment(1)
I gather you didn't read the error message or the docs it points to.Stroboscope
B
5

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

Beaujolais answered 29/6, 2016 at 6:36 Comment(4)
Thakns Ashu. It will solve my problem. But using your email and password is not the solution of my problem. I just want to enable my gmail account to work with smtp. Thanks again.Middlemost
I also did this. The proble was with the goDaddy hosting. They wasted my two days for debugging this. Anyway thanks for your great help.Middlemost
no issues #zubairbhai, so finally you got answer ryt?Beaujolais
Yes dear. goDaddy created a workspace for email and they instructed me to use their smtp instead of Gmail. Now, there is no email delay. Thanks for your great help.Middlemost
A
1

Consider increasing the debugging whilst you have the problem:

$mail->SMTPDebug = SMTP::DEBUG_LOWLEVEL ;

(see PHPMailer Debug messages)

My issue finally turned out to be the ports on my mail requesting server were not open to even get to the mail server (i.e. fix the firewall).

Accoutre answered 1/7, 2022 at 16:48 Comment(0)
H
0

the PHPMailer docs says that if it

-> //TCP port to connect to; use 587 if you have set SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS

And in your case u used ssl.

Hitandmiss answered 23/9, 2021 at 11:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.