8wDlpd.png
8wDFp9.png
8wDEOx.png
8wDMfH.png
8wDKte.png

PHPMailer 在发送电子邮件循环期间停止,没有异常或错误

Sheesh Abby 1月前

22 0

我有一个简单的代码,可以通过循环发送电子邮件。它在数组中发送一堆电子邮件,然后在没有异常或错误的情况下停止。这是我使用的代码。$array_emails = [ [ \'email...

我有一个简单的代码,可以通过循环发送电子邮件。它在数组中发送一堆电子邮件,然后在没有异常或错误的情况下停止。这是我使用的代码。

$array_emails = [
  [
    "email" => "[email protected]",
    "body" => "message"
  ],
  ...
];

$emailService = new EmailService();
$emailService->sender($senderEmail, $senderName);
$emailService->subject($subject);
foreach ($array_emails as $item){
  try{
    $userEmail = trim($item["email"]);
    $logger->log_debug("Sending email to '$userEmail'");
    $result = $emailService->send($userEmail, "", $item["body"], $logger);

    if ($result) {
      $logger->log_info(">>> Email is sent to '$userEmail'");
    } else {
      $logger->log_error("!!! The email is not sent to '$userEmail' due to: $emailError");
    }
  } catch (Exception $ex) {
    $logger->log_debug($ex->getMessage());
  }
}
$emailService->close();

EmailService

<?php
if (!defined('__ROOT__')) define('__ROOT__', dirname(dirname(__FILE__)));

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;

require __ROOT__ . '/php4/PHPMailer/src/Exception.php';
require __ROOT__ . '/php4/PHPMailer/src/PHPMailer.php';
require __ROOT__ . '/php4/PHPMailer/src/SMTP.php';

class EmailService
{
  private PHPMailer $mail;
  private string $error = '';

  public function __construct($useLocalEmailFunction = false)
  {
    global $email_config;

    $this->mail = new PHPMailer(true);
    if ($useLocalEmailFunction) {
      $this->mail->isMail();
    } else {
      $this->mail->isSMTP();                                          //Send using SMTP
    }
    // $mail->SMTPDebug = SMTP::DEBUG_SERVER;                         //Enable verbose debug output
    $this->mail->Host       = $email_config["smtp"];                  //Set the SMTP server to send through
    $this->mail->SMTPAuth   = true;                                   //Enable SMTP authentication
    $this->mail->Username   = $email_config["email"];                 //SMTP username
    $this->mail->Password   = $email_config["email_password"];        //SMTP password
    $this->mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         //Enable implicit TLS encryption
    $this->mail->Port       = $email_config["email_port"];            //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
    $this->mail->SMTPKeepAlive = true;                                //SMTP connection will not close after each email sent, reduces SMTP overhead

    $this->mail->DKIM_domain = 'domain.com';
    $this->mail->DKIM_private = "/key";
    $this->mail->DKIM_selector = 'phpmailer';
    $this->mail->DKIM_passphrase = 'passcode';
    $this->mail->DKIM_identity = $email_config["email"];
  }

  public function sender($email, $name)
  {
    $this->mail->Sender = $email;
    $this->mail->setFrom($email, $name);
  }

  public function subject($subject)
  {
    $this->mail->Subject = $subject;
  }

  public function send($to, $name, $body, DashboardLogger $logger)
  {
    try {
      $this->mail->clearAddresses();
      $this->mail->addAddress($to, $name);
      $this->mail->Body = $body;
      $this->mail->isHTML();
      $this->mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
      $logger->log_debug("new address $to added. associated body added. ready to send");
      try {
        if (!$this->mail->send()) {
          $this->error = $this->mail->ErrorInfo;
          return false;
        }
        return true;
      } catch (Exception   $th) {
        try {
          $logger->log_error($th->getMessage());
          $this->error = $th->getMessage();
          $this->mail->getSMTPInstance()->reset();
        } catch (\Exception $th) {
          $logger->log_error($th->getMessage());
          $this->error = $th->getMessage();
        }
        return false;
      }
    } catch (\Exception $th) {
      $logger->log_error($th->getMessage());
      $this->error = $th->getMessage();
      return false;
    }
  }

  public function error()
  {
    return $this->error;
  }

  public function close()
  {
    $this->mail->SMTPKeepAlive = false;
    $this->mail->smtpClose();
  }
}

有什么想法吗?我不是 PHP 专家,可能我错过了谜题的节奏。非常感谢您的帮助。

我每天使用此代码发送一次电子邮件。循环毫无规律地停止,有时它会完成其工作,有时只会发送几封电子邮件。

如果你看一下我发布的代码,会注意到我使用了 $logger try/catch 块。 $logger 配置好了并且工作正常我添加了这些项来捕获异常/错误。所以我发布了我使用的实际代码。问题是循环在函数之后停止在数组的中间 send

帖子版权声明 1、本帖标题:PHPMailer 在发送电子邮件循环期间停止,没有异常或错误
    本站网址:http://xjnalaquan.com/
2、本网站的资源部分来源于网络,如有侵权,请联系站长进行删除处理。
3、会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。
4、本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
5、站长邮箱:yeweds@126.com 除非注明,本帖由Sheesh Abby在本站《php》版块原创发布, 转载请注明出处!
最新回复 (0)
  • 您如何知道循环本身实际上“停止”了?您能从您在那里写的日志条目中看出这一点吗?或者您可能只是从并非所有这些邮件都成功发送的事实中得出了错误的结论

  • 说实话,这感觉像是一项不错的任务,可以转移到 CLI 并通过 cron 调用。无论如何,你研究过超时吗?这些不会被捕获

  • 是的,对我来说这听起来像是超时问题。发送的电子邮件数量取决于当时邮件服务器处理电子邮件的速度。

返回
作者最近主题: