dousao1175 2015-05-31 23:20
浏览 266
已采纳

Php - imap_delete - 删除刚刚选择的电子邮件

I have a script that prints out all inbox emails fetched from an imap server. I have added a delete submit button that when pressed needs to delete just the email that belongs to the button. However, the script right now, deletes all the email when any delete button is pressed. A simplified code below:

<?php    
foreach($emails as $email_number) {
?>
 <form method="post">
        <th class="tg-031e"><input type="submit" name="delete_inbox" value="Delete"></th>
        </form>

<?php
if(isset($_POST['delete_inbox'])){
$check = imap_mailboxmsginfo($imap);
echo "Messages before delete: " . $email_number . "<br />
";
imap_delete($imap, $email_number);
$check = imap_mailboxmsginfo($imap);
echo "Messages after  delete: " . $check->Nmsgs . "<br />
";
imap_expunge($imap);
$check = imap_mailboxmsginfo($imap);
echo "Messages after expunge: " . $check->Nmsgs . "<br />
";
}
}?>

Any ideas in how I could just delete the email selected by pressing delete button? Also why do I have to refresh the page two times in order to see the changes after deletion?

  • 写回答

1条回答 默认 最新

  • drtkyykai004574380 2015-06-01 04:32
    关注

    It appears there is no association between the deletion form for an email and the email itself. PHP has no way of knowing what exact email you wish to delete.

    As suggested before, one idea is to put the email's i.d within the value of the submit button next to each email.

    You can then take that from the post data of the delete_inbox input.

     <?php
        if(isset($_POST['delete_inbox'])){
        // Retrieve email i.d value from delete_inbox button
        $email_to_delete = $_POST['delete_inbox'];
        $check = imap_mailboxmsginfo($imap);
    
    
    echo "Messages before delete: " . $email_number . "<br />
    ";
    // Delete selected email
    imap_delete($imap, $email_to_delete);
    $check = imap_mailboxmsginfo($imap);
    echo "Messages after  delete: " . $check->Nmsgs . "<br />
    ";
    imap_expunge($imap);
    $check = imap_mailboxmsginfo($imap);
    echo "Messages after expunge: " . $check->Nmsgs . "<br />
    ";
    }
    
    foreach($emails as $email_number) {
    ?>
     <form method="post">
            <th class="tg-031e"><button type="submit" name="delete_inbox" value="<?php echo $email_number; ?>">Delete</button></th>
            </form>
    
    <?php }?>
    

    Note: I have also removed the $_POST section of the code out of the foreach loop. I don't see any reason for it to be there.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题