我有一个简单的代码,可以通过循环发送电子邮件。它在数组中发送一堆电子邮件,然后在没有异常或错误的情况下停止。这是我使用的代码。$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
。
PHPMailer 在发送电子邮件循环期间停止,没有异常或错误
下载声明:
本站所有软件和资料均为软件作者提供或网友推荐发布而来,仅供学习和研究使用,不得用于任何商业用途。如本站不慎侵犯你的版权请联系我,我将及时处理,并撤下相关内容!