How to conifgure sending mail on NginX Amazon Web Services lightsail web server with PHP and verify the email with md5 HASH



Share this page:



  1. First you will need to download the SMTP PEAR modules files required as shown on this page on your NginX AWS Lightsail web server.


  2. MySQL Workbench intrucrions

    First line:Create the database woth the following MySQL command changing {DATABASE-NAME} for the database name you would like


    Second line:Telling MySQL Workbench whichj database we would like to use

    Third SQL command:Creating the table within your database with the following values

    
      
    
      
    CREATE DATABASE {DATABASE-NAME};
    
    USE {DATABASE-NAME};
    
    CREATE TABLE {TABLE-NAME} (
      id INT AUTO_INCREMENT PRIMARY KEY,
      email VARCHAR(255) NOT NULL,
      verified BOOLEAN DEFAULT 0
    );
      
    

  3. Extending the code from section 2. We can store the email submitted by the user in our databse for email subscriptions and then give that entry to the database a verify value which is boolean (0 OR 1). The value will automatically set to 0 and when the user clicks the verification link in their email inbox it will direct them to the verify.php page and then our php code on that page will compare the md5 HASH to the users email HASH. If they match it will change the verify value for that email to a 1

    action_page.php
      
        
      
        
    <?php
    
    require_once "Mail.php";
    
    $servername = "{ENDPOINT-TO-MYSQL-DATABASE}";
    $username = "{USERNAME-FOR-DATABASE}";
    $password = "{PASSWORD-FOR-DATABASE}";
    $dbname = "{DATABASE-NAME}";
    
    $vemail = $_POST['email'];
    
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
      die("Connection failed: " . $conn->connect_error);
    }
    echo "Connected successfully";
    
    $sql = "INSERT INTO subs (email)
    VALUES ('{$vemail}')";
    
    if ($conn->query($sql) === TRUE) {
      echo "New record created successfully";
    
          // Send verification email
          $verificationCode = md5($vemail);
          $verificationLink = "https://computersciencex.com/verify.php?code=$verificationCode";
          $subject = "Verify Your Email Address";
          $message = "Click the link below to verify your email address:\n$verificationLink";
    
    
    } else {
      echo "Error: " . $sql . "
    " . $conn->error; } $from = "admin@computersciencex.com"; $to = $vemail; $host = "ssl://smtp.zoho.eu"; $port = "465"; $username = 'admin@computersciencex.com'; $password = '{PASSWORD}'; $body = $message; $headers = array ('From' => $from, 'To' => $to,'Subject' => $subject); $smtp = Mail::factory('smtp', array ('host' => $host, 'port' => $port, 'auth' => true, 'username' => $username, 'password' => $password)); $mail = $smtp->send($to, $headers, $body); if (PEAR::isError($mail)) { echo($mail->getMessage()); } else { echo("Message successfully sent!\n"); } header("Location: https://computersciencex.com/indexSuccess.html", true, 301); $conn->close(); ?>

  4. Now create a php file called verify.php and add the following code(ensuring that you change the variable values to your own):

    verify.php
      
        
      
        
    <?php
    
    $servername = "{ENDPOINT-TO-MYSQL-DATABASE}";
    $username = "{USERNAME-FOR-DATABASE}";
    $password = "{PASSWORD-FOR-DATABASE}";
    $dbname = "{DATABASE-NAME}";
    
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
      die("Connection failed: " . $conn->connect_error);
    }
    echo "Connected successfully";
    
    if (isset($_GET['code'])) {
        $code = $_GET['code'];
        
        // Update user status to verified
        $sql = "UPDATE subs SET verified = 1 WHERE MD5(email) = '$code'";
    
        
        if ($conn->query($sql) === TRUE) {
            header("Location: https://computersciencex.com/indexVerify.html", true, 301);  
    
        } else {
            echo "Error verifying email: " . $conn->error;
        }
    }
    
    
    $conn->close();
    
    ?>
      
    

