1. 安装扩展imap
2.composer下载
composer require php-imap/php-imap
包地址:
3.使用
public function read_email(){
// Create PhpImap\Mailbox instance for all further actions
$mailbox = new \PhpImap\Mailbox(
‘{imap.qq.com:993/imap/ssl}INBOX‘, // IMAP server and mailbox folder
‘[email protected]‘, // 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();
}
// dd($mailsIds);
// 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
dd($mail);
// Print all attachements of $mail
echo "\n\nAttachments:\n";
dd($mail->getAttachments());//附件
}
注:
设置收信的范围
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。