PHP code example of haogood / php-imap

1. Go to this page and download the library: Download haogood/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/ */

    

haogood / php-imap example snippets


use IMAP\IMAPMailbox;
$host = '{imap.gmail.com:993/imap/ssl}';
$user = '[email protected]';
$pwd = '******';
$mailbox = new IMAPMailbox($host, $user, $pwd);

$emails = $mailbox->search('ALL');

foreach ($emails as $email) {

    // Header info
    $headerinfo = $email->fetchHeaderinfo();

    // Author
    $author = $headerinfo->from->personal;

    // Sender address
    $from = $headerinfo->from->mailbox.'@'.$headerinfo->from->host;

    // Timestamp
    $timstamp = $headerinfo->udate;

    // Contents
    $contents = $email->getBody();

}

foreach ($emails as $email) {
    foreach($email->getAttachments() as $attachment) {

        // Filename
        $filename = $attachment->getFilename();

        // Extension
        $ext = $attachment->getExtension();

        // Attachment file
        $file = $attachment->getBody();

        // Attchment info
        $info = $attachment->getInfo();

    }
}

composer