Skip to content

devkavin/otptest

Repository files navigation

Contributors Forks Stargazers Issues LinkedIn


Send Verification codes through Gmail.

Description

This is a simple tutorial on how to send a Verification mail with phpMyAdmin using PHPMailer through gmail.

If you haven't setup your gmail account to send SMTP mail. refer to the Help section.

Getting Started

Dependencies

Setting up Composer

  1. Download the Composer Setup.

  2. Run the default setup and make sure to enter the correct path to your php.exe file in the "/xampp/php" folder from directory you've installed xampp to.

  3. Once the Setup is complete, you should be able to check the composer version using a Windows Terminal or Command Prompt.

composer --version
  • If you're not getting the composer version on your terminal, restart your machine.

(back to top)

Setting up SMTP in XAMPP

  1. First, locate your XAMPP's php.ini file and open it with a text editor such as notepad++, sublime text or VSCode.

  2. In your text editor find the mail configuration by searching it using "[mail function]". Then, follow the configuration shown in the image below.

Note that "sendmail_from = " should have the email address that you want to send the OTP from.

  1. Next we'll edit the XAMPP's sendmail configuration. Locate and open the sendmail.ini file in your text editor. Then, follow the configuration shown in the image below.
  • Note: Use your gmail credential of the account you want use to send mail.

(back to top)

Create the Database

  1. In your browser, browse the XAMPP's PHPMyAdmin [http://localhost/phpmyadmin] and create a new database named otptest.

  2. Open the database and navigate the page to the SQL Tab and paste the following mysql script:

CREATE TABLE `user` (
  `user_id` int(11) NOT NULL,
  `name` varchar(255) NOT NULL,
  `email` varchar(255) NOT NULL,
  `otp` int(10) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

(back to top)

Executing program

Fork before testing!

  1. Click on the "Fork" button in the top-right corner of the page.

  2. Go to your forked repository on GitHub (it should be at https://github.com/{your-username}/otptest).

  3. Click on the green "Code" button and copy the HTTPS or SSH URL.

  4. Open a new window on VSCode and click Clone Git Repository then paste the copied link. Alternately you can open a terminal and use the following git clone command:

git clone https://github.com/{your-username}/otptest.git

(back to top)

Send the OTP

Import Composer

  1. Open the terminal and run the following command.
composer require phpmailer/phpmailer
  1. Copy the path to the <autoload.php> file in the generated "/vendor" folder.

  2. Navigate to send.php and paste the path as shown in the image below.

  • Your Path should look like its on the image below:

(back to top)

Create Config

  1. Create a new config.php

  2. Copy the following code into it.

<?php
define('SMTP_Username', 'yourSMTPemail@gmail.com');
define('SMTP_Password', 'yourSMTPpassword');
?>
  • Note: 'yourSMTPemail@gmail.com' and 'yourSMTPpassword' should be the email and password that you've set up on your gmail account that you want to send the OTP from.

  • If you're having trouble setting up your gmail account to send OTP, Refer to the Help section.

(back to top)

Lets send the OTP code!

  1. Run XAMPP and start the Apache and MySQL servers.

  2. On a browser of your choice, navigate to http://localhost/otptest/index.php

  3. Enter any text into the Test Info box. (Additional Text button to keep track of the test mail)

  4. Fill in your name and Email then click send. You should receive an email with the OTP code.

  • This sample code auto generates the OTP and stores it in the database.
  1. Verify the OTP
  • If you followed all the steps right, you should see the dashboard page.

(back to top)

Help

Gmail and SMTP

Google has removed the less secure apps option as of May 30th, 2022, and it is no longer possible to turn it on. To use Google's SMTP server, you have to enable 2-factor authentication and create an app password to use instead of your true password in the code.

Follow these steps to enable 2-factor authentication (2 Step verification) and create an app password:

  1. Go to your Google Account settings (https://myaccount.google.com/) and click on "Security" in the left sidebar.

  2. Scroll down to "Signing in to Google" and click on "2-Step Verification."

  3. Follow the prompts to enable 2-step verification for your Google account. This will require you to enter a code from your phone in addition to your password when signing in to Google.

  4. Once 2-step verification is enabled, scroll down to the "App passwords" section and click on "Generate new app password."

  1. Choose "Mail" as the app and select the device or application that you'll be using to send emails (e.g. "Windows Computer" or "PHPMailer").
  2. Click on "Generate" to generate a new app password. Copy this password and save it in a secure location.

(back to top)

Composer help

System Requirements:

Composer in its latest version requires PHP 7.2.5 to run. A long-term-support version (2.2.x) still offers support for PHP 5.3.2+ in case you are stuck with a legacy PHP version. A few sensitive php settings and compile flags are also required, but when using the installer you will be warned about any incompatibilities.

  • If you're having issues with composer, check whether the "composer\vendor\bin" path has been added to the Environment PATH variable as shown in the image below:
  • If you're still having issues with composer even after restarting your machine, follow these steps:

(back to top)

Manual Composer Setup

  1. Go to your XAMPP's php folder (\xampp\php)

  2. Create a text file in that folder and paste the following code:

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '55ce33d7678c5a611085589f1f3ddf8b3c52d662cd01d4ba75c0ee0459970c2200a51f492d557530c71c15d8dba01eae') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"
  1. Click save as, set the file type as "all files" and paste the following as the filename:
ComposerSetup.bat
  1. Save the file and run it.
  • This should install composer into your XAMPP's php folder. Next we have to add it to the Environment PATH variable.
  1. Search for "Environment variable" (without " ") on the Windows search bar and open System Properties.

  2. Click Environment Variables..., under user variables click Path and edit.

  3. Click New and add the path to the composer.phar file from the \xampp\php folder.

  • Restart your Machine and run the code below to check if it was installed properly:
composer --version

(back to top)

Contact

Find me on:

Twitter Hashnode LinkedIn

(back to top)

Version History

  • 0.1
    • Initial Release

(back to top)