Imap class for PHP

v0.1.31 2017-11-30 14:35 UTC

README

Packagist minimum PHP version Packagist version Packagist licence

imap-php

Uid oriented Imap class

Summary

Installation

In your repository :

composer require port-adhoc/imap

back to summary

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

back to summary

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;

back to function list

back to summary

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

back to function list

back to summary

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

back to function list

back to summary

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

back to function list

back to summary

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 ) {
  // ...
}

back to function list

back to summary

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

back to function list

back to summary

Email

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

back to function list

back to summary