PHP code example of port-adhoc / imap
1. Go to this page and download the library: Download port-adhoc/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/ */
port-adhoc / imap example snippets
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 = '[email protected] ';
$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();
}
public function getName(): string
use PortAdhoc\Imap\Imap;
$imap = new Imap;
$imap->server = 'example.host.com';
$imap->port = 993;
$imap->flags = ['imap', 'ssl', 'readonly'];
$imap->user = '[email protected] ';
$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;
}
public function getContent(): string
use PortAdhoc\Imap\Imap;
$imap = new Imap;
$imap->server = 'example.host.com';
$imap->port = 993;
$imap->flags = ['imap', 'ssl', 'readonly'];
$imap->user = '[email protected] ';
$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 );
}
public function __construct(): PortAdhoc\Imap\Imap
use PortAdhoc\Imap\Imap;
$imap = new Imap;
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
public function connect(): PortAdhoc\Imap\Imap
use PortAdhoc\Imap\Imap;
$imap = new Imap;
$imap->server = 'example.host.com';
$imap->port = 993;
$imap->flags = ['imap', 'ssl', 'readonly'];
$imap->user = '[email protected] ';
$imap->password = 'example';
$imap->mailbox = 'INBOX'; // or INBOX/folder
$imap->start = '1'; // uid
$imap->end = '500'; // uid
$imap->connect();
public function getMessage( int $uid ): PortAdhoc\Imap\Message
use PortAdhoc\Imap\Imap;
$imap = new Imap;
$imap->server = 'example.host.com';
$imap->port = 993;
$imap->flags = ['imap', 'ssl', 'readonly'];
$imap->user = '[email protected] ';
$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 );
public function getMessages(): PortAdhoc\Imap\Message[]
use PortAdhoc\Imap\Imap;
$imap = new Imap;
$imap->server = 'example.host.com';
$imap->port = 993;
$imap->flags = ['imap', 'ssl', 'readonly'];
$imap->user = '[email protected] ';
$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 ) {
// ...
}
public function getConnectionString(): string
use PortAdhoc\Imap\Imap;
$imap = new Imap;
$imap->server = 'example.host.com';
$imap->port = 993;
$imap->flags = ['imap', 'ssl', 'readonly'];
$imap->user = '[email protected] ';
$imap->password = 'example';
$imap->mailbox = 'INBOX'; // or INBOX/folder
$imap->start = '1'; // uid
$imap->end = '500'; // uid
$cs = $imap->getConnectionString();
echo $cs;
public $email;
public $name;
use PortAdhoc\Imap\Imap;
$imap = new Imap;
$imap->server = 'example.host.com';
$imap->port = 993;
$imap->flags = ['imap', 'ssl', 'readonly'];
$imap->user = '[email protected] ';
$imap->password = 'example';
$imap->mailbox = 'INBOX';
$uid = 500;
$message = $imap->getMessage( $uid );
$from = $message->getFrom();
$email = $from->email;
$name = $from->name;
echo $name . ' ' . $email;
bash
> php script.php
reporting result quarter 4.xlsx
bash
/vendor
composer.json
composer.lock
script.php
reporting result quarter 4.xlsx
bash
> php script.php
{example.host.com:993/imap/ssl/readonly}INBOX/folder
bash
> php script.php
John Doe [email protected]