消息系统 - 如何发送组电子邮件,但只在sendbox中列出一封电子邮件

时间:2013-04-23 17:13:29

标签: php mysql

我使用PHP和MySQL创建了一个个人消息系统。系统的一部分允许向适合某些群体的人发送群组电子邮件。

发送电子邮件的代码是:

session_start();
include_once('../../dbconnect.php');

$email=$_SESSION['email'];
$to = $_POST ['touser'];
$toemail = $_POST['touseremail'];
$from = $_SESSION['name'];
$message = $_POST['message'];
$subject = $_POST['subject'];
$time = time();

if ($to == 'Level 4'){
 $usersquery = "SELECT email FROM users WHERE account = 'Level4'";
 $getusers = $conn -> query($usersquery);
 while ($row = $getusers->fetch_assoc()){

  foreach ($row as $value){

 $query ="INSERT INTO messages (to_user, to_email, subject, message, from_user, from_email, daterecord) VALUES ('" . $to . "', '" . $value . "', '" . mysqli_real_escape_string($conn, $subject) . "', '" . mysqli_real_escape_string($conn, $message) . "', '$from', '$email', '$time')";
   $send = $conn -> query($query);

  }

 }

}

这很好用,但问题是当我打开发送箱时,会显示每条消息 - 当一个大组被消息时,这会堵塞发送箱并且非常烦人! sendbox的代码是:

$emailquery = "SELECT * FROM messages WHERE from_email = '$email' AND sent_deleted = 'no' ORDER BY daterecord DESC";
$sentemails = $conn->query($emailquery);
$emailcount = $sentemails ->num_rows;

if ($emailcount == 0){
 echo '<div>No messages sent</div>';
}

else{
?>
<table class="udtable">
    <tr>
        <th class="reqhead">To</th>
        <th class="reqhead">Subject</th>
        <th class="reqhead"></th>
    </tr>
<?
$i=1;
while ($sent = $sentemails->fetch_assoc()){
if ($i%2 != 0){
        $rowclass = 'reqodd';
        }
        else {
        $rowclass = 'reqeven';
        }

 echo ' <tr class = "' . $rowclass . '">
         <td class="reqfrom">' . $sent['to_user'] . '</td>
         <td class="reqsubj">' . $sent['subject'] . '</td>
         <td class="req"><a id="link' . $sent['id'] . '" href="#" class="sentopen">Open</a></td>
         <td class="reqmessage"><pre class=sentboxmessage>' . $sent['message'] . '</pre></td>
         <td class="reqid">' . $sent['id'] . '</td>
        </tr>';
        $i++;
}

?>
</table>
<?
}

?>

发送方框如下所示:

enter image description here

我想将所有这些消息合并为一行。

有什么想法吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

您需要在sendbox代码段中的mysql select语句中使用分组

SELECT * FROM messages WHERE from_email = '$email' AND sent_deleted = 'no' GROUP BY subject, daterecord ORDER BY daterecord DESC

这将有效并将按主题和日期记录对您的邮件进行分组。因此,任何同时使用相同主题发送的所有消息都将组合在一起。如果多人可以同时发送具有相同主题的邮件,您可能需要添加更多分组选项以进行优化。