Делал для себя.
В данном примере используются библиотеки (Composer):
ddeboer/imap
soundasleep/html2text
<?php
use Ddeboer\Imap\Search\Flag\Unseen;
use Ddeboer\Imap\SearchExpression;
use Ddeboer\Imap\Server;
$server = (new Server('imap.example.com'))->authenticate('user', 'pass');
$mailbox = $server->getMailbox('INBOX');
$sr = new SearchExpression();
$sr->addCondition(new Unseen());
$messages = $mailbox->getMessages($sr);
foreach ( $messages as $msg) {
try {
if($html = $msg->getBodyHtml()){
$options = array(
'ignore_errors' => true,
);
$text = \Soundasleep\Html2Text::convert($html, $options);
} else {
$text = $msg->getBodyText();
}
foreach ( $msg->getAttachments() as $attachment) {
$path_to_save = '/files/' . $attachment->getFilename();
file_put_contents($path_to_save,
$attachment->getDecodedContent());
}
} catch (Exception $e) {
error_log($e);
} finally {
$msg->markAsSeen();
}
}