all the emails are sending together to all the recipients showing their emails in "TO", which i don't want.
how to send mails individually instead of sending to all recipients together?
my code is:

function SendMail($msg,$subject,$addresses)
    {
    require "class.phpmailer.php";
    //Fetch from email here
    foreach($addresses as $key=>$val){
             $email = test_input($val);
            if((trim($email)=='') || (!filter_var($email, FILTER_VALIDATE_EMAIL))){
                unset($addresses[$key]);
            }
        }
        $addresses=array_unique($addresses);
    $row = FetchAllsettingsCustomMailchmp();
    //end fetch
    $mail = new PHPMailer();
    $mail->PluginDir = "";
    $mail->Host = "localhost";
    $mail->From = $row['email_from'];
    $mail->FromName = $row['sitename'];
    $mail->Subject =  $subject;
    foreach ($addresses as $address)
        $mail->AddAddress($address);
    $mail->MsgHTML($msg);
    $mail->IsHTML(true);
    $mail->AltBody ="Order";
    $mail->CharSet = 'UTF-8';
    $success = $mail->Send();
    $try = 1;

    while((!$success)&&($try<1)&&($mail->ErrorInfo!="SMTP Error: Data not accepted"))
        {
        sleep(5);
        $success = $mail->Send();
        $try++;
        }

    $mail->ClearAddresses();
    if(!$success)
        return false;
        else
        return true;
    }

function test_input($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}

Hi,
You just need to loop through every email and send it.
Try executing below code and confirm.

function SendMail($msg,$subject,$addresses)
    {
    require "class.phpmailer.php";
    //Fetch from email here
    foreach($addresses as $key=>$val){
             $email = test_input($val);
            if((trim($email)=='') || (!filter_var($email, FILTER_VALIDATE_EMAIL))){
                unset($addresses[$key]);
            }
        }
        $addresses=array_unique($addresses);
    $row = FetchAllsettingsCustomMailchmp();
    //end fetch
    $mail = new PHPMailer();
    $mail->PluginDir = "";
    $mail->Host = "localhost";
    $mail->From = $row['email_from'];
    $mail->FromName = $row['sitename'];
    $mail->Subject =  $subject;
    foreach ($addresses as $address) {
        $mail->AddAddress($address);
    $mail->MsgHTML($msg);
    $mail->IsHTML(true);
    $mail->AltBody ="Order";
    $mail->CharSet = 'UTF-8';
    $success = $mail->Send();
    $try = 1;
    while((!$success)&&($try<1)&&($mail->ErrorInfo!="SMTP Error: Data not accepted"))
        {
        sleep(5);
        $success = $mail->Send();
        $try++;
        }
    $mail->ClearAddresses();
    }
    if(!$success)
        return false;
        else
        return true;
    }
function test_input($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}
commented: Thanks, it worked!!! +0

The above works, but it may get you blocked as a spam sender.

Why can't you just add all your recipients as BCC, and send it to yourself?

I see MailChimp mentioned in your of your functions. Why not use that to send your e-mails instead?

pritaeas, i don't want to use BCC.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.