0

I'm trying to send multiple separate emails to multiple addresses. The below code sends it in 1 email with multiple TO addresses. This becomes an issue because everyone in the email can see each other's email addresses.

Is there a way to send separate emails?

<?php

$smtp       = 'xxx.com';
$port       = 25;
$secure     = 'tls';

$username   = 'test@xxx.com';
$pass       = '';
$from       = 'test@xxx.com';
$to         = 'info@xxx.com';
$to1        = '';
$subject    = 'Test Email';
$content    = $mail_content;

require_once("include/class.phpmailer.php");
$mail=new PHPMailer(true);
$mail->IsSMTP();
try{
    $mail->Host         = $smtp;
    $mail->SMTPAuth     = true;
    $mail->Port         = $port;
    $mail->SMTPSecure   = $secure;
    $mail->Username     = $username;
    $mail->Password     = $pass;
    $mail->SetFrom($from);

    if (isset($email) && $email) {
        $mail->AddAddress($email);
    }
    else {
        while($row = mysqli_fetch_object($result)) {
            $mail->AddAddress($row->email);
            echo $row->email."<br>";
        }
    }

    $mail->Subject      = $subject;
    $mail->MsgHTML($content);
    $mail->Send();

    if (isset($email) && $email) {
?>
        <script>location.href="<?php echo '../index.php' . $_REQUEST['redirect']; ?>";</script>

<?php
    }
}
catch (phpmailerException $e){
    echo $e->errorMessage();
}
catch (Exception $e){
    echo $e->getMessage();
}

?>
AnFi
  • 10,493
  • 3
  • 23
  • 47

2 Answers2

0

Send the email in the BCC.

Look here for an example :

PHP Email sending BCC

Ragnar
  • 2,550
  • 6
  • 36
  • 70
0

You have a loop that checks for all of the email addresses and then assigns the email address to $mail->AddAddress().

All you need to do in this same loop is send the email with $mail->Send(); with each iteration of the loop as well

//PHPmailer object
$mail=new PHPMailer(true);

//set up
$mail->IsSMTP();
$mail->Host         = $smtp;
$mail->SMTPAuth     = true;
$mail->Port         = $port;
$mail->SMTPSecure   = $secure;
$mail->Username     = $username;
$mail->Password     = $pass;
$mail->SetFrom($from);
$mail->Subject      = $subject;
$mail->MsgHTML($content);

//add address
if (isset($email) && $email) {
    $mail->AddAddress($email);
}
else {
    //loop through the DB results 
    while($row = mysqli_fetch_object($result)) {

        //you already have this
        $mail->AddAddress($row->email);
        echo $row->email."<br>";

        //now send it here as well
        $mail->Send();

        //Do more stuff here
    }
}

I have not tested this on my local system but this is one way to send the mail individually for each address as an alternative to sending one mail and using bcc.

Daniel_ZA
  • 574
  • 11
  • 26