phpmailerHow do I use PHPMailer to send an email to multiple addresses?
Using PHPMailer to send an email to multiple addresses is quite simple. The following example code shows how to do it:
<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
// Load Composer's autoloader
require 'vendor/autoload.php';
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Recipients
$mail->setFrom('[email protected]', 'Mailer');
$mail->addAddress('[email protected]', 'Recipient 1');
$mail->addAddress('[email protected]', 'Recipient 2');
$mail->addAddress('[email protected]', 'Recipient 3');
$mail->addReplyTo('[email protected]', 'Reply To');
// Content
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
The output of this code will be Message has been sent
.
The code consists of the following parts:
- Importing the PHPMailer classes into the global namespace.
- Loading Composer's autoloader.
- Instantiating PHPMailer and passing
true
to enable exceptions. - Setting the sender of the email.
- Adding recipients to the email.
- Setting the content of the email.
- Sending the email.
- Outputting a message if the email was sent successfully.
Helpful links
More of Phpmailer
- How do I determine which version of PHPMailer I'm using?
- How can I configure PHPMailer to work with GoDaddy?
- How can I use PHPMailer without SMTP secure?
- How do I use PHPMailer to attach a ZIP file?
- How can I configure PHPMailer to support Polish characters?
- How do I use PHPMailer to encode emails in UTF-8?
- How can I set up PHPMailer to use Zimbra SMTP?
- How can I use PHPMailer in Yii 1?
- How can I use PHPMailer to send emails with a Yahoo account?
- How can I use PHPMailer to send emails through Yandex?
See more codes...