PHP IMAP

在 PHP 中管理邮箱,过滤/获取/删除邮件(支持 IMAP/POP3/NNTP)。「Manage mailboxes, filter/get/delete emails in PHP (supports IMAP/POP3/NNTP)」

  • 所有者: barbushin/php-imap
  • 平台:
  • 许可证: MIT License
  • 分类:
  • 主题:
  • 喜欢:
    0
      比较:

Github星跟踪图

PHP IMAP

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

Initially released in December 2012, the PHP IMAP Mailbox is a powerful and open source library to connect to a mailbox by POP3, IMAP and NNTP using the PHP IMAP extension. This library allows you to fetch emails from your email server. Extend the functionality or create powerful web applications to handle your incoming emails.

Features

  • Connect to mailbox by POP3/IMAP/NNTP, using PHP IMAP extension
  • Get emails with attachments and inline images
  • Get emails filtered or sorted by custom criteria
  • Mark emails as seen/unseen
  • Delete emails
  • Manage mailbox folders

Requirements

  • PHP 5.6, 7.0, 7.1, 7.2, 7.3 or 7.4
  • PHP imap extension must be present; so make sure this line is active in your php.ini: extension=php_imap.dll
  • PHP mbstring extension must be present; so make sure this line is active in your php.ini: extension=php_mbstring.dll
  • PHP iconv extension must be present, if mbstring is not available; so make sure this line is active in your php.ini: extension=php_iconv.dll

Installation by Composer

Install the latest available release:

$ composer require php-imap/php-imap

Install the latest available and stable source code from master, which is may not released / tagged yet:

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

Install the latest available and may unstable source code from develop, which is may not properly tested yet:

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

PHPUnit Tests

Before you can run the PHPUnit tests you may need to run composer install to install all (development) dependencies.

You can run all PHPUnit tests by running the following command (inside of the installed php-imap directory): php vendor/bin/phpunit --testdox

Integration with frameworks

Getting Started Example

Below, you'll find an example code how you can use this library. For further information and other examples, you may take a look at the 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)
);

try {
	// Get all emails (messages)
	// PHP.net imap_search criteria: http://php.net/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());

Method imap() allows to call any imap function in a context of the the instance:

// Call imap_check(); 	
// http://php.net/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;

Some request require much time and resources:

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

主要指标

概览
名称与所有者barbushin/php-imap
主编程语言PHP
编程语言PHP (语言数: 1)
平台
许可证MIT License
所有者活动
创建于2012-12-06 03:16:38
推送于2024-04-25 07:07:43
最后一次提交2022-12-05 16:47:03
发布数65
最新版本名称5.0.1 (发布于 )
第一版名称1.0 (发布于 )
用户参与
星数1.7k
关注者数70
派生数461
提交数1.1k
已启用问题?
问题数429
打开的问题数69
拉请求数235
打开的拉请求数11
关闭的拉请求数57
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?