1 | initial version |
To send an email to several recipients at once using PHP mailer, you can use the following steps:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php'; // This will load the PHPMailer library
$mail = new PHPMailer(true);
$mail->SMTPDebug = 0;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com'; // This can be changed to any SMTP host
$mail->SMTPAuth = true;
$mail->Username = 'youremail@gmail.com'; // Your email address
$mail->Password = 'yourpassword'; // Your email password
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('youremail@gmail.com', 'Your Name');
$mail->addAddress('recipient1@example.com', 'Recipient 1');
$mail->addAddress('recipient2@example.com', 'Recipient 2');
$mail->Subject = 'Email Subject';
$mail->Body = 'Email content goes here.';
if ($mail->send()) {
echo 'Email sent successfully.';
} else {
echo 'Unable to send email. Error: ' . $mail->ErrorInfo;
}
By adding multiple recipients with the addAddress()
method, you can send the same email to several recipients at once.