1. Go to this page and download the library: Download benpoulson/php-imap library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
benpoulson / php-imap example snippets
// Create PhpImap\Mailbox instance for all further actions
$mailbox = new PhpImap\Mailbox(
'{imap.gmail.com:993/imap/ssl}INBOX', // IMAP server and mailbox folder
'[email protected]', // Username for the before configured mailbox
'*********', // Password for the before configured username
__DIR__, // Directory, where attachments will be saved (optional)
'UTF-8' // Server encoding (optional)
);
try {
// Get all emails (messages)
// PHP.net imap_search criteria: http://php.net/manual/en/function.imap-search.php
$mailsIds = $mailbox->searchMailbox('ALL');
} catch(PhpImap\Exceptions\ConnectionException $ex) {
echo "IMAP connection failed: " . $ex;
die();
}
// If $mailsIds is empty, no emails could be found
if(!$mailsIds) {
die('Mailbox is empty');
}
// Get the first message
// If '__DIR__' was defined in the first line, it will automatically
// save all attachments to the specified directory
$mail = $mailbox->getMail($mailsIds[0]);
// Show, if $mail has one or more attachments
echo "\nMail has attachments? ";
if($mail->hasAttachments()) {
echo "Yes\n";
} else {
echo "No\n";
}
// Print all information of $mail
print_r($mail);
// Print all attachements of $mail
echo "\n\nAttachments:\n";
print_r($mail->getAttachments());
// Call imap_check();
// http://php.net/manual/en/function.imap-check.php
$info = $mailbox->imap('check'); //
// Show current time for the mailbox
$currentServerTime = isset($info->Date) && $info->Date ? date('Y-m-d H:i:s', strtotime($info->Date)) : 'Unknown';
echo $currentServerTime;
// If you don't need to grab attachments you can significantly increase performance of your application
$mailbox->setAttachmentsIgnore(true);
// get the list of folders/mailboxes
$folders = $mailbox->getMailboxes('*');
// loop through mailboxs
foreach($folders as $folder) {
// switch to particular mailbox
$mailbox->switchMailbox($folder['fullpath']);
// search in particular mailbox
$mails_ids[$folder['fullpath']] = $mailbox->searchMailbox('SINCE "1 Jan 2018" BEFORE "28 Jan 2018"');
}
print_r($mails_ids);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.