第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

PHP Mail Service循環(huán)將郵件發(fā)送給每個人。如何避免呢?

PHP Mail Service循環(huán)將郵件發(fā)送給每個人。如何避免呢?

PHP
梵蒂岡之花 2021-04-29 14:12:52
對于我的網(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);

}


查看完整回答
反對 回復(fù) 2021-05-14
  • 2 回答
  • 0 關(guān)注
  • 163 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號