如何使用PHPmailer发送多封电子邮件

时间:2018-08-09 12:58:05

标签: php email phpmailer

我有成千上万的电子邮件,我想向所有人发送电子邮件,但我无法解决。任何人都可以通过解决方法的实例来帮助和阐明

如下面的代码所示,什么是问题,什么是解决方案

$mail->isSMTP();                              // Set mailer to use SMTP
    $mail->Host = 'gator4164.hostgator.com '; // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                   // Enable SMTP authentication
    $mail->Username   = "...";                // SMTP username
    $mail->Password = '...';                  // SMTP password
    $mail->SMTPSecure = 'ssl';                // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 465;                        // TCP port to connect to
    $mail->CharSet = 'UTF-8';
    $get = new get();
    $getEmails = $get->getEmails();
    $CountEmails = $get->CountEmails();

    $mail->setFrom('email@gmail.com', 'Email Name');

    foreach($getEmails as $getEmails){

        for ($x = $emails['id']; $x <= $CountEmails; $x++) {
            $mail->addAddress($emails['email'],$emails['name']);
            $mail->isHTML(true);              // Set email format to HTML
                $mail->Subject = $title;

                $mail->Body    = nl2br($content);

                $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

      }
      if(!$mail->send()) {
          echo 'Message could not be sent.';
          echo 'Mailer Error: ' . $mail->ErrorInfo;
      } else {
          echo'<div class="done">Done</div>';

      }
    }

2 个答案:

答案 0 :(得分:1)

在这种情况下,您必须创建一个Process Queue,因为每次发送电子邮件都需要一些时间来处理,如果电子邮件数为千,那么在这种情况下,系统将因为处理时间而崩溃限制。为了创建流程队列,您必须维护一个数据库表,在其中保存所有记录并维护具有不同值的状态,例如:

0: Initial, 1:Processing, 2: Email Sent

并创建一个单独的功能,该功能可以定期提取记录并发送电子邮件并更改状态。将此功能放在CRON上。

答案 1 :(得分:0)

不要在一个循环中添加所有带有“添加”的收件人,那么您只有一封电子邮件包含所有收件人。每次发送电子邮件。

foreach($getEmails as $getEmails){
    $emails['email'] = $getEmails['email'];
    $emails['name']  = $getEmails['name'];

    $mail->addAddress($emails['email'],$emails['name']);
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = $title;
    $mail->Body    = nl2br($content);
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    if(!$mail->send()) {
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    } else {
        echo'Email send to ' + $emails['email'];
    }
    $mail->clearAddresses();
}

类似的东西。因此,在每个持续时间发送电子邮件。为防止向他人发送重复的电子邮件,如果发送了电子邮件,则应在数据库中设置状态,否则,如果脚本中有错误并且必须重新启动脚本,请再次发送。

相关问题