Jump to content

Recommended Posts

Hi all,

I need your help. I am working on my code as I want to set the limit to 25 as I want to fetch 25 emails from gmail using imap.

 

When I try this:

 

<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

// Load Composer's autoloader
require_once "Mail.php";
require_once "Mail/mime.php";
require_once('Mail/IMAPv2.php');

$username = 'myusername@gmail.com';
$password = 'mypassword';

$server = '{imap.gmail.com:993/imap/ssl/ssl}INBOX';
$inbox = imap_open($server, $username, $password);

$test = listMessages($inbox);

print_r($test);


function listMessages($inbox) {
    $page = 1;
    $per_page = 25;
    $sort = null;
    $limit = ($per_page * $page);
    $start = ($limit - $per_page) + 1;
    $start = ($start < 1) ? 1 : $start;
    $limit = (($limit - $start) != ($per_page-1)) ? ($start + ($per_page-1)) : $limit;
    $info = imap_check($inbox);
    $limit = ($info->Nmsgs < $limit) ? $info->Nmsgs : $limit;
    $msgs = '';
    $result = '';

    
     $perPage = 25;
     $start = $page == 1 ? 0 : $page * $perPage - ($perPage - 1);
     $order = 0;
     $by = SORTDATE;


     if (is_array($sort)) {
         $order = $this->sortBy['order'][$sort[0]];
         $by = $this->sortBy['by'][$sort[1]];
     }
     $sorted = imap_sort($inbox, $by, $order);

    $mails = array_chunk($sorted, $perPage);
    $mails = $mails[$page - 1];
    print_r($mails);

    return $return;
}

?>

 

It will fetch and display 77,500 emails which is too much so I need to reduce it.

How I can fetch 25 emails from gmail using imap when I connect to my gmail inbox?

Link to comment
https://forums.phpfreaks.com/topic/317424-set-imap-pagination/
Share on other sites

Your code isn't that far off.  imap_sort gives you a list of message numbers after the sorting has been applied.  You have that list split into pages using array_chunk.  Take the page you want and fetch those messages.  The second parameter to imap_fetch_overview is how you specify the messages you want to fetch, and documented as:

Quote

A message sequence description. You can enumerate desired messages with the X,Y syntax, or retrieve all messages within an interval with the X:Y syntax

So to fetch arbitrary messages, specify them as a comma-separated list of message numbers.  To do that, you can just implode your page of numbers that you get from the array_chunk result.

There is a lot of stuff in your listMessages function that is not necessary and can be removed.  After cleaning up the function and adding the call to imap_fetch_overview, the function would look like:

function listMessages($inbox, int $page, int $perPage) : array{
    $sorted = imap_sort($inbox, SORTDATE, 1);
    $pageList = array_chunk($sorted, $perPage);
    $messagesToFetch = implode(',', $pageList[$page - 1]);

    $details = imap_fetch_overview($inbox, $messagesToFetch);

    return array_reverse($details);
}

You can then use it to fetch a page of messages and display the summary info.

$messages = listMessages($inbox, $page ?? 1, 25);
foreach ($messages as $item){
    $number = $item->msgno;
    $date = $item->date ?? 'Unknown';
    $subject = $item->subject ?? '';
    printf("[%d] %s - %s\r\n", $number, $date, $subject);
}

 

Link to comment
https://forums.phpfreaks.com/topic/317424-set-imap-pagination/#findComment-1612853
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.