PHPMailer help

I’m trying to send an email using this…

I renamed the folder (to PHPMailer) and put in in the root

Here is my test

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require '../PHPMailer/src/Exception.php';
require '../PHPMailer/src/PHPMailer.php';
require '../PHPMailer/src/SMTP.php';


if(isset($_POST['Register'])) {

    try {

        if($_POST['RegisterName'] == '') {
            throw new Exception("First name can not be empty");

        if($_POST['RegisterEmail'] == '') {
            throw new Exception("Email can not be empty");

        if(!filter_var($_POST['RegisterEmail'],FILTER_VALIDATE_EMAIL)) {
            throw new Exception("Email is invalid");

        $statement = $pdo->prepare("SELECT email FROM users WHERE email=?");
        $total = $statement->rowCount();
        if($total) {
            throw new Exception("Email already exists");

        if($_POST['RegisterPassword'] == '' || $_POST['RetypePassword'] == '') {
            throw new Exception("Password can not be empty");

        if($_POST['RegisterPassword'] != $_POST['RetypePassword']) {
            throw new Exception("Passwords must match");

        $password = password_hash($_POST['RegisterPassword'], PASSWORD_DEFAULT);
        $token = time();

        $statement = $pdo->prepare("INSERT INTO users (name,email,password,token,status) VALUES (?,?,?,?,?)");

        $link = 'localhost/scouts/loginregistration-verify.php?email='.$_POST['RegisterEmail'].'&token='.$token;
        $email_message = 'Please click on this link to verify registration: <br>';
        $email_message .= '<a href="'.$link.'">';
        $email_message .= 'Click Here';
        $email_message .= '</a>';

        $mail = new PHPMailer();
        try {
            $mail->Host = '';
            $mail->SMTPAuth = true;
            $mail->Username = '';
            $mail->Password = '';
            $mail->SMTPSecure = 'tls';
            $mail->Port = 587;
            $mail->Subject = 'Registration Verification Email';
            $mail->Body = $email_message;

            $success_message = 'Registration is completed. An email is sent to your email address. Please check that and verify the registration.';

        } catch (Exception $e) {
            echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";

    } catch(Exception $e) {
        $error_message = $e->getMessage();


What are the 2 use statements at the top and what are the first 7 settings in the try block?

Here’s an explaination of the 7 in the try-catch block

    $mail->isSMTP();                                            //Send using SMTP
    $mail->Host = ' ';                     //Set the SMTP server to send through
    $mail->SMTPAuth = true;                                   //Enable SMTP authentication
    $mail->Username = '';                     //SMTP username
    $mail->Password = EMAIL_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`

I have gmail, can I use

$mail->Host = '';
$mail->Username = '';
$mail->Password = EMAIL_PASSWORD;

You will of course need to substitute your own host, username and password, but sending via gmail requires that you enable SSL, specify the port, and I believe it now requires that you use an application password, which you create in your gmail account. There used to be a flag to allow less secure applications (those that don’t handle 2-factor authentication) but that was removed.

1 problem, I dont see the “App Password” option like his google account has

I did see a notice that less secure apps is not going to be available in 2024. It looks like you’ll need a new approach for sending emails with Gmail once this is removed.

1 Like

I do have a bluehost account, I can use theres, right?

You should have no problem if you have a domain email account for sending emails, e.g.