Php - imap_delete - 删除刚刚选择的电子邮件
Php - imap_delete - Delete just selected email
我有一个脚本可以打印出从 imap 服务器获取的所有收件箱电子邮件。
我添加了一个删除提交按钮,按下时只需要删除属于该按钮的电子邮件。但是,现在的脚本会在按下任何删除按钮时删除所有电子邮件。下面是一个简化的代码:
<?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 />\n";
imap_delete($imap, $email_number);
$check = imap_mailboxmsginfo($imap);
echo "Messages after delete: " . $check->Nmsgs . "<br />\n";
imap_expunge($imap);
$check = imap_mailboxmsginfo($imap);
echo "Messages after expunge: " . $check->Nmsgs . "<br />\n";
}
}?>
关于如何通过按删除按钮删除所选电子邮件的任何想法?另外为什么我必须刷新页面两次才能看到删除后的变化?
电子邮件的删除表单和电子邮件本身之间似乎没有关联。 PHP 无法知道您要删除的确切电子邮件。
如前所述,一个想法是将电子邮件的 i.d 放在每封电子邮件旁边的提交按钮的值内。
然后您可以从 delete_inbox 输入的 post 数据中获取它。
<?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 />\n";
// Delete selected email
imap_delete($imap, $email_to_delete);
$check = imap_mailboxmsginfo($imap);
echo "Messages after delete: " . $check->Nmsgs . "<br />\n";
imap_expunge($imap);
$check = imap_mailboxmsginfo($imap);
echo "Messages after expunge: " . $check->Nmsgs . "<br />\n";
}
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 }?>
注意:我还从 foreach 循环中删除了代码的 $_POST 部分。我看不出它存在的任何理由。
我有一个脚本可以打印出从 imap 服务器获取的所有收件箱电子邮件。 我添加了一个删除提交按钮,按下时只需要删除属于该按钮的电子邮件。但是,现在的脚本会在按下任何删除按钮时删除所有电子邮件。下面是一个简化的代码:
<?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 />\n";
imap_delete($imap, $email_number);
$check = imap_mailboxmsginfo($imap);
echo "Messages after delete: " . $check->Nmsgs . "<br />\n";
imap_expunge($imap);
$check = imap_mailboxmsginfo($imap);
echo "Messages after expunge: " . $check->Nmsgs . "<br />\n";
}
}?>
关于如何通过按删除按钮删除所选电子邮件的任何想法?另外为什么我必须刷新页面两次才能看到删除后的变化?
电子邮件的删除表单和电子邮件本身之间似乎没有关联。 PHP 无法知道您要删除的确切电子邮件。
如前所述,一个想法是将电子邮件的 i.d 放在每封电子邮件旁边的提交按钮的值内。
然后您可以从 delete_inbox 输入的 post 数据中获取它。
<?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 />\n";
// Delete selected email
imap_delete($imap, $email_to_delete);
$check = imap_mailboxmsginfo($imap);
echo "Messages after delete: " . $check->Nmsgs . "<br />\n";
imap_expunge($imap);
$check = imap_mailboxmsginfo($imap);
echo "Messages after expunge: " . $check->Nmsgs . "<br />\n";
}
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 }?>
注意:我还从 foreach 循环中删除了代码的 $_POST 部分。我看不出它存在的任何理由。