Using multiple email address for posting mails from PHPMailer class
We may have a list of email address to which the common message should go. For this we will store all email address in an array and then we will loop through the array to add all emails to our outgoing address.
Above code is a sample to display all email address but our requirement is to add them to our mail outgoing list. You can see our mail posting script using PHPMailer class here. To this script we will add the array looping like this.
<?php
$bodytext = 'This is the body of the test mail';
$subject = 'plus2 Message Subject' . date(" H:i:s", time());
$address = array(
'user1@example.com',
'user2@example.com',
'user3@example.com',
'user4@example.com'
);
require_once('my_phpmailer/class.phpmailer.php');
$email = new PHPMailer();
$email->From = 'userid@example.com';
$email->FromName = 'Your Name';
$email->Subject = $subject;
$email->Body = $bodytext;
while (list ($key, $val) = each ($address)) {
$email->AddAddress($val);
}
if(!$email->send()) {
echo"Mailer Error: " . $email->ErrorInfo;
} else {
echo"Message has been sent successfully";
}
?>
Collecting email address from a table
Like above code we can collect all email address from a table and use them to send emails to all. You can read how to display data from table here. Same code we will use to add address to our phpmailer class.
You may not be interested to show all address to each member of your list, in such a case you can add BCC to each address.
You can only change above line in respective AddAddress line.
$email->AddBCC($row['email']); // Data taken from table$email->AddBCC($val); // Data taken from array
Example: Sending Emails with Multiple Attachments
require_once('my_phpmailer/class.phpmailer.php');
$mail = new PHPMailer();
$mail->addAddress('recipient@example.com');
$mail->Subject = 'Invoice';
$mail->Body = 'Please find the attached invoices.';
$mail->addAttachment('/path/invoice1.pdf');
$mail->addAttachment('/path/invoice2.pdf');
$mail->send();
Example: Error Handling When Sending to Multiple Recipients
require_once('my_phpmailer/class.phpmailer.php');
$mail = new PHPMailer;
$recipients = array(
'recipient1@example.com',
'recipient2@example.com',
'recipient3@example.com'
);
foreach ($recipientsas$email) {
$mail->addAddress($email);
if(!$mail->send()) {
echo"Failed to send email to " . $email . ". Error: " . $mail->ErrorInfo;
} else {
echo"Email sent to " . $email . ".";
}
$mail->clearAddresses(); // Reset addresses after each send
}
🛠Explanation:
Includes PHPMailer using require_once().
Creates a new PHPMailer instance using $mail = new PHPMailer();.
Defines an array of recipients ($recipients).
Loops through each email in the array:
Adds the email to the recipient list using addAddress().
Attempts to send the email using send().
Handles errors:
If send() fails, it displays the error message.
If successful, it confirms email delivery.
Clears the recipient list after each email using clearAddresses().
📌 Example Output:
Email sent to recipient1@example.com.
Failed to send email to recipient2@example.com. Error: SMTP connection failed.
Email sent to recipient3@example.com.