我有下面的代码来选择事件,用户和子用户,并发送电子邮件给用户,如果有新的事件,子用户创建。
$result_m = mysqli_query($db, $query_mail) or die(mysqli_error());
while ($re4 = mysqli_fetch_array($result_m, MYSQLI_ASSOC))
{
$ajdi = $re4["pid"];
$name1 = $re4["name"];
$surname1 = $re4["surname"];
$email1 = $re4["email"];
$mob = $re4["mob"];
$titl1 = $re4["title"];
$zip1 = $re4["zip"];
$city1 = $re4["city"];
$address1 = $re4["address"];
$insurance1 = $re4["insurance"];
$born1 = $re4["born"];
$receiver = $re4["demail"];
$content= "<p><img src='$logo'></p>";
$content.= "<p><br/>Name: $name1 $surname1, Born: $born1, Insurance: $insurance1<br/>$address1, $city1 $zip1, Tel: $mob, Email: $email1</p>";
$title= "Mail title";
$content.= "<p>Footer message</p>";
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/html; charset=utf-8";
$headers[] = "From: no-reply@test.com";
if ($receiver!= "")
{
mail($receiver, $title, $content, implode("rn", $headers));
}
}
这也被封装在另一个获取所有事件的while循环中。在这种情况下,用户在创建事件时收到电子邮件,但如果有更多的事件,他会收到每个事件的电子邮件。如何发送只是一个电子邮件用户与所有创建的事件在它?
例如,当我执行echo "$receiver<br>$content<br>";
时,我得到:
email@email.com
Some content
email@email.com
Some content2
email2@email.com
Some content
email2@email.com
Some content2
但我需要这样:
email@email.com
Some content
Some content2
email2@email.com
Some content
Some content2
如何实现这一点?谢谢你
Array
(
[mail1@email.com] =>
22.07.2015., 08:30 Uhr
)
Array
(
[mail1@email.com] =>
23.07.2015., 08:15 Uhr
)
Array
(
[mail1@email.com] =>
09.09.2015., 14:45 Uhr
)
Array
(
[mail1@email.com] =>
03.08.2015., 14:40 Uhr
)
Array
(
[mail1@email.com] =>
03.08.2015., 11:40 Uhr
)
Array
(
[mail1@email.com] =>
03.08.2015., 14:20 Uhr
)
Array
(
[mail2@email.com] =>
30.07.2015., 15:40 Uhr
)
Array
(
[mail2@email.com] =>
24.07.2015., 14:00 Uhr
)
Array
(
[mail3@email.com] =>
24.07.2015., 14:00 Uhr
)
Array
(
[mail3@email.com] =>
30.07.2015., 15:40 Uhr
)
您可以通过几种方式实现这一点。
一个简单的方法是:不发送电子邮件,但首先将它们全部存储在一个数组中,使用emailaddress作为键。
像这样:
... db stuff
$allRecipients = array();
while ($re4 = mysqli_fetch_array($result_m, MYSQLI_ASSOC)) {
$ajdi = $re4["pid"];
$name1 = $re4["name"];
$surname1 = $re4["surname"];
$email1 = $re4["email"];
$mob = $re4["mob"];
$titl1 = $re4["title"];
$zip1 = $re4["zip"];
$city1 = $re4["city"];
$address1 = $re4["address"];
$insurance1 = $re4["insurance"];
$born1 = $re4["born"];
$receiver = $re4["demail"];
$content= "<p><img src='$logo'></p>";
$content.= "<p><br/>Name: $name1 $surname1, Born: $born1, Insurance: $insurance1<br/>$address1, $city1 $zip1, Tel: $mob, Email: $email1</p>";
if ($receiver!= ""){
if (isset($allRecipients[$receiver]){
// add the content.
$allRecipients[$receiver] .= "<hr>".$content;
} else {
$allRecipients[$receiver] = $content;
}
}
}
// now mail
$title= "Mail title";
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/html; charset=utf-8";
$headers[] = "From: no-reply@test.com";
// If you are curious how that array looks, add this:
// echo "<pre>";
// print_r($allRecipients);
// echo "</pre>";
foreach ($allRecipients as $receiver => $content){
$content.= "<p>Footer message</p>";
mail($receiver, $title, $content, implode("rn", $headers));
}
我认为你的问题可以通过以下方法解决。
在select查询中添加GROUP BY
并合并所有内容
即SELECT email,"Merged Content" FROM your_table_name GROUP BY email
注意:-此合并内容您必须根据您的要求格式化
然后迭代查询结果并发送电子邮件:)