Jump to content

PHP Mailer not working


Naim

Recommended Posts

Hi team,

Can someone help with the code below? I am not getting to the OTP page. The email account is in godaddy cpanel. The app is hosted in godaddy also.

<?php

session_start();

include "connection.php";

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

//Load Composer's autoloader
require 'vendor/autoload.php';


function send_verification($fname, $lname, $email, $token){

    $mail = new PHPMailer(true);

        $mail->isSMTP();
        $mail->SMTPAuth  = false;
        $mail->Port = 25;
        $mail->Host = "localhost";
        $mail->Username = "username"; //SMTP username
        $mail->Password = "pass";
        
        $mail->IsSendMail(); 
        
        $mail->From = "username";
        $mail->FromName = "Name";
        
        $mail->addAddress($email);
        
        
        $mail->Subject = " OTP Verification!";
        $mail->WordWrap = 80;
        
        $email_template = "
            <h5>Here's Your Security Code:</h5>
            <h4 style='color: green;'>$token</h4><br/>

            <h5> 
            Hello $fname $lname <br/><br/>

            We've recieved a security request from your account. Please use the code above to login to your account.<br/><br/>

            Regards,
 
            </h5>
            ";
        
          
        $mail->MsgHTML($email_template);
        $mail->isHTML(true);    
        $mail->send();
    
    if(!$mail->send()){
        echo $mail->ErrorInfo;
    }

}

if(isset($_POST['username']) && isset($_POST['password'])) {

    function validate($data) {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }

$username = validate($_POST['username']);
$password = validate($_POST['password']);
$otp_value = rand(1000, 9999);


if(empty($username)){
    header ("Location: index.php?error=Username is required!");
    exit();
}

else if(empty($password)) {
    header ("Location: index.php?error=Password is required!");
    exit();
}

$sql = "SELECT * FROM login_js WHERE email='$username' AND password='$password'";

$result = mysqli_query($conn, $sql);

if(mysqli_num_rows($result) === 1) {
    $row = mysqli_fetch_assoc($result);
    if($row['email'] === $username && $row['password'] === $password) {
        $_SESSION['id'] = $row['id'];
        $_SESSION['jsid'] = $row['jsid'];
        $_SESSION['email'] = $row['email'];
        
        $jsid = $row['jsid'];
        $fname = $row['firstmiddlenames'];
        $lname = $row['lastname'];
        $email = $row['email'];
        
        
        $sql2 = "UPDATE login_js SET loginOTP='$otp_value' WHERE email = '$username'";

        $result2 = mysqli_query($conn, $sql2);

        if($result2){
        send_verification("$fname", "$lname", "$email", "$otp_value");
        header ("Location: verification.php?email=".$email);
        exit();
    } else {
        header ("Location: index.php?error=problem updating otp");
    }
    } else {
        header ("Location: index.php?error=Incorrect Username or Password!");
        exit();
    }
}
} else {
    header ("Location: index.php?error=Cannot Find the Username and Password");
    exit();
}
?>
Link to comment
Share on other sites

We get these type of questions all the time.  I'm not sure what you mean by  "I am not getting to the OTP page" or what this has to do with phpmailer. 

If emails are not going out as expected, one reason would be that this wouldn't work with godaddy, as you'd need the hostname and whatever other credentials for their mail server.  

 

$mail->isSMTP();
$mail->SMTPAuth  = false;
$mail->Port = 25;
$mail->Host = "localhost";

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.