Red
Red

Reputation: 6398

Get particular imap folder names PHP

I need to get the imap folder names , and normally i do

<?php
$mbox = imap_open(SERVER,USERNAME,PASSWORD)
      or die("can't connect: " . imap_last_error());

$list = imap_getmailboxes($mbox, SERVER, "*");
if (is_array($list)) {
    foreach ($list as $key => $val) {
        echo "($key) ";
        echo imap_utf7_decode($val->name) . ",";
        echo "'" . $val->delimiter . "',";
        echo $val->attributes . "<br />\n";
    }
} else {
    echo "imap_getmailboxes failed: " . imap_last_error() . "\n";
}

imap_close($mbox);
?>

It will give us an array of mailbox names , but what i need to get is particular mailbox names.

Ex : for email sent items some servers gives me Sent Items and some others Sent. So it depending on the server.

My requirement is i need to search for Inbox and Sent Items.

What is the most preferable way to achieve this ?

Upvotes: 1

Views: 2113

Answers (1)

Anshul
Anshul

Reputation: 1446

This is a very tricky question. The folder name is totally dependent on servers implementation. Different mailers have a different patterns for names of the folders. If you are planning to use this code for generic purpose then there is no solution to it. There is no way to identify the folder type by its name.

Even for specific mailers its difficult to identify the Sent items folder because of following reasons:

If you are planning to have a regex or pattern matching then the users are free to create a user folder with names like: Sent Ansh or SentMailAnsh or AnshSentMail

Secondly Clients like Outlook(2010 i suppose) for IMAP gives you the flexibility to store the Sent Items mails in specific folders so even the System has a folder named "Sent Mails" the user might prefer to store these mails in a new folder named "AnshSend"

So I would recommend you to analyze the situation based on your requirements.

Upvotes: 3

Related Questions