對于我的網(wǎng)頁,我有一個php郵件服務(wù),可讓我通過發(fā)送郵件EmailService::getService()->sendEmail($email, $first_name, $subject, $body);除非我將此行放入循環(huán)中,例如,通知所有列出的管理員,否則此方法工作正常:$sql = "SELECT * FROM admin_notifications";$result = mysqli_query($con, $sql);while($row = mysqli_fetch_assoc($result)){ EmailService::getService()->sendEmail($row['email'], $row['first_name'], $subject, $body);}現(xiàn)在,每個管理員都會收到每封郵件。例如,如果有3個管理員,則每個管理員都會收到3個不同的郵件。該服務(wù)似乎分別向每個接收者發(fā)送3封郵件。由于我沒有實現(xiàn)郵寄服務(wù)本身,并且因為我不完全了解它,所以我真的不知道從哪里開始尋找此錯誤。也許有人在這里有建議嗎?這是郵件服務(wù)的代碼:<?php /*Verschickt Emails*/ use TijsVerkoyen\CssToInlineStyles\CssToInlineStyles; use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; class EmailService{ /** * instance * * Statische Variable, um die aktuelle (einzige!) Instanz dieser Klasse zu halten * * @var Singleton */ protected static $_instance = null; protected $mail; /** * get service * * Falls die einzige Service-Instanz noch nicht existiert, erstelle sie * Gebe die einzige Service-Instanz dann zurück * * @return Singleton */ public static function getService() { if (null === self::$_instance) { self::$_instance = new self; } return self::$_instance; } /** * clone * * Kopieren der Service-Instanz von aussen ebenfalls verbieten */ protected function __clone() {} /** * constructor * * externe Instanzierung verbieten */ protected function __construct() { //new PHPMailerAutoload(); $this->mail = new PHPMailer();
2 回答

慕萊塢森
TA貢獻1810條經(jīng)驗 獲得超4個贊
您每次在循環(huán)中都使用相同的實例,每次調(diào)用sendEmail()它都會為實例添加地址。
您可以每次創(chuàng)建一個新實例,而不是調(diào)用getService(),它將重新啟動。
while($row = mysqli_fetch_assoc($result)){
$mailer = new EmailService;
$mailer->sendEmail($row['email'], $row['first_name'], $subject, $body);
}
- 2 回答
- 0 關(guān)注
- 163 瀏覽
添加回答
舉報
0/150
提交
取消