Home > Software design >  Php mailer not working on localhost(liveserver)
Php mailer not working on localhost(liveserver)

Time:10-07

Guys why isn't my php mailer sending mails to my gmail I have written the same password as my gmal account and have less secure app access on but this isn't sending email to the entered email pls help every variable like the $code and email I'm giving to phpmailer is giving information This is the code

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
function sendMail($useremail,$code){
    require ("mail/PHPMailer.php");
    require ("mail/SMTP.php");
    require ("mail/Exception.php");

    $mail = new PHPMailer(true);
    try {
    //Server settings                   
    $mail->isSMTP();                                            //Send using SMTP
    $mail->Host       = 'smtp.gmail.com';                     //Set the SMTP server to send through
    $mail->SMTPAuth   = true;                                   //Enable SMTP authentication
    $mail->Username   = '[email protected]';                     //SMTP username
    $mail->Password   = '******';                               //SMTP password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;            //Enable implicit TLS encryption
    $mail->Port       = 465;                                    //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
    
    //Recipients
    $mail->setFrom('[email protected]', 'VoidPvP');
    $mail->addAddress($useremail);     //Add a recipient
   
    //Content
    $mail->isHTML(true);                                  //Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = "Thanks for registering at VoidPvP Website </br>
pls click to link below to get verified and enjoy the website</br>
<a href='https: /partials/mail/verify.php?email=$useremail&v_code=$code>verification link pls click here to verify</a>";
    
    
    $mail->send();
    return true;
  } catch (Exception $e) {
    return false;
  }
    }
require_once 'com/random_compat.phar';
if(isset($_POST['submits'])){
    require_once '_dbconnect.php';
    function sanitizeUserInput($userInput)
    {
        global $conn;

        return mysqli_escape_string($conn, stripcslashes($userInput));
    }

    # COLLECT POST VARIABLES
    $username = sanitizeUserInput($_POST['signupusername']);
    $useremail = sanitizeUserInput($_POST['signupemail']);
    $password = sanitizeUserInput($_POST['signuppassword']);
    
    $username = str_replace("<", "&lt;", "$username");
    $username = str_replace(">", "&gt;", "$username");
    $useremail = str_replace("<", "&lt;", "$useremail");
    $useremail= str_replace(">", "&gt;", "$useremail");
    $password = str_replace("<", "&lt;", "$password");
    $password = str_replace(">", "&gt;", "$password");

    $password = password_hash($password, PASSWORD_DEFAULT);
 $code = bin2hex(random_bytes(16));
    $sqlToCheckUserEmailExists = $conn->query("SELECT * FROM Users WHERE user_email = '$useremail'");
    $sqlTOCheckUsernameExists = $conn->query("SELECT * FROM Users WHERE user_name = '$username'");
    if(!$sqlToCheckUserEmailExists || !$sqlTOCheckUsernameExists)
    {
        echo "SQL FAILED HERE IS WHY " . mysqli_error($conn);
        
    }
    $doEmailExist = mysqli_num_rows($sqlToCheckUserEmailExists);
    $doUsernameExist = mysqli_num_rows($sqlTOCheckUsernameExists);
    if ($doEmailExist != 0) {
        $showError = "Email Is In Use";
        echo '<div class="alert alert-danger d-flex align-items-center my-0" role="alert">
<svg class="bi flex-shrink-0 me-2" width="24" height="24" role="img" aria-label="Success:"><use xlink:href="#check-circle-fill"/></svg>
<div>
<strong>Email already in use pls use a new one</strong>
</div>
</div>';
        
    }
    elseif($doUsernameExist != 0)
    {
        $showError = "Username Is In Use";
    echo '<div class="alert alert-danger d-flex align-items-center my-0" role="alert">
<svg class="bi flex-shrink-0 me-2" width="24" height="24" role="img" aria-label="Success:"><use xlink:href="#check-circle-fill"/></svg>
<div>
<strong>Username already in use pls use a new one</strong>
</div>
</div>';
        
    }
    else
    {
        $sqlToINsertUserIntoDatabase = $conn->query(("INSERT INTO Users (sno, user_name, user_email, user_pass, user_date, code, status) VALUES(NULL, '$username', '$useremail', '$password', CURRENT_TIMESTAMP, '$code', 0)"));
        if (!$sqlToINsertUserIntoDatabase) 
        {
            echo "FAILED TO INSERT USER INTO DATABSE, HERE IS WHY<b>" . mysqli_error($conn) . "</b>";

        }
        else
    {
      sendMail($useremail,$code);
    
              echo '<div class="alert alert-success d-flex align-items-center my-0" role="alert">
<svg class="bi flex-shrink-0 me-2" width="24" height="24" role="img" aria-label="Success:"><use xlink:href="#check-circle-fill"/></svg>
<div>
<strong>Successfully Signed up you can now login.</strong>
</div>
</div>';
        
            
        }
    }

}
?>

CodePudding user response:

Are you using your normal Gmail password or a google app password?

If you're using a Gmail account password you should use an app password I believe: https://support.google.com/mail/answer/185833?hl=en-GB

  • Related