How can PHPMailer be utilized to securely send mass emails to members while protecting against misuse?

To securely send mass emails to members while protecting against misuse, PHPMailer can be utilized with SMTP authentication and encryption. This ensures that the emails are sent securely and only authorized users can send emails through the script.

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

// Include PHPMailer autoload file
require 'vendor/autoload.php';

// Instantiate PHPMailer
$mail = new PHPMailer();

// Set mailer to use SMTP
$mail->isSMTP();

// Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = SMTP::DEBUG_OFF;

// Set SMTP host
$mail->Host = 'smtp.example.com';

// Set SMTP port
$mail->Port = 587;

// Enable SMTP authentication
$mail->SMTPAuth = true;

// SMTP username
$mail->Username = 'your_smtp_username';

// SMTP password
$mail->Password = 'your_smtp_password';

// Enable TLS encryption
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;

// Set sender email address
$mail->setFrom('from@example.com', 'Sender Name');

// Add a recipient
$mail->addAddress('recipient@example.com', 'Recipient Name');

// Set email subject
$mail->Subject = 'Test Email';

// Set email body
$mail->Body = 'This is a test email sent using PHPMailer.';

// Send the email
if ($mail->send()) {
    echo 'Email has been sent';
} else {
    echo 'Email could not be sent';
}