【发布时间】:2019-09-13 18:33:26
【问题描述】:
对于我的网页,我有一个 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;
}
}
}
?>
【问题讨论】:
-
那么谁应该收到电子邮件,谁不应该收到呢?
-
表
admin_notifications由什么组成? -
您一定对如何调试有所了解?您可以从数据库返回到 $row 的内容开始?这是否将所有人列出了三遍?
-
你向我们展示了
EmailService类中的所有方法吗?