0

I am using php mailer to send mail. It is working on localhost. But not working on live cpanel hosting.

Mail is sending proper in my localhost. In live server I found error.

Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

    public function send_mail($to,$bodytext,$subject)
 {
    try{
    // start mailer
    $toAddress = $to;
    $message = $bodytext;
    $mail = new PHPMailer();
    $mail->IsSMTP();
    $mail->SMTPAuth    = true;
    $mail->Host        = "smtp.gmail.com";
    $mail->Port        = 587;
    $mail->SMTPOptions = array(
        'ssl' => array(
            'verify_peer' => false,
            'verify_peer_name' => false,
            'allow_self_signed' => true
        )
    );
    $mail->IsHTML(true);
    $mail->Username = "xxxxxx@gmail.com"; // your gmail address
    $mail->Password = "xxxxx@123"; // password
    $mail->SetFrom("xxxxxx@gmail.com");
    $mail->Subject = $subject; // Mail subject
    $mail->Body = $message;
    $mail->AddAddress($toAddress);
    if (!$mail->Send()) {

        echo "Mailer Error: " . $mail->ErrorInfo;
        exit;
       return False;

    } else {
        return true;
    }
    // end mailer
    }
    catch (Exception $e)
    {
        return $e->getMessage();
    } 
}
Vinod
  • 123
  • 1
  • 3
  • 14
  • Does this answer your question? [PHPMailer, SMTP connect() failed error with Gmail](https://stackoverflow.com/questions/25924651/phpmailer-smtp-connect-failed-error-with-gmail) – Simone Rossaini Jun 03 '20 at 07:01
  • no. It is not solving my error. – Vinod Jun 03 '20 at 07:13
  • So what were the results of all the tests you tried having read the troubleshooting guide? – Synchro Jun 03 '20 at 07:14
  • You have to authorize in your Gmail account the less secure application to send e-mail. If you don't authrorize it Gmail block all connection from, custom application or other application like Outlook. – Inazo Jun 03 '20 at 07:19
  • I did less secure app. That is why in my localhost mail is sending. But in live, it is not sending. – Vinod Jun 03 '20 at 07:36
  • I created email on webmail of server and put webmail cresentials in code. Now it is working. – Vinod Jun 03 '20 at 08:14

0 Answers0