我遇到的问题是我编写的脚本只是从数据库中选择条目并向用户发送电子邮件,以便在与其相关的条目上更新它们。 电子邮件程序功能正在运行,过滤到各个用户的条目也是如此。但是,我遇到的问题是,电子邮件的发送次数与数据库中的条目相同。该电子邮件包含相同的内容(所有4个条目都出现在同一封电子邮件中,但作为示例发送了4次)。
这是邮件脚本:
$data = getData();
$uniqueUsers = array();
foreach($data as $item){
$user = $item[1];
$name = $item[3];
$array = array($user, $name);
if(!in_array($array, $uniqueUsers)){
array_push($uniqueUsers, $array);
};
};
echo print_r($uniqueUsers);
echo "<br>";
echo "<hr>";
foreach($uniqueUsers as $user){
$email = $user[0];
$name = $user[1];
$fullName = explode(" ", $name);
$firstName = $fullName[0];
$updates = "";
echo $email;
echo "<br>";
echo $name;
echo "<br>";
foreach($data as $item){
if($user[0] == $item[1]){
$equipmentName = $item[0];
$approved = $item[2];
$start = $item[5];
$end = $item[6];
$updates .= $approved . " - " . $equipmentName . " (Booked from " . date('l, jS F', strtotime($start)) . " to " . date('l, jS F', strtotime($end)) . ")";
$updates .= "<br>";
};
};
echo $updates;
echo "<hr>";
mailer($email, $firstName, $updates);
};
这是getdata()脚本:
mysql_select_db("WBLResources", $con) or die ("Error selecting specified database on mysql server: ".mysql_error());
$query = "SELECT * FROM `Bookings` WHERE `updated` > '" . $date . "'";
$result = mysql_query($query);
$numOfRows = mysql_num_rows($result);
if($numOfRows < 1){
exit();
};
for ($i = 0; $i < $numOfRows; $i++) {
$row = mysql_fetch_array($result);
$result_array[$i] = array($row["equipment_name"],
$row["user"],
$row["approved"],
$row["name"],
$row["updated"],
$row["start"],
$row["end"]);
};
return $result_array;
};
根据我的分析,mailer()函数应该在uniqueUsers数组上运行。这是我在10分钟前提到的那个片段:
Array (
[0] => Array (
[0] =>
[1] => )
[1] => Array (
[0] => ####@###.co.uk
[1] => Username )
[2] => Array (
[0] => ####@###.co.uk
[1] => Username )
[3] => Array (
[0] => #####@###.co.uk
[1] => Username )
[4] => Array (
[0] => #####@###.co.uk
[1] => Username )
[5] => Array (
[0] => ######@###.co.uk
[1] => Username )
[6] => Array (
[0] => #####@###.co.uk
[1] => Username ) )
邮件程序功能:`函数邮件程序($ email,$ firstName,$ updates){
$mail = new PHPMailer;
$subject = "Resource Bookings";
$htmlMessageBody = "<div style=font-size:10.0pt;font-family:'Arial',sans-serif;color:black;mso-fareast-language:EN-GB'>Dear " . $firstName . ",<br><br>The following resource bookings have been updated: <br><br>" . $updates . "<br><br>Many thanks,<br>TSW Support</div>";
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = '#######'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '###@####.co.uk'; // SMTP username
$mail->Password = '######';
$mail->SMTPSecure = 'tls';
$mail->Port = 25; // TCP port to connect to
//$mail->SMTPDebug = 3;
$mail->setFrom('####@#####.co.uk', 'TSW Training Group');
$mail->addAddress($email, $firstName); // Add a recipient
$mail->addReplyTo('#####@######.co.uk', 'TSW Training Group');
$mail->addCC("#####@#####.co.uk", "Support");
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $htmlMessageBody;
//$mail->AltBody = $plainMessageBody;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
};
};`
任何帮助都会受到赞赏吗?
答案 0 :(得分:0)
这个逻辑应该可以解决你的问题。但是Haven没有测试过,所以如果你遇到任何错误,请告诉我。
$user_lists = array();
foreach($data as $item){
$user = $item[1];
$name = $item[3];
$array = array($user, $name);
if(!in_array($user, $user_lists)){
array_push($uniqueUsers, $array);
$user_lists[] = $user;
};
};
希望这有帮助!