mark107 Posted November 8, 2023 Share Posted November 8, 2023 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? Quote Link to comment https://forums.phpfreaks.com/topic/317424-set-imap-pagination/ Share on other sites More sharing options...
mark107 Posted November 9, 2023 Author Share Posted November 9, 2023 any idea how i can fetch 20 emails using imap_fetch_overview?? Quote Link to comment https://forums.phpfreaks.com/topic/317424-set-imap-pagination/#findComment-1612826 Share on other sites More sharing options...
mark107 Posted November 11, 2023 Author Share Posted November 11, 2023 Does anyone know how I can fetch 20 emails when I am using imap??????? Quote Link to comment https://forums.phpfreaks.com/topic/317424-set-imap-pagination/#findComment-1612850 Share on other sites More sharing options...
kicken Posted November 12, 2023 Share Posted November 12, 2023 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); } Quote Link to comment https://forums.phpfreaks.com/topic/317424-set-imap-pagination/#findComment-1612853 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.