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);
}
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.
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.
PHPMailer sending to multiple address separated by a ,
You should explode it and then add the emails.
$addresses = explode(',', $result["emailto"]);
foreach ($addresses as $address) {
$email->AddAddress($address);
}
how to send emails to multiple addresses using phpmailer
Assume you have good data in $email_addresses variable.
You need to change line:
$email->AddAddress( $emailto );
into:
$addr = explode(',',$email_addresses);
foreach ($addr as $ad) {
$email->AddAddress( trim($ad) );
}
PHP mailer multiple addresses
Try using array concepts, get all the form value i mean the to address in an array iterate and send it. This is like sending separate mail to recipients automated by a for loop .
$Address = array();
$Address[] = $_POST['txtTO'];
foreach($Address AS $Add=>$ToEmail){
mail($ToEmail, $Subject, $Message, $mailheaders);
phpMailer not adding multiple addresses
There is not enought code shown to exactly and positively nail down your problem.
But your $row
strongly suggests that you're in the middle of a while($row=$stmt->fetch())
, right?
We don't see anything like it in the code you show.
I think I have 99% chances to be right on this.
Your fix is to "extract" your database data BEFORE the PHPMailer part.
Get all your vars (Sender, From, FromName and an array of adresses... An array of BCC too? ).
Finish your while fetch loop BEFORE getting in the PHPMailer part, which shall not be included in a loop.
THEN, you can go on with your email construction.
Set your Body, AltBody and Subject.
Now is the time to loop into an address array.
(And don't call it $row!!).
By the way, your for loop declaration isn't as correct as you think:
( $i=0;$i<$rows;$i++ )
should be ( $i=0;$i<sizeOf($rows);$i++ )
.
Notice the sizeOf()?
This code that worked perfect for me:
I suggest you try it in order to get a confimation that PHPMailer isn't the problem.
// This array would be defined by you fetch loop.
$moreToAdressArray=["test1@example.com","test2@example.com","test3@example.com","test4@example.com"];
$endpointMsg->AddAddress( $clientLine['email'] );
for($i=0;$i<sizeOf($moreToAdressArray);$i++){
$endpointMsg->AddAddress( $row[$i] );
}
$endpointMsg->AddBCC ($SENDER);
I've had 5 times the same email with this (adresses were different than ..@example.com - Doh!).
PHPMailer works like a charm.
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
How to add multiple cc email address using phpmailer
Create an array to store multiple cc emails.
while ($row = $querycc->fetch_assoc()) {
$ccemail[]= $row['email'];
$ccname[]= $row['employee'];
}
And these arrays in phpmailer code.
Or you can use the below code.
if($num_rowscc>0){
while ($row = $querycc->fetch_assoc()) {
// create an array to have multiple records
$recipients[]= array('email'=>$row['email'],'name'=>$row['employee']);
}
} else {
$recipients[]= array('email'=>'akash1sethi@gmail.com','name'=>'Akash Sethi');
}
In phpmailer
// loop the array and add to cc
foreach($recipients as $recipient){
$mail->AddCC($recipient['email'],$recipient['name']);
}
Related Topics
How to Prevent from Printscreen of My Webpage
How to Paginate Lines in a Foreach Loop with PHP
Jquery Mobile: Sending Data from One Page to the Another
Differencebetween PHP Require and Include
PHP Datetime Round Up to Nearest 10 Minutes
How to Execute a Stored Procedure in PHP Using SQLsrv and "" Style Parameters
Why Is Calling a Function (Such as Strlen, Count etc) on a Referenced Value So Slow
How to Run a .Sh File from PHP
Bubble Sort Implementation in PHP
Interface Implementation: Declaration Must Be Compatible
$_Post Is Empty After Form Submit
Remove Double Quote in JSON_Encode()
Get Last Executed Query in PHP Pdo