Skip to content
Stefan edited this page Apr 15, 2018 · 6 revisions

Welcome to the PHP-SMTP-Mailer wiki!

<?php

require 'src/PhpImap/Mailbox.php';

$hoststring = '{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX';
$username   = 'halojoy@gmail.com';
$password   = 'K7meRZ^}6s';
$attachdir  = 'attachments';

// Construct the $mailbox handle
$mailbox = new Mailbox($hoststring, $username, $password, $attachdir);

// Get INBOX emails after date 2017-07-01
$mailsIds = $mailbox->searchMailbox('SINCE "20170701"');
if(!$mailsIds) exit('Mailbox is empty');

// Put the latest email on top of listing
rsort($mailsIds);

// Show the total number of emails loaded
echo 'n= '.count($mailsIds).'<br>';

// Get the last 15 emails only
array_splice($mailsIds, 15);

// Loop through emails one by one
foreach($mailsIds as $num) {
    
    // Show header with subject and data on this email
    $head = $mailbox->getMailHeader($num);
    echo '<div style="text-align:center"><b>';
    echo $head->subject.'</b>&nbsp;&nbsp;(';
    if     (isset($head->fromName))    echo 'by '.$head->fromName.' on ';
    elseif (isset($head->fromAddress)) echo 'by '.$head->fromAddress.' on ';
    echo $head->date.')';
    echo '</div>';
    
    // Show the main body message text
    // Do not mark email as seen
    $markAsSeen = false;
    $mail = $mailbox->getMail($num, $markAsSeen);
    if ($mail->textHtml)
        echo $mail->textHtml;
    else
        echo $mail->textPlain;
    
    // Load eventual attachment into attachments directory
    $mail->getAttachments();

}

$mailbox->disconnect();
Clone this wiki locally