1

I am trying to send bulk mail. And It worked but the problem is when I am checking on Gmail this shows. I can use Bcc and solve this issue but I want to for learning purposes.

------------------------------------------
from:   example <example@gmail.com>
to: example1@gmail.com,
example2@gmail.com,
example2@gmail.com
date:   Dec 19, 2021, 1:18 AM
subject:    Testing Mail
mailed-by:  example
signed-by:  example
security:    Standard encryption (TLS) Learn more
---------------------------------------------

In the recipient why all subscriber's mail is showing in "to". Please help me to fix it. I want to show specific subscriber mail.

Here is my code

<?php 
        if(isset($_POST['sendmail'])) {
            $status=1;
        $sql = "SELECT * FROM subscribers  WHERE status=? ORDER BY id ";
        $stmt = $conn2->prepare($sql); 
        $stmt->bind_param("s", $status);
        $stmt->execute();
        $result15 = $stmt->get_result();

            $mail = new PHPMailer;
            //$mail->SMTPDebug = 3;                               // Enable verbose debug output
            $mail->isSMTP();                                      // Set mailer to use SMTP
            $mail->Host = 'smtp.example.com';  // Specify main and backup SMTP servers
            $mail->SMTPAuth = true;                               // Enable SMTP authentication
            $mail->Username = EMAIL;                 // SMTP username
            $mail->Password = PASS;                           // SMTP password
            $mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
            $mail->Port = 465;                                    // TCP port to connect to
            
            if (mysqli_num_rows($result15)>0) {
                while ($row = mysqli_fetch_array($result15)) {  
                    $mail->AddAddress($row['email']);  // Add a recipient 
                }
 }
                $mail->setFrom(EMAIL, 'example');
                //$mail->addReplyTo(NOREPLY);
                $mail->isHTML(true);                                  // Set email format to HTML

                $mail->Subject = $_POST['subject'];
                $mail->Body    = '<div style="border:2px solid red;">This is the HTML message body <b>in bold!</b></div>';
                $mail->AltBody = $_POST['message'];
                if (!$mail->send()) {
                    echo 'Message could not be sent.';
                    echo 'Mailer Error: ' . $mail->ErrorInfo;
                } else {
                    echo 'Message has been sent';
                }
                
}
     ?> 
  • You did use a while loop for that. – nice_dev Dec 18 '21 at 20:03
  • @nice_dev how to fix this, if I will not use while loop then who will I send mail in bulk? Please guide I am a beginner. – Rishabh Raj Dec 18 '21 at 20:08
  • 1
    Your while loop adds every recipient to the same email message. So you're sending one message with a lot of recipients in the "To" field. If you want each recipient to get a message with only their email address in the "To" field, you need to move your while loop so that you create a new `$mail` and send it for every recipient. Note that if you do this, it's possible that your SMTP server will think you're a spammer and might block you. That depends on which SMTP server you use. – rickdenhaan Dec 18 '21 at 20:23
  • 1
    It's because you're telling to add all the addresses to one message. Take a look at [the mailing list example provided with PHPMailer](https://github.com/PHPMailer/PHPMailer/blob/master/examples/mailing_list.phps). It shows how to re-use the SMTP connection without dropping it or having to create a new instance, and also demonstrates how to use `addAddress` in conjunction with `clearAddresses` inside your sending loop. – Synchro Dec 18 '21 at 20:59

0 Answers0