How to conifgure sending mail on NginX Amazon Web Services lightsail web server with PHP and verify the email with md5 HASH


Page Sections:

  1. Download the SMTP files required
  2. MySQL Workbench intrucrions
  3. Javscript example
  4. Add users email to MySQL DB for subscriptions and then give that entry to the database a verify value

Share this page:



  1. First you will need to download the SMTP PEAR modules files required as shown on this page on your NginX AWS Lightsail web server.


  2. MySQL Workbench intrucrions

    First line:Create the database woth the following MySQL command changing {DATABASE-NAME} for the database name you would like

    Second line:Telling MySQL Workbench whichj database we would like to use

    Third SQL command:Creating the table within your database with the following values

    
    
    
    
    CREATE DATABASE {DATABASE-NAME};
    
    USE {DATABASE-NAME};
    
    CREATE TABLE {TABLE-NAME} (
    id INT AUTO_INCREMENT PRIMARY KEY,
    email VARCHAR(255) NOT NULL,
    verified BOOLEAN DEFAULT 0
    );
    
    

  3. Extending the code from section 2. We can store the email submitted by the user in our databse for email subscriptions and then give that entry to the database a verify value which is boolean (0 OR 1). The value will automatically set to 0 and when the user clicks the verification link in their email inbox it will direct them to the verify.php page and then our php code on that page will compare the md5 HASH to the users email HASH. If they match it will change the verify value for that email to a 1

    action_page.php
    
    
    
    
    <?php
    
    require_once "Mail.php";
    
    $servername = "{ENDPOINT-TO-MYSQL-DATABASE}";
    $username = "{USERNAME-FOR-DATABASE}";
    $password = "{PASSWORD-FOR-DATABASE}";
    $dbname = "{DATABASE-NAME}";
    
    $vemail = $_POST['email'];
    
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
    }
    echo "Connected successfully";
    
    $sql = "INSERT INTO subs (email)
    VALUES ('{$vemail}')";
    
    if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
    
      // Send verification email
      $verificationCode = md5($vemail);
      $verificationLink = "https://computersciencex.com/verify.php?code=$verificationCode";
      $subject = "Verify Your Email Address";
      $message = "Click the link below to verify your email address:\n$verificationLink";
    
    
    } else {
    echo "Error: " . $sql . "
    " . $conn->error; } $from = "admin@computersciencex.com"; $to = $vemail; $host = "ssl://smtp.zoho.eu"; $port = "465"; $username = 'admin@computersciencex.com'; $password = '{PASSWORD}'; $body = $message; $headers = array ('From' => $from, 'To' => $to,'Subject' => $subject); $smtp = Mail::factory('smtp', array ('host' => $host, 'port' => $port, 'auth' => true, 'username' => $username, 'password' => $password)); $mail = $smtp->send($to, $headers, $body); if (PEAR::isError($mail)) { echo($mail->getMessage()); } else { echo("Message successfully sent!\n"); } header("Location: https://computersciencex.com/indexSuccess.html", true, 301); $conn->close(); ?>

  4. Now create a php file called verify.php and add the following code(ensuring that you change the variable values to your own):

    verify.php
    
    
    
    
    <?php
    
    $servername = "{ENDPOINT-TO-MYSQL-DATABASE}";
    $username = "{USERNAME-FOR-DATABASE}";
    $password = "{PASSWORD-FOR-DATABASE}";
    $dbname = "{DATABASE-NAME}";
    
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
    }
    echo "Connected successfully";
    
    if (isset($_GET['code'])) {
    $code = $_GET['code'];
    
    // Update user status to verified
    $sql = "UPDATE subs SET verified = 1 WHERE MD5(email) = '$code'";
    
    
    if ($conn->query($sql) === TRUE) {
        header("Location: https://computersciencex.com/indexVerify.html", true, 301);  
    
    } else {
        echo "Error verifying email: " . $conn->error;
    }
    }
    
    
    $conn->close();
    
    ?>