The Web Blinders logo

Programming

PHP - How to send Verification code or OTP using PHPMailer with out composer?

Usually, To verify identity of a user ,we will be sending a verification code or a link to users email account.In this article you will see how to do it
Demo of Verification code sender script
Demo of the verification code sender script.

PHPMailer is widely used for sending emails.

STEP 1 : INSTALLING PHPMAILER WITHOUT COMPOSER

Go to the official website , download and extract source files to your project directory . In this article we are using PHPMailer 6.0.3

STEP 2 : DIRECTORY STRUCTURE

Keep your directory structure as shown in the below image and create a index.php file inside directory.

Directory structure of our project
Directory structure of our project.

STEP 3 : MAKE CHANGES TO index.php

Copy below code to your index.php file and make appropriate changes to it, as mentioned in the comments of __constructor.

                            
<?php

use PHPMailer\PHPMailer\PHPMailer;

require 'PHPMailer-6.0.5/src/Exception.php';
require 'PHPMailer-6.0.5/src/PHPMailer.php';
require 'PHPMailer-6.0.5/src/SMTP.php';

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

    public function __construct($receiver)
    {
        /**
         * Your email id  
         * For example : johndoe@gmail.com
         * contact@johndoe.com
         * 
         */
        $this->sender = ""; 

        /**
         *  YOUR PASSWORD 
         *  ************
         */               
        $this->password = "";  

        /**
         * Receiver email
         * For example : johndoe@gmail.com
         */     
        $this->receiver = $receiver;  

        /**
         * YOUR SMTP HOST NAME 
         * For example : smtp.gmail.com 
         * OR mail.youwebsite.com
         */     
        $this->smtpHost="";        
        
        /**
         * SMTP port number
         * For example :587
         */
        $this->smtpPort = 587;

    }
    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 Codes");
        $mail->AddAddress($this->receiver);
        if($mail->send()){
          echo "MAIL SENT SUCCESSFULLY";
          // return true;
          exit;  
        }
        echo "FAILED TO SEND MAIL";
        // return false;

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

    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;
    }

}

// pass your recipient's email
$vc=new VerificationCode('johndoe@xxxmail.com'); 
$vc->sendMail(); // MAIL SENT SUCCESSFULLY

Now run index.php, if the mail is sent , you will get a message MAIL SENT SUCCESSFULLY.

You can download files from the Github Repo https://github.com/thewebblinders/how-to-send-otp-verification-codes-as-mail-php

Use comments section for any queries.

Need developers ?

if so, send a message.

thewebblinders@gmail.com

More Programming from our blog

SEARCH FOR ARTICLES