DaryllB Posted April 30, 2023 Share Posted April 30, 2023 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"); } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/316222-php-mailer-dont-function-smtp-error-could-not-authenticate/ Share on other sites More sharing options...
Strider64 Posted April 30, 2023 Share Posted April 30, 2023 (edited) $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 April 30, 2023 by Strider64 1 Quote Link to comment https://forums.phpfreaks.com/topic/316222-php-mailer-dont-function-smtp-error-could-not-authenticate/#findComment-1607829 Share on other sites More sharing options...
requinix Posted April 30, 2023 Share Posted April 30, 2023 $password = md5(rand()); $verify_token = $_POST['password']; Isn't that backwards? Quote Link to comment https://forums.phpfreaks.com/topic/316222-php-mailer-dont-function-smtp-error-could-not-authenticate/#findComment-1607836 Share on other sites More sharing options...
DaryllB Posted May 2, 2023 Author Share Posted May 2, 2023 (edited) 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 May 2, 2023 by DaryllB Quote Link to comment https://forums.phpfreaks.com/topic/316222-php-mailer-dont-function-smtp-error-could-not-authenticate/#findComment-1607905 Share on other sites More sharing options...
Barand Posted May 2, 2023 Share Posted May 2, 2023 You have one comma too many. Quote Link to comment https://forums.phpfreaks.com/topic/316222-php-mailer-dont-function-smtp-error-could-not-authenticate/#findComment-1607911 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.