port-adhoc / imap
Imap class for PHP
v0.1.31
2017-11-30 14:35 UTC
Requires
- php: >=5.3.0
This package is not auto-updated.
Last update: 2024-11-10 04:12:17 UTC
README
imap-php
Uid oriented Imap class
Summary
Installation
In your repository :
composer require port-adhoc/imap
Usage
Example 1 : traversing a whole mailbox and retriving its content
use PortAdhoc\Imap\Imap; use PortAdhoc\Imap\Encoding; $imap = new Imap; $imap->server = 'example.host.com'; $imap->port = 993; $imap->flags = ['imap', 'ssl', 'readonly']; $imap->user = 'example@host.com'; $imap->password = 'example'; $imap->mailbox = 'INBOX'; $imap->start = '1'; $imap->end = '*'; $imap->connect(); $messages = $imap->getMessages(); foreach( $messages as $message ) { $subject = $message->getSubject(); $date = $message->getDate(); $from = $message->getFrom(); $to = $message->getTo(); $cc = $message->getCC(); $bcc = $message->getBCC(); $text = $message->getPlainText( Encoding::UTF_8 ); $html = $message->getPlainText( Encoding::UTF_8 ); $attachments = $message->getAttachments(); }
Function list
Attachment
getName
public function getName(): string
Example:
use PortAdhoc\Imap\Imap; $imap = new Imap; $imap->server = 'example.host.com'; $imap->port = 993; $imap->flags = ['imap', 'ssl', 'readonly']; $imap->user = 'example@host.com'; $imap->password = 'example'; $imap->mailbox = 'INBOX'; // or INBOX/folder $imap->start = '500'; // uid $imap->end = '500'; // uid $imap->connect(); $uid = 500; $message = $imap->getMessage( $uid ); $attachments = $message->getAttachments(); foreach( $attachments as $attachment ) { $name = $attachment->getName(); echo $name; }
Result:
> php script.php
reporting result quarter 4.xlsx
getContent
public function getContent(): string
Example:
use PortAdhoc\Imap\Imap; $imap = new Imap; $imap->server = 'example.host.com'; $imap->port = 993; $imap->flags = ['imap', 'ssl', 'readonly']; $imap->user = 'example@host.com'; $imap->password = 'example'; $imap->mailbox = 'INBOX'; // or INBOX/folder $imap->start = '500'; // uid $imap->end = '500'; // uid $imap->connect(); $uid = 500; $message = $imap->getMessage( $uid ); $attachments = $message->getAttachments(); foreach( $attachments as $attachment ) { $name = $attachment->getName(); $content = $attachement->getContent(); $path = __DIR__ . '/' . $name; // inside the current repository file_put_contents( $path, $content ); }
Result:
/vendor composer.json composer.lock script.php reporting result quarter 4.xlsx
Imap
Imap construct
public function __construct(): PortAdhoc\Imap\Imap
Example:
use PortAdhoc\Imap\Imap; $imap = new Imap;
Imap properties
public string $server; public int $port; public string $user; public string $password; public string $mailbox; public int $connection_time; public int $message_fetching_time; public string $start; // uid public string $end; // uid
connect
public function connect(): PortAdhoc\Imap\Imap
Example:
use PortAdhoc\Imap\Imap; $imap = new Imap; $imap->server = 'example.host.com'; $imap->port = 993; $imap->flags = ['imap', 'ssl', 'readonly']; $imap->user = 'example@host.com'; $imap->password = 'example'; $imap->mailbox = 'INBOX'; // or INBOX/folder $imap->start = '1'; // uid $imap->end = '500'; // uid $imap->connect();
getMessage
public function getMessage( int $uid ): PortAdhoc\Imap\Message
Example:
use PortAdhoc\Imap\Imap; $imap = new Imap; $imap->server = 'example.host.com'; $imap->port = 993; $imap->flags = ['imap', 'ssl', 'readonly']; $imap->user = 'example@host.com'; $imap->password = 'example'; $imap->mailbox = 'INBOX'; // or INBOX/folder $imap->start = '1'; // uid $imap->end = '500'; // uid $imap->connect(); $uid = 500; $message = $imap->getMessage( $uid );
getMessages
public function getMessages(): PortAdhoc\Imap\Message[]
Example:
use PortAdhoc\Imap\Imap; $imap = new Imap; $imap->server = 'example.host.com'; $imap->port = 993; $imap->flags = ['imap', 'ssl', 'readonly']; $imap->user = 'example@host.com'; $imap->password = 'example'; $imap->mailbox = 'INBOX'; // or INBOX/folder $imap->start = '1'; // uid $imap->end = '500'; // uid $imap->connect(); $messages = $imap->getMessages(); foreach( $messages as $message ) { // ... }
getConnectionString
public function getConnectionString(): string
Example:
use PortAdhoc\Imap\Imap; $imap = new Imap; $imap->server = 'example.host.com'; $imap->port = 993; $imap->flags = ['imap', 'ssl', 'readonly']; $imap->user = 'example@host.com'; $imap->password = 'example'; $imap->mailbox = 'INBOX'; // or INBOX/folder $imap->start = '1'; // uid $imap->end = '500'; // uid $cs = $imap->getConnectionString(); echo $cs;
Result:
> php script.php
{example.host.com:993/imap/ssl/readonly}INBOX/folder
Email properties
public $email; public $name;
Example:
use PortAdhoc\Imap\Imap; $imap = new Imap; $imap->server = 'example.host.com'; $imap->port = 993; $imap->flags = ['imap', 'ssl', 'readonly']; $imap->user = 'example@host.com'; $imap->password = 'example'; $imap->mailbox = 'INBOX'; $uid = 500; $message = $imap->getMessage( $uid ); $from = $message->getFrom(); $email = $from->email; $name = $from->name; echo $name . ' ' . $email;
Result:
> php script.php
John Doe john.doe@contoso.com