对于我的网页,我有一个php邮件服务,可让我通过以下方式发送邮件
EmailService::getService()->sendEmail($email, $first_name, $subject, $body);
除非我将此行放入循环,例如通知所有列出的管理员,否则此方法工作正常:
$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);
}
现在,每个管理员都收到每封邮件。例如,如果有3个管理员,则每个管理员都会收到3个不同的邮件。该服务似乎分别向每个接收者发送3封邮件。
由于我没有实现邮寄服务本身,并且因为我不完全了解它,所以我真的不知道从哪里开始寻找此错误。
也许有人在这里有建议吗?
这是邮件服务的代码:
<?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();
$configs = ConfigService::getService()->getConfigs();
$this->mail->SMTPDebug = 3; //Gibt starke Debugging Ausgaben aus - für Realease Deaktivieren (später auf 2)
$this->mail->setLanguage('de');
$this->mail->IsSendmail();
$this->mail->Host = $configs['email_host'];
$this->mail->Port = $configs['email_port'];
$this->mail->SMTPSecure = "ssl";
$this->mail->SMTPAuth = true;
$this->mail->Username = $configs['email_username'];
$this->mail->Password = $configs['email_password'];
$this->mail->From = $configs['email_username'];
$this->mail->FromName = "Studienführer - VWI-ESTIEM-Karlsruhe e.V.";
$this->mail->CharSet = 'UTF-8';
$this->mail->isHTML(true);
}
/**
* sendet Email
*
* Sendet eine Email an den Nutzer. Gibt ein gewisses Format vor
*/
public function sendEmail($toEmail, $userName, $subject, $body){
$this->mail->AddAddress($toEmail, $userName);
$this->mail->Subject = $subject;
$this->mail->AddEmbeddedImage(__DIR__ . '/../../pictures/logo_studi.png', 'studilogo.png', 'studilogo.png');
$this->mail->AddEmbeddedImage(__DIR__ . '/../../pictures/email/facebook.png', 'facebook.png', 'facebook.png');
$this->mail->AddEmbeddedImage(__DIR__ . '/../../pictures/email/instagram.png', 'instagram.png', 'instagram.png');
$htmlWithoutCSS = '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional //EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My Mail</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
</head>
'.'
<body>
<p>Hallo $userName,</p>
$body
</body>
</html>
';
$cssToInlineStyles = new CssToInlineStyles();
$this->mail->Body = $cssToInlineStyles->convert(
$htmlWithoutCSS,
file_get_contents(__DIR__ . '/../../res/css/emails.css')
);
if(!$this->mail->Send()){
return false;
}else{
return true;
}
}
}
?>
答案 0 :(得分:2)
您遇到问题的原因是此行
$this->mail->AddAddress($toEmail, $userName);
每次调用都会将新的电子邮件地址添加到地址列表中。这导致您向上一个管理员发送一封电子邮件,向倒数第二个管理员发送2封电子邮件,依此类推
public function sendEmail($toEmail, $userName, $subject, $body){
似乎没有被设计为可以在每个实例中多次调用,但是通过简单的更改就可以解决问题
将$mail->ClearAllRecipients();
添加到这样的方法中。
public function sendEmail($toEmail, $userName, $subject, $body){
// remove previous recipients if being called multiple times in a loop
$this->mail->ClearAllRecipients();
$this->mail->AddAddress($toEmail, $userName);
$this->mail->Subject = $subject;
. . .
答案 1 :(得分:1)
您每次在循环中都使用相同的实例,并且每次调用sendEmail()
都会向该实例添加地址。
您可以每次都创建一个新实例,而不用调用getService()
,它会重新启动。
while($row = mysqli_fetch_assoc($result)){
$mailer = new EmailService;
$mailer->sendEmail($row['email'], $row['first_name'], $subject, $body);
}