iiifx/php-imap

管理邮箱,在PHP中筛选/获取/删除电子邮件(支持IMAP/POP3/NNTP)

4.1.0 2020-06-14 20:18 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 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() 允许在实例上下文中调用任何 imap 函数

// Call imap_check();
// https://php.ac.cn/manual/en/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);

推荐