kreya
kreya

Reputation: 1229

SMTP ERROR: Failed to connect to server: (0) SMTP connect() failed

I am trying to send an email using Mailer but getting below error

Connection: opening 2017-05-25 08:22:07 SMTP ERROR: Failed to connect to server: (0) SMTP connect() failed. Mailer Error: SMTP connect() failed.

php_openssl extension & IMAP both are enabled. I tried to find it on google but still no luck.

Code:

function sendMail($subject='',$to='',$emailcontent='',$attach='')
{
    global $_mailmsg;
    //echo $emailcontent;exit;
    $mail = new PHPMailer;
    $mail->SMTPDebug = 4;
    $mail->isSMTP();                                    
    $mail->Host = 'smtp.gmail.com';
    $mail->Port = '465';
    $mail->SMTPAuth = true;                               
    $mail->Username = '[email protected]';                            
    $mail->Password = 'xxxx';                           
    $mail->SMTPSecure = 'ssl';                                  
    $mail->From = '[email protected]';
    $mail->FromName = 'Test';
    $mail->addAddress($to);  // Add a recipient
    if(!empty($cc)){    $mail->addCC($cc); }
    if(!empty($bcc)){   $mail->addBCC($bcc);    }

    $mail->WordWrap = 50;                                 
    if($attach != ''){
        $mail->addAttachment($attach);    
    }
    //$mail->addAttachment('/tmp/image.jpg', 'new.jpg');   
    $mail->isHTML(true);                                  
    $mail->Subject = $subject;
    $mail->Body    = 'Test';
    try
    {
        if($mail->send()) {
            return 1;
           exit;
        }
        else
        {
            echo 'Mailer Error: ' . $mail->ErrorInfo;
            return 0;
        }
    }
    catch(Exception $e)
    {
        return 0;   
    }
}

Upvotes: 1

Views: 6010

Answers (2)

Vladan
Vladan

Reputation: 903

As you are using Gmail, just turn on "Allow less secure apps":

https://myaccount.google.com/u/0/lesssecureapps

And also you'll probably need to allow access to your Google account without unlock captcha:

https://accounts.google.com/DisplayUnlockCaptcha

Upvotes: 0

Synchro
Synchro

Reputation: 37710

This looks like your server is not permitted to connect to remote SMTP servers, something very common at big ISPs like GoDaddy. If you do the steps described in the troubleshooting guide you can figure out what's blocking you. The fact that there is no link to the guide in your error message tells me that you're using a very old version of PHPMailer, so get the latest.

PHPMailer has nothing to do with IMAP; that's for inbound mail only.

Upvotes: 1

Related Questions