I want to have a simple email verification system on my website.
I was told to use PHPMailer to do this but sending an actual email seems to always fail and I cannot figure out what a lot of the configuration possibilities are actually doing.
Here is the error I'm getting:
2019-10-10 09:05:53 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP l19sm817499edb.50 - gsmtp
2019-10-10 09:05:53 CLIENT -> SERVER: EHLO localhost
2019-10-10 09:05:53 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [2a02:aa10:e27d:d780:d905:c422:7d5d:365d]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2019-10-10 09:05:53 CLIENT -> SERVER: STARTTLS
2019-10-10 09:05:53 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
2019-10-10 09:05:53 CLIENT -> SERVER: EHLO localhost
2019-10-10 09:05:53 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [2a02:aa10:e27d:d780:d905:c422:7d5d:365d]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2019-10-10 09:05:53 CLIENT -> SERVER: AUTH LOGIN
2019-10-10 09:05:53 SERVER -> CLIENT: 334 VXNlcm5hbWU6
2019-10-10 09:05:53 CLIENT -> SERVER: <credentials hidden>
2019-10-10 09:05:53 SERVER -> CLIENT: 334 UGFzc3dvcmQ6
2019-10-10 09:05:53 CLIENT -> SERVER: <credentials hidden>
2019-10-10 09:05:53 SERVER -> CLIENT: 535-5.7.8 Username and Password not accepted. Learn more at535 5.7.8 https://support.google.com/mail/?p=BadCredentials l19sm817499edb.50 - gsmtp
2019-10-10 09:05:53 SMTP ERROR: Password command failed: 535-5.7.8 Username and Password not accepted. Learn more at535 5.7.8 https://support.google.com/mail/?p=BadCredentials l19sm817499edb.50 - gsmtp
SMTP Error: Could not authenticate.
2019-10-10 09:05:53 CLIENT -> SERVER: QUIT
2019-10-10 09:05:53 SERVER -> CLIENT: 221 2.0.0 closing connection l19sm817499edb.50 - gsmtp
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Message has been sent
This is the error I get if I try to do it with the localhost. I'm using XAMPP with Apache and MySql but I haven't installed 'Mercury' because I don't really understand how all of this works.
In the end I want this to be hosted online here: OpenStringStudios so I don't understand what 'Mercury' has to do with anything in this regard.
Here is the php code I'm using:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
// echo "Hello World!";
if (isset($_POST['submit'])) {
$con = new mysqli('localhost', 'root', '', 'osscom_users_test');
$name = $con->real_escape_string($_POST['name']);
$email = $con->real_escape_string($_POST['email']);
$pass = $con->real_escape_string($_POST['pass']);
// test if the username is already taken
// one email addresses can have multiple users
$token = 'qwertzuiopasdfghjklyxcvbnmQWERTZUIOPASDFGHJKLYXCVBNM0123456789';
$token = str_shuffle($token);
$token = substr($token, 0, 10);
$hashedPass = password_hash($pass, PASSWORD_BCRYPT);
$con->query("INSERT INTO test_users (NAME, EMAIL, PASSWORD, VERIFIED, TOKEN)
VALUES ('$name', '$email', '$hashedPass', '0', '$token');
");
include_once "PHPMailer/PHPMailer.php";
include_once "PHPMailer/Exception.php";
include_once "PHPMailer/SMTP.php";
try {
// Server settings
$mail = new PHPMailer();
$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 = 'root'; // SMTP username
$mail->Password = ''; // SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('openstring.studios@gmail.com', 'OpenString Team');
$mail->addAddress($email, $name);
$mail->isHTML(true);
$mail->Subject = "Welcome To The OpenString Membership, Please Verify Your Account";
$mail->Body = "
<h1>Welcome To The OpenString Membership</h1>
Thanks for joining, please click the link below to verify your account.<br>
Name: $name<br>
<a href='localhost/session/activation?email=$email&token=$token'>Click Here To Activate Your Account.</a>
<p>
Legal Statements:
By clicking this link you agree to all our terms of services.
</p>
";
$mail->send();
echo 'Message has been sent';
} catch(Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrofInfo}";
}
// Redirect('account', false);
}
function Redirect($url, $permanent = false) {
if (headers_sent() === false) {
header('Location: ' . $url, true, ($permanent === true) ? 301 : 302);
}
exit();
}
?>
I don't really understand how I can solve this issue. Especially important to me would be that it can both work for my localhost and for when it's hosted on domain.com.
You have all you need here:
535-5.7.8 Username and Password not accepted. Learn more at535 5.7.8 https://support.google.com/mail/?p=BadCredentials l19sm817499edb.50 - gsmtp
You need set your login and pass from your gmail box.