企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] > [github](https://github.com/barbushin/php-imap) ## php-imap 依赖 php 的 官方 imap 的扩展支持 ## 安装 ``` composer require php-imap/php-imap ``` ## 示例 ``` // 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) true, // Trim leading/ending whitespaces of IMAP path (optional) false // Attachment filename mode (optional; false = random filename; true = original filename) ); // set some connection arguments (if appropriate) $mailbox->setConnectionArgs( CL_EXPUNGE // expunge deleted mails upon mailbox close ); 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: " . implode(",", $ex->getErrors('all')); 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()); ```