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

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

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

?>

<?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=?");
        $statement->execute([$_POST['RegisterEmail']]);
        $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 (?,?,?,?,?)");
        $statement->execute([$_POST['RegisterName'],$_POST['RegisterEmail'],$password,$token,0]);

        
        $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->isSMTP();
            $mail->Host = '';
            $mail->SMTPAuth = true;
            $mail->Username = '';
            $mail->Password = '';
            $mail->SMTPSecure = 'tls';
            $mail->Port = 587;
        
            $mail->setFrom('contact@example.com','Administrator');
            $mail->addAddress($_POST['RegisterEmail'],$_POST['RegisterName']);
            $mail->addReplyTo('contact@example.com');
            $mail->isHTML(true);
            $mail->Subject = 'Registration Verification Email';
            $mail->Body = $email_message;
            $mail->send();

            $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 = 'smtp.ionos.com ';                     //Set the SMTP server to send through
    $mail->SMTPAuth = true;                                   //Enable SMTP authentication
    $mail->Username = 'pepster@pepster.com';                     //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 = 'smtp.gmail.com';
$mail->Username = 'lurtnowski@gmail.com';
$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. info@mydomain.com

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.