Jump to content

PHP Mailer don't function, SMTP Error: Could not authenticate


DaryllB

Recommended Posts

Fatal error: Uncaught PHPMailer\PHPMailer\Exception: SMTP Error: Could not authenticate. in C:\xampp\htdocs\myproject\vendor\phpmailer\phpmailer\src\PHPMailer.php:2213 Stack trace: #0 C:\xampp\htdocs\myproject\vendor\phpmailer\phpmailer\src\PHPMailer.php(2020): PHPMailer\PHPMailer\PHPMailer->smtpConnect(Array) #1 C:\xampp\htdocs\myproject\vendor\phpmailer\phpmailer\src\PHPMailer.php(1679): PHPMailer\PHPMailer\PHPMailer->smtpSend('Date: Sun, 30 A...', '\r\n <h2>You h...') #2 C:\xampp\htdocs\myproject\vendor\phpmailer\phpmailer\src\PHPMailer.php(1517): PHPMailer\PHPMailer\PHPMailer->postSend() #3 C:\xampp\htdocs\myproject\code.php(44): PHPMailer\PHPMailer\PHPMailer->send() #4 C:\xampp\htdocs\myproject\code.php(57): sendemail_verify('Harivote User', 'harivoteuser@gm...', 'HariVoteuser0') #5 {main} thrown in C:\xampp\htdocs\myproject\vendor\phpmailer\phpmailer\src\PHPMailer.php on line 2213

 

<?php
session_start();
include "dbcon.php";
//Import PHPMailer classes into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

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

/**
 * Summary of sendemail_verify
 * @param mixed $name
 * @param mixed $email
 * @param mixed $verify_token
 * @return void
 */
function sendemail_verify($name,$email,$verify_token)
{
    $mail = new PHPMailer(true);

    $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   = 'harivoteuser@gmail.com';                //SMTP username
    $mail->Password   = 'Sec_ret123';                            //SMTP password
    $mail->SMTPSecure = "PHPMailer::ENCRYPTION_STARTTLS";        //Enable implicit TLS encryption
    $mail->Port       = 587;                                     //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
    
    $mail->setFrom("harivoteuser@gmail.com", $name);
    $mail->addAddress($email);                                   //Add a recipient
    
    $mail->isHTML(true);                                         //Set email format to HTML
    $mail->Subject = 'Email Verification from HariVote';

    $email_template = "
    <h2>You have Registered with Harivote</h2>
    <h5>Verify your email address to Login with the below given link </h5>
    <br/><br/>
    <a href='http://http://localhost/myproject/verify-email.php?token=$verify_token'>Click Me </a> 
    ";
    $mail->Body = $email_template;
    $mail->send();
    //echo 'Message has been sent';
    
}

if(isset($_POST['register_btn']))
{
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$password = md5(rand());
$verify_token = $_POST['password'];

sendemail_verify("$name","$email","$verify_token");
echo 'send or not?';
//Email exists or not
    $check_email_query = "SELECT email FROM users WHERE email='$email'LIMIT 1";
    $check_email_query_run = mysqli_query($con, $check_email_query);

    if(mysqli_num_rows($check_email_query_run)>0)
    {
        $_SESSION['status'] = "Email Id already Exists";
        header("Location: register.php");
    }
    else
    {
    // Inswer User / Registered User Data
    $query = "INSERT INTO users (name,phone,email,password,verify_token) VALUES ('$name','$phone','$email','$password','$verify_token',)";
    $query_run = mysqli_query($con,$query);
        if($query_run)
        {   
            sendemail_verify("$name","$email","$verify_token");
            $_SESSION['status'] = "Registration Successful.! Please verify your Email Address.";
        header("Location: register.php");
        }
        else
        {
            $_SESSION['status'] = "Registration Failed";
        header("Location: register.php");
        }
    }


}


?>

 

Link to comment
Share on other sites

 $mail->SMTPSecure = "PHPMailer::ENCRYPTION_STARTTLS";        

should be this I believe

$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;

Incorrect URL

<a href='http://localhost/myproject/verify-email.php?token=$verify_token'>Click Me </a>

Instead of using md5(rand()) for generating the password, you should use PHP's built-in password hashing functions like password_hash().

You have two calls to the sendemail_verify() function. Remove the first one (before the "send or not?" echo statement) as it is redundant and might lead to confusion.

You are using $_POST['password'] as the value for the $verify_token variable. Instead, you should generate a random token using functions like bin2hex(random_bytes($length)).

Edited by Strider64
  • Great Answer 1
Link to comment
Share on other sites

I did some improvements but still got this fatal error.

Fatal error: Uncaught mysqli_sql_exception: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')' at line 1 in C:\xampp\htdocs\myproject\code.php:66 Stack trace: #0 C:\xampp\htdocs\myproject\code.php(66): mysqli_query(Object(mysqli), 'INSERT INTO use...') #1 {main} thrown in C:\xampp\htdocs\myproject\code.php on line 66.

 

What to do next?

 

<?php
session_start();
include "dbcon.php";
//Import PHPMailer classes into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

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


function sendemail_verify($name,$email,$verify_token)
{
    $mail = new PHPMailer(true);

    $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   = 'harivoteuser@gmail.com';                //SMTP username
    $mail->Password   = 'Sec_ret123';                            //SMTP password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;        //Enable implicit TLS encryption
    $mail->Port       = 587;                                     //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
    
    $mail->setFrom("harivoteuser@gmail.com", $name);
    $mail->addAddress($email);                                   //Add a recipient
    
    $mail->isHTML(true);                                         //Set email format to HTML
    $mail->Subject = 'Email Verification from HariVote';

    $email_template = "
    <h2>You have Registered with Harivote</h2>
    <h5>Verify your email address to Login with the below given link </h5>
    <br/><br/>
    <a href='http://localhost/myproject/verify-email.php?token=$verify_token'>Click Me </a>
    ";
    $mail->Body = $email_template;
    $mail->send();
    //echo 'Message has been sent';
    
}

if(isset($_POST['register_btn']))
{
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$verify_token = $_POST['password'];
$password = md5(rand());



//Email exists or not
    $check_email_query = "SELECT email FROM users WHERE email='$email'LIMIT 1";
    $check_email_query_run = mysqli_query($con, $check_email_query);

    if(mysqli_num_rows($check_email_query_run)>0)
    {
        $_SESSION['status'] = "Email Id already Exists";
        header("Location: register.php");
    }
    else
    {
    // Inswer User / Registered User Data
    $query = "INSERT INTO users (name,phone,email,password,verify_token) VALUES ('$name','$phone','$email','$password','$verify_token',)";
    $query_run = mysqli_query($con,$query);
        if($query_run)
        {   
            sendemail_verify("$name","$email","$verify_token");
            $_SESSION['status'] = "Registration Successful.! Please verify your Email Address.";
        header("Location: register.php");
        }
        else
        {
            $_SESSION['status'] = "Registration Failed";
        header("Location: register.php");
        }
    }


}


?>

 

Edited by DaryllB
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.