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.

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

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么