port-adhoc / imap
PHP 的 Imap 类
v0.1.31
2017-11-30 14:35 UTC
Requires
- php: >=5.3.0
This package is not auto-updated.
Last update: 2024-09-29 03:41:52 UTC
README
imap-php
以 Uid 为导向的 Imap 类
摘要
安装
在您的仓库中
composer require port-adhoc/imap
使用
示例 1:遍历整个邮箱并检索其内容
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(); }
函数列表
附件
getName
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 = '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; }
结果
> php script.php
reporting result quarter 4.xlsx
getContent
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 = '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 ); }
结果
/vendor composer.json composer.lock script.php reporting result quarter 4.xlsx
Imap
Imap 构造
public function __construct(): PortAdhoc\Imap\Imap
示例
use PortAdhoc\Imap\Imap; $imap = new Imap;
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 = '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
示例
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[]
示例
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
示例
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;
结果
> php script.php
{example.host.com:993/imap/ssl/readonly}INBOX/folder
电子邮件
电子邮件属性
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 = '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;
结果
> php script.php
John Doe john.doe@contoso.com