0

I am trying to send an email for user to get confirmation code, i am 100% sure about my smtp settings are correct. But the problem now its failing to send mail, i dont seem to find any errors when debugging. What could be other reason am i getting this fail? What other possible reason could i not be seem to be sending this? the smtp settings i have used them last week for attaching the pdf document but not on sending email for otp confirmation.

// php code

<?php

use PHPMailer\PHPMailer\PHPMailer;

require_once(__DIR__ . '/sendEmails/vendor/phpmailer/phpmailer/src/Exception.php');
require_once(__DIR__ . '/sendEmails/vendor/phpmailer/phpmailer/src/PHPMailer.php');
require_once(__DIR__ . '/sendEmails/vendor/phpmailer/phpmailer/src/SMTP.php');


class VerificationCode
{
    public $smtpHost;
    public $sender;
    public $smtpPort;
    public $password;
    public $receiver;
    public $code;


    // some function to receive,send and port.

    public function _constructor($receiver) {
        $this->sender = "xxx@example.org";
        $this->password = "****";
        $this->smtpHost = "mail.example.com";
        $this->smtpPort = 587;

    }
    // function to send email().
    public function sendMail(){

        $mail= new PHPMailer();
        $mail->isSMTP();
        $mail->SMTPAuth = true;
        $mail->SMTPOptions = array(
            'ssl'=> array(
                'verify_peer'=>false,
                'verify_peer_name' => false,
                'allow_self_signed'=> true
            )
            );

            $mail->Host = $this->smtpHost;
            $mail->Port =$this->smtpPort;
            $mail->IsHTML(true);
            $mail->Username =$this->sender;
            $mail->Password= $this->password;
            $mail->Body=$this->getHTMLMessage();
            $mail->Subject = "Your verification code is {$this->code}";
            $mail->SetFrom($this->sender, "Verification Code");
            $mail->AddAddress($this->receiver);
            if($mail->send()) {
                echo "Mail Sent Successfully";
                exit;
            }
            echo "Failed to Send Mail";
    }

    // function to get html message from the email.
    public function getHTMLMessage() {
        $this->code=$this->getVerificationCode();
        $htmlMessage=<<<MSG
        <!DOCTYPE html>
        <html>
            <body>
                <h1>Your verification code is {$this->code}</h1>
                <p>Use this code to verify your account.</p>
            </body>
        </html>
        MSG;
        return $htmlMessage;
    }

    // get verificationCode.
    public function getVerificationCode() {
        return (int) substr(number_format(time() * rand(), 0,  '', ''), 0, 6);
    }

}

// instantiate VerificationCode and send email
$vc=new VerificationCode('gcira2023@outlook.com'); 
$vc->sendMail();

?>
Synchro
  • 35,538
  • 15
  • 81
  • 104

0 Answers0