0

I am using email/office from godaddy and I have a php page, which will send an email but it seems something block from godaddy as I am getting following error

Error: SMTP -> ERROR: Failed to connect to server: Connection refused (111)SMTP Connect() failed.

Why Connection refused? Is there any setup, which I missed? What could be the issue, I tried Gmail smtp as well and Cpanel email seems it is fine but some time the email got delay and sometimes not sent. I want to use my office 365 one.

To check the error: http://iclicks.co/uc_sipc/test_email.php

Here is my coding and note that it is running fine with gmail smtp in my local PC:

<?php

//send_email1();
send_email1();

function send_email1()
{
require 'class/class.phpmailer.php';
try
{


        $mail = new PHPMailer;
        $mail->IsSMTP();                                                 
        $mail->Host = 'smtp.office365.com';                                  
        $mail->Port = '587';                                             
        $mail->SMTPAuth = true;                                          
        $mail->Username = 'services@iclicks.co';                         
        $mail->Password = '***********';                                     

        $mail->SMTPDebug = 1;

        $mail->SMTPSecure = 'tls';                                       

        $mail->From = 'services@iclicks.co';                                        
        $mail->FromName = 'Mahmood';                                         
        $mail->AddAddress('maa8868@gmail.com', 'Mahmood');                       
        $mail->AddCC ('maa8868@gmail.com', 'Commercial Team');           
        $mail->WordWrap = 50;                                            
        $mail->IsHTML(true);                                                        
        $mail->Subject = 'test';                                         
        $mail->Body = 'test tes test';                                           

        $error='';

        var_dump($mail->Send());
        exit();
        if($error=$mail->Send())                                                 
        {   


                 $error = '<label class="text-success"> Thank you for contacting us, please check your email </label>';
                 //$query  = mysqli_query($mysqli, "INSERT INTO mail_logs (log_toname , log_tocompany, log_toposition, log_tomail ,log_status,log_date) VALUES ('$fname', '$company', '$position','$email', 'Sent Succesfully','$date')") ;
                echo "Email sent";
                 //header('Location: /projects/uc_sipc/index.php');
    }



        else
        {

            //$error = '<label class="text-danger">Erroe: Failed to send email.</label>';
            //header('Location: /projects/uc_sipc/index.php');
            echo ('Erroe: Failed to send email'.$error );
        }

}catch (Exception $e)   

{
echo ('Exception: '.$e);
}
echo ('Functon ended up');
}


?>
  • For starters, you have `if($error=$mail->Send())` which makes no sense; you're assigning a variable to `$mail->Send()`. It will always evaluate to `true`. You probably simply want `if($mail->Send())` there, as it appears to be your **success** flow, not your failure flow. You also don't need to assign `$error` at all. In addition to this, you have `exit()` before this, so that block will never be reached anyway. Finally, you're open to SQL injection - ensure you paramaterise your queries. – Obsidian Age Jun 25 '18 at 21:34
  • How does `if($error=$mail->Send())` "always evaluate to `true`?" - It will be whatever `$mail->Send()` returns... unless I don't understand? – Sean Bright Jun 25 '18 at 21:42
  • I found your exact problem: you didn't search before posting nor read the docs, either of which would tell you that GoDaddy blocks outbound SMTP connections. You're also using a very old version of PHPMailer. – Synchro Jun 26 '18 at 07:45
  • Possible duplicate of [PHPMailer GoDaddy Server SMTP Connection Refused](https://stackoverflow.com/questions/21841834/phpmailer-godaddy-server-smtp-connection-refused) – Synchro Jun 26 '18 at 07:46
  • Your try/catch won't do anything as you have not enabled exceptions in PHPMailer (by passing `true` to the constructor). Base your code on the examples provided with PHPMailer and you might find things go a little smoother. `$mail->send()` will not always return true (i.e. if sending fails), so testing its return value is reasonable. – Synchro Jun 26 '18 at 07:52

0 Answers0