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?