Search code examples
phpemailsmtpgmail

smtp connect() failed in PHP using phpmailer Library


I have Found Error to send mail in PHP using phpmailer Library.any one can answer this Error. Thank you.

smtp connect() failed. https://github.com/phpmailer/phpmailer/wiki/troubleshooting

require_once APPPATH.'third_party/phpmailer/class.phpmailer.php';
require_once APPPATH.'third_party/phpmailer/class.smtp.php';

    $rtn = false;
    $mail = new PHPMailer();

    $mail->IsSMTP();
    $mail->SMTPDebug  = 0;
    $mail->SMTPAuth   = true;
    $mail->SMTPSecure = "tls";
    $mail->Host       = 'smtp.gmail.com';
    $mail->Port       = 587;
    $mail->Username   = '[email protected]';
    $mail->Password   = 'mypassword';
    $mail->SMTPOptions = array(
        'ssl' => array(
            'verify_peer' => false,
            'verify_peer_name' => false,
            'allow_self_signed' => true
        )
    );
    $mail->AddAddress($email);
    $mail->SetFrom('[email protected]', 'Smart Saver Support');
    $mail->AddReplyTo('[email protected]', 'Smart Saver Support');
    $mail->IsHTML(true);
    $mail->Subject = $subject;
    $mail->MsgHTML($msg);

    $rtn = $mail->Send();
    return $rtn;

Solution

  • Hey Do you Allow less secure apps to access your Gmail account ? if you not allow less secure apps enable and Run again and see the result.

    Here is the Example: https://devanswers.co/allow-less-secure-apps-access-gmail-account/