Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integrating IMAP on php website

I am trying to create a IMAP service on my website with help of php. It basically has few steps.

The main part that I want is that i get a list of folders of gmail account, on the click of a particular folder, the list of mails in that folder should get open and on click of any particular mail, its details should get open.

I have the list of folders, i have a list of mails, i have details of mails, but they are separate part, but i wish to combine and create one process as stated above.

The code of 3 steps is given below

Step 1 List folders: It will list all the folders of gmail. Code that i have is

$folders = imap_list($imap, "{imap.gmail.com:993/imap/ssl}", "*");
echo "<ul>";
foreach ($folders as $folder) {
    $folder = str_replace("{imap.gmail.com:993/imap/ssl}", "", $folder);
     $folder = str_replace("[Gmail]/", "", $folder);
    echo '<li>' . $folder . '</li>';
}
echo "</ul>";

o/p of step 1

INBOX
DRAFT
SENT
TRASH

Step 2 List email (clicking on a folder mail list should get opened)

$numMessages = imap_num_msg($imap);

for ($i = $numMessages; $i > ($numMessages - 20); $i--)
    {
        $header = imap_header($imap, $i);
        $fromInfo = $header->from[0];
        $replyInfo = $header->reply_to[0];

        $details = array(
        "fromAddr" => (isset($fromInfo->mailbox) && isset($fromInfo->host))
            ? $fromInfo->mailbox . "@" . $fromInfo->host : "",
        "fromName" => (isset($fromInfo->personal))
            ? $fromInfo->personal : "",
        "replyAddr" => (isset($replyInfo->mailbox) && isset($replyInfo->host))
            ? $replyInfo->mailbox . "@" . $replyInfo->host : "",
        "replyName" => (isset($replyTo->personal))
            ? $replyto->personal : "",
        "subject" => (isset($header->subject))
            ? $header->subject : "",
        "udate" => (isset($header->udate))
            ? $header->udate : ""
        );

    $uid = imap_uid($imap, $i);

    $datee= gmdate("F j, Y, g:i a", $details["udate"] );

    echo "<ul>";
    echo "<li><strong>From:</strong>" . $details["fromName"];
    echo " " . $details["fromAddr"] . "</li>";
    echo "<li><strong>Subject:</strong> " . $details["subject"] . "</li>";
    echo "<li><strong>DATE:</strong> " . $datee . "</li>";

    } 

o/p of step 2 (clicking on a particular mail content of that mail should get opened)

From:ABC
Subject: TOPIC
DATE: September 2, 2015, 9:00 am

Step 3 View messages

$message_count = imap_num_msg($imap);

    for ($i = 1; $i <= $message_count; ++$i) {
        $header = imap_header($imap, $i);
        $body = trim(substr(imap_body($imap, $i), 0, 100));
        $prettydate = date("jS F Y", $header->udate);

        if (isset($header->from[0]->personal)) {
            $personal = $header->from[0]->personal;
        } else {
            $personal = $header->from[0]->mailbox;
        }

        $email = "$personal <{$header->from[0]->mailbox}@{$header->from[0]->host}>";
        echo "On $prettydate, $email said \"$body\".\n";
    }

Can anyone please tel how i can achieve the above requirement

like image 595
Sam Avatar asked Sep 02 '15 04:09

Sam


1 Answers

If you want to do this without a page reload after each selection, you will have to use AJAX requests. You will need:

1) One normal page which lists the available folders in a dropdown. Selecting a folder in the dropdown will need to trigger a Javascript function which performs an AJAX request.

2) You will then need to handle the AJAX request from step 1) and return a data structure containing all the available messages. Again using JS populate a table or list or whatever on the same page in step 1).

3) Finally, you will need to attach another Javascript method to the messages defined in step 2) to make another AJAX request to the server to return the individual message content, and then using JS insert this to the message pane of the page from step 1).

If you want to make generating AJAX requests easy I can recommend JQuery of course - https://api.jquery.com/jquery.ajax/

Secondly, to help the PHP code interacting with the IMAP server I can also highly recommend the Fetch library which will help you avoid a lot of pitfalls and abstract complexity out of your code. https://github.com/tedious/Fetch

like image 160
Benr77 Avatar answered Oct 29 '22 15:10

Benr77