haogood/php-imap

Get email contents and attachments using php imap extension.


Keywords
php, imap, pop3, mailbox, receive emails
License
MIT

Documentation

Author Release Software License Size

Requirements

  • PHP >= 5.5.0
  • IMAP PHP Extension

Installation

composer require haogood/php-imap

Initialize

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

Search

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

Fetch header info

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();

}

Fetch attachments

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();

    }
}

Reference

License

haogood/php-imap is licensed under The MIT License (MIT).