luchaninov/php-imap

此包已被弃用,不再维护。作者建议使用 php-imap/php-imap 包代替。

管理邮箱,在PHP中过滤/获取/删除邮件(支持IMAP/POP3/NNTP)

4.2.0 2021-04-09 13:09 UTC

README

GitHub release Software License Packagist Build Status Supported PHP Version Maintainability Test Coverage Type Coverage

最初发布于2012年12月,PHP IMAP邮箱是一个功能强大且开源的库,可以通过PHP IMAP扩展连接到邮箱,支持POP3、IMAP和NNTP。这个库允许您从您的邮件服务器中获取邮件。扩展功能或创建强大的Web应用程序来处理您的收件箱。

功能

  • 通过PHP IMAP扩展连接到邮箱(POP3/IMAP/NNTP)
  • 获取带附件和内联图片的邮件
  • 通过自定义标准过滤或排序邮件
  • 标记邮件为已读/未读
  • 删除邮件
  • 管理邮箱文件夹

要求

PHP版本 php-imap版本
5.6 3.x
7.0 3.x
7.1 3.x
7.2 3.x, 4.x
7.3 3.x, 4.x
7.4 >3.0.33, 4.x
8.0 >3.0.33, 4.x
  • PHP fileinfo 扩展必须存在;请确保您的php.ini中此行已激活:extension=php_fileinfo.dll
  • PHP iconv 扩展必须存在;请确保您的php.ini中此行已激活:extension=php_iconv.dll
  • PHP imap 扩展必须存在;请确保您的php.ini中此行已激活:extension=php_imap.dll
  • PHP mbstring 扩展必须存在;请确保您的php.ini中此行已激活:extension=php_mbstring.dll

通过Composer安装

安装最新版本

$ composer require php-imap/php-imap

master安装最新和稳定的源代码,可能尚未发布/标记

$ composer require php-imap/php-imap:dev-master

develop安装最新和可能不稳定的源代码,可能尚未经过适当测试

$ composer require php-imap/php-imap:dev-develop

运行测试

在您运行任何测试之前,您可能需要运行composer install来安装所有(开发)依赖项。

运行所有测试

您可以通过运行以下命令(在安装的php-imap目录内)来运行所有可用的测试:composer run tests

仅运行PHPUnit测试

您可以通过运行以下命令(在安装的php-imap目录内)来运行所有PHPUnit测试:php vendor/bin/phpunit --testdox

与框架集成

入门示例

以下是一个示例代码,展示如何使用这个库。有关更多信息和其他示例,您可以查看wiki

// Create PhpImap\Mailbox instance for all further actions
$mailbox = new PhpImap\Mailbox(
	'{imap.gmail.com:993/imap/ssl}INBOX', // IMAP server and mailbox folder
	'some@gmail.com', // Username for the before configured mailbox
	'*********', // Password for the before configured username
	__DIR__, // Directory, where attachments will be saved (optional)
	'UTF-8' // Server encoding (optional)
);

// set some connection arguments (if appropriate)
$mailbox->setConnectionArgs(
    CL_EXPUNGE // expunge deleted mails upon mailbox close
    | OP_SECURE // don't do non-secure authentication
);

try {
	// Get all emails (messages)
	// PHP.net imap_search criteria: https://php.ac.cn/manual/en/function.imap-search.php
	$mailsIds = $mailbox->searchMailbox('ALL');
} catch(PhpImap\Exceptions\ConnectionException $ex) {
	echo "IMAP connection failed: " . $ex;
	die();
}

// If $mailsIds is empty, no emails could be found
if(!$mailsIds) {
	die('Mailbox is empty');
}

// Get the first message
// If '__DIR__' was defined in the first line, it will automatically
// save all attachments to the specified directory
$mail = $mailbox->getMail($mailsIds[0]);

// Show, if $mail has one or more attachments
echo "\nMail has attachments? ";
if($mail->hasAttachments()) {
	echo "Yes\n";
} else {
	echo "No\n";
}

// Print all information of $mail
print_r($mail);

// Print all attachements of $mail
echo "\n\nAttachments:\n";
print_r($mail->getAttachments());

方法imap()允许在实例的上下文中调用任何PHP IMAP函数。示例

// Call imap_check() - see https://php.ac.cn/manual/function.imap-check.php
$info = $mailbox->imap('check');


// Show current time for the mailbox
$currentServerTime = isset($info->Date) && $info->Date ? date('Y-m-d H:i:s', strtotime($info->Date)) : 'Unknown';

echo $currentServerTime;

某些请求需要大量的时间和资源

// If you don't need to grab attachments you can significantly increase performance of your application
$mailbox->setAttachmentsIgnore(true);

// get the list of folders/mailboxes
$folders = $mailbox->getMailboxes('*');

// loop through mailboxs
foreach($folders as $folder) {

	// switch to particular mailbox
	$mailbox->switchMailbox($folder['fullpath']);

	// search in particular mailbox
	$mails_ids[$folder['fullpath']] = $mailbox->searchMailbox('SINCE "1 Jan 2018" BEFORE "28 Jan 2018"');
}

print_r($mails_ids);

从3.x版本升级

在3.1之前,Mailbox使用了一个“魔法”方法(Mailbox::imap()),现在Imap类执行此功能,调用许多imap_*函数,并自动进行参数和返回值的字符串编码/解码

之前

    public function checkMailbox()
    {
        return $this->imap('check');
    }

之后

    public function checkMailbox(): object
    {
        return Imap::check($this->getImapStream());
    }

推荐