How to not make PHPmailer send an email with multiple 'to' addresses?
Are you doing something like this?
$mailer = new PHPMailer();
while($row = fetch_from_db()) {
$mailer->AddAddress($row['email']);
$mailer->send();
}
If so, you need to do a
$mailer->ClearAllRecipients()
after you send each email, so you start out with a "fresh" To:
list.
Sending emails to multiple addresses using config PHP file and PHP Mailer
Based on the examples from PHPMailer courtesy of the link shared by @Chris Haas. I made these changes:
<?php
$config = [
"host" => "xxx",
"username" => "xxx",
"password" => "xxx",
"secure" => "ssl", // ssl or tls
"port" => 465,
"sendTo" => "abc@email.com",
"sendTo2" => "efg@email.com",
"sendTo3" => "hik@email.com",
"sendToBCC" => "xyz@email.com",
"from" => "no-reply@email.com",
"fromName" => "Contact Form"
];
And in the sending file I did this:
//Recipients
$mail->setFrom($config['from'], $config['fromName']);
$mail->addAddress($config['sendTo']);
$mail->addAddress($config['sendTo2']);
$mail->addAddress($config['sendTo3']);
$mail->addCC($config['sendToCC']);
$mail->addBCC($config['sendToBCC']);
$mail->addAddress($_POST['email']);
That resolved the issue.
PHP mailer multiple address
You need to call the AddAddress
method once for every recipient. Like so:
$mail->AddAddress('person1@domain.example', 'Person One');
$mail->AddAddress('person2@domain.example', 'Person Two');
// ..
Better yet, add them as Carbon Copy recipients.
$mail->AddCC('person1@domain.example', 'Person One');
$mail->AddCC('person2@domain.example', 'Person Two');
// ..
To make things easy, you should loop through an array to do this.
$recipients = array(
'person1@domain.example' => 'Person One',
'person2@domain.example' => 'Person Two',
// ..
);
foreach($recipients as $email => $name)
{
$mail->AddCC($email, $name);
}
PHPMailer - Sending multiple emails to addresses stored in the mySQL database
You can directly get address
$recipients = array();
while ($row = mysqli_fetch_array($result)){
$mail->addRecipent($row['email'], $row['name']);
}
p.s. better use for
PHPMailer: Send emails to multiple recipients error
@Natrium nearly had it. You're not accessing your array elements correctly. Do it like this:
foreach ($emailto as $contacts) {
$mail->addAddress($contacts['email']);
}
It's also a good idea to check return values, perhaps:
foreach ($emailto as $contacts) {
if (!$mail->addAddress($contacts['email'])) {
echo 'Address rejected: '.$contacts['email'];
}
}
PHP Mailer Multiple recipients
You're code looks like it should be doing the trick. Make sure that $address
doesn't contain any whitespace for the entries. For safe measure, add the trim()
function.
$mail->AddAddress(trim($address));
If that does not work, make sure that you're recipient addresses are real.
Additionally, in case the privacy of the recipients is of concern, I would recommend you use AddBCC()
instead of AddAddress()
so that their addresses are not revealed.
Related Topics
Reference - What Does This Symbol Mean in PHP
How to Flatten a Multidimensional Array
How to Access and Manipulate Multi-dimensional Array by Key Names/Path
How to Change the Background Color of Dropdown List in Select Tag
Laravel, Failed to Authenticate on Smtp Server With Username "" Using 3 Possible Authenticators
How to Count Columns With the Same Value in a Specific Row in MySQL
How to Add Auto Numbering With Each Fetching Row
Contact Form 7 Cause Http 500 Error
How to Compare Comma Separated Ids on Left Join
How to Get Part of Url Before Last Slash With PHP
Nginx Error Recv() Failed (104: Connection Reset by Peer)
How to Unlink (Delete) an Image in Codeigniter
Dynamically Display a CSV File as an HTML Table on a Web Page