I'm working on a script for a custom inbox to read e-mails. (We have several inboxes available to us on our hosting account but we need a custom one for a few reasons.) Today, e-mails have begun vanishing from the account -- they're not just gone from this custom inbox but also from the inboxes already set up on our hosting account, and from those trash folders. As yet, there is no trash folder for the custom script. Here's the script.

<?php

  $host = "webmail.ourdomain.com"; // We're using a cPanel proxy to avoid HTTP authentication popups.
  $connection = imap_open("{" . $host . "/imap/notls}", 'address@ourdomain.com', 'emailpassword') or die("Can't connect: " . imap_last_error());
  $newemails = imap_mailboxmsginfo($connection);

?>

<html>
<head>
<meta name="robots" content="noindex">
<title>E-mail (<?php echo $newemails->Unread; ?>)</title>

<style>
a {color:blue; text-decoration:none;}
a:hover {color:blue; text-decoration:underline;}
a:visited {color:blue; text-decoration:none;}
</style>

</head>
<body>
<table style='border-collapse:collapse'>

<?php

if($_REQUEST['delete']) {
    $number=$_REQUEST['delete'];
    imap_delete($connection,$number);
    imap_expunge($connection);
}

if($_REQUEST['unread']) {
    $number=$_REQUEST['unread'];
    imap_clearflag_full($connection, $number, '\\Seen');
    
}

if($_REQUEST['msg']) {
    $number=$_REQUEST['msg'];

    echo "<pre>";
    echo imap_body($connection,$number);
    echo "</pre><p>\n\n";
       
    echo "<a href='javascript:history.back()'>Back</a>";
    echo "<br><a href='mailbox.php?delete=$number'>Delete</a>";
    echo "<br><a href='mailbox.php?unread=$number'>Mark as unread</a>";
       
} else {
    if($_REQUEST['compose']=="new") {
        if($_POST['send_m']) {
            $to=$_POST['to'];
            $subject=$_POST['title'];
            $message=$_POST['mail'];

            imap_mail($to,$subject,$message,"From: $from");
        }
        ?>
<form method=POST>
To: <input type="text" name="to"><br>
Title:<input type="text" name="title"><p>
Mail:<br>
<textarea name='mail'>
</textarea><p>
<input type="submit" name='send_m'  value='Send'>
</form>
    <?php
    } else {
        $emails=imap_num_msg($connection);
        echo "<strong>" . $from . "</strong> ";
        if($emails==0) {
            echo "<i>No e-mails</i>";
        } else       
        if($emails==1) {
            echo "1 e-mail<br>";
        } else
            echo "$emails e-mails<br>";
            if($newemails->Unread==1) {
                 echo "<strong>1</strong> unread e-mail<p>";
            } else
            echo "<strong>" . $newemails->Unread . "</strong> unread e-mails<p>";
            echo "<p><a href='mailbox.php?compose=new' style='text-decoration:underline'>Compose new e-mail</a><p>";

            echo "<tr><td>Sender</td><td>Subject</td><td>Recipient</td><td>Date</td></tr>";

            $msgorders = imap_sort($connection, SORTDATE, 1);
            // for($i=1;$i<=$emails;$i++) {
            foreach ($msgorders as $msgorder) {
                $chead=imap_headerinfo($connection,$msgorder);
                $mid=ltrim($chead->Msgno);
                date_default_timezone_set('America/Chicago');
                
                if ($chead->Unseen == 'U' || $chead->Recent == 'N') {$td = "<td style='font-weight:bold'>";}
                else {$td = "<td";}
                   
                echo "<tr style='border-style:solid none solid none; border-width:1px'>$td<a href='mailbox.php?msg=$mid'>";

                echo $chead->senderaddress;
                echo "</a></td>$td<a href='mailbox.php?msg=$mid'>";
                echo $chead->subject;
                echo "</a></td>$td<a href='mailbox.php?msg=$mid'>";
                echo $chead->toaddress;
                echo "</a></td>$td<a href='mailbox.php?msg=$mid'>";
                $convertdate = strtotime($chead->date); //convert to timestamp
                $showdate = date("D, j M",$convertdate); //format date from timestamp
                $arcdate = date("D, j M Y",$convertdate);
                $today = date("D, j M");
                $yesterday = date("D, j M", strtotime("-1 day"));
                $thisyear = date("Y");
                $msgyear = date("Y",$convertdate);;
                   if($today==$showdate) {
                   echo "Today";} else
                   if($yesterday==$showdate) {
                   echo "Yesterday";} else
                   if($thisyear!=$msgyear) {
                   echo $arcdate;} else
                echo $showdate;
                echo "</a></td></tr>";

            }
        }
    }
imap_close($connection);

?>

</table>
</body>
</html>

Is there anything here causing the e-mails to disappear? The last thing I changed was to add a count of the unread e-mails to the <title> tag.

Nothing is obviously wrong that I can see. Suggest that might want to add a log for messages deleted, added etc. Until you have it sorted out you may want to remove the expunge and do some testing to see if you can see what is happening. If you have a mail reader open for the same mail file, you should see the (delete) boxes ticked once you've done the delete with no expunge. That will show you what it was going to delete. The code is driven by $_REQUEST so maybe it's doing what it's being asked to do but the "request" is for the wrong record number.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.