Sending Multiple Mails In PHP Mailer

前端 未结 3 1668
走了就别回头了
走了就别回头了 2021-02-11 04:30

I have been writing a trigger that mails an email using PHP mailer. The problem with the code is that it is sending multiple mails to a single recipient. I even tried using the

3条回答
  •  故里飘歌
    2021-02-11 04:59

    AddAddress() simply adds a new address to the end of an array of addresses. If you want to send individual emails, per-address, you'll have to clear that list on each iteration, e.g.

    for(...) {
       $mail->AddAddress(...);
       $mail->send();
       $mail->ClearAddresses();  // <---you need this line.
    }
    

    If a single user is receiving multiple emails, then you'd need some code to detect when you've already sent one, e.g.:

    $sent = array();
    for(...) {
       if (isset($sent[$email[$i]])) {
          continue; // check if sent already, skip if so
       }
       ...
       if ($mail->send()) {
         $sent[$email[$i]] = true; // flag address as already sent
       }
    }
    

提交回复
热议问题