I have been trying to set up a welcome email using phpmailer. I am facing Username and Password not accepted and Message could not be sent. Mailer Error: SMTP Error: Could not authenticate. Error. My email and password are very simple no special characters were used, tried all possible solutions on stack overflow and other sites. Disabled Unlock Captcha, tried to allow access to less secure apps and whatnot. The code is attached below.
Kindly help. I apologize if this has been asked already, I need help, Thanks in Advance
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
session_start();
if (isset($_POST['username'])) {
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_NAME', 'rakaposhi');
// Create connection
$conn = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$username = mysqli_real_escape_string($conn,$_POST['username']);
$pswd = mysqli_real_escape_string($conn,md5($_POST['pswd']));
$name = mysqli_real_escape_string($conn,$_POST['name']);
$email = mysqli_real_escape_string($conn,$_POST['email']);
$phone = mysqli_real_escape_string($conn,$_POST['phone']);
$token = bin2hex(random_bytes(15));
$timestamp;
$status = "Inactive";
$sql = "SELECT username FROM userdata WHERE username = '{$username}'";
if ($result = mysqli_query($conn, $sql)) {
if (mysqli_num_rows($result) > 0) {
echo "Username already exists";
}
else {
$sql = "INSERT INTO userdata (username, pswd, name, email, phone, token, status)
VALUES ('{$username}', '{$pswd}', '{$name}', '{$email}', '{$phone}', '{$token}', '{$status}')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
//Load Composer's autoloader
require 'vendor/autoload.php';
//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER; //Enable verbose debug output
$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 = ''; // Removed Password for security purpose hereSMTP 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]', 'Rakaposhi');
$mail->addAddress($email, $username); //Add a recipient
// $mail->addAddress('[email protected]'); //Name is optional
$mail->addReplyTo('[email protected]', 'Rakaposhi Information');
// $mail->addCC('[email protected]');
// $mail->addBCC('[email protected]');
//Attachments
// $mail->addAttachment('/var/tmp/file.tar.gz'); //Add attachments
// $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); //Optional name
//Content
$mail->isHTML(true); //Set email format to HTML
$mail->Subject = 'Welcome - Activation Link';
$mail->Body = 'Activate kr na bhai';
$mail->AltBody = 'kr le bhai';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
}
else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
}
else {
echo "Oops! Something went wrong. Please try again later.";
}
// mysqli_close($conn);
// header("Location: index.php");
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<title>HTML 5 Boilerplate</title>
<!-- Custom CSS -->
<link rel="stylesheet" href="app.css">
</head>
<body>
<div class="container">
<div class="row">
<section class="vh-100">
<div class="container h-100">
<div class="row d-flex justify-content-center align-items-center h-100">
<div class="col-lg-12 col-xl-11">
<div class="card text-black" style="border-radius: 25px;">
<div class="card-body p-md-5">
<div class="row justify-content-center">
<div class="col-md-10 col-lg-6 col-xl-5 order-2 order-lg-1">
<p class="text-center h1 fw-bold mb-5 mx-1 mx-md-4 mt-4">Sign up</p>
<form class="mx-1 mx-md-4" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<div class="d-flex flex-row align-items-center mb-4">
<div class="form-outline flex-fill mb-0">
<input type="text" id="form3Example1c" class="form-control" placeholder="Username" name="username" required>
</div>
</div>
<div class="d-flex flex-row align-items-center mb-4">
<div class="form-outline flex-fill mb-0">
<input type="password" id="form3Example1c" class="form-control" placeholder="Password" name="pswd" required>
</div>
</div>
<div class="d-flex flex-row align-items-center mb-4">
<div class="form-outline flex-fill mb-0">
<input type="text" id="form3Example1c" class="form-control" placeholder="Full Name" name="name" required>
</div>
</div>
<div class="d-flex flex-row align-items-center mb-4">
<div class="form-outline flex-fill mb-0">
<input type="email" id="form3Example1c" class="form-control" placeholder="Email" name="email" required>
</div>
</div>
<div class="d-flex flex-row align-items-center mb-4">
<div class="form-outline flex-fill mb-0">
<input type="phone" id="form3Example1c" class="form-control" placeholder="Phone" name="phone" required>
</div>
</div>
<div class="form-check d-flex mb-5">
<input class="form-check-input" type="checkbox" value="" id="flexCheckChecked" checked>
<label class="form-check-label" for="flexCheckChecked">
I agree to <a href="#">Terms and Conditions</a>
</label>
</div>
<div class="d-flex justify-content-center mx-4 mb-3 mb-lg-4">
<button type="submit" class="btn btn-primary btn-lg">Register</button>
</div>
</form>
</div>
<div class="col-md-10 col-lg-6 col-xl-7 d-flex align-items-center order-1 order-lg-2">
<img src="imgs/cover2.webp"
class="img-fluid" alt="Sample image">
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
<br><br><br><br></div>
</div>
<!-- JS Popper Bootstrap -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"
integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"
integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy"
crossorigin="anonymous"></script>
</body>
</html>
With gmail now you need to create a app password to send emails and allow less secure apps no longer works. Look at instructions here: https://codecr8.com/2022/06/10/google-smtp/
Basically you need to log into your gmail account and under security settings you need to enable 2 step verification. Once thats done you will need to generate a app password which will be the password used in your settings for the account password for smtp settings