php代码:
/**
php使用imap_open读取qq邮箱
imap_open 官方文档:
https://www.php.net/function.imap_open
*/
function parse_mailstr($subject) {
$a = explode('?',$subject);
$n = count($a);
$a = $a[$n-2];
return base64_decode($a);
}
function recevie_email() {
// 腾讯 qq 邮箱为例。
// 官方文档:https://service.mail.qq.com/detail/0/339
$mailbox = '{imap.qq.com:993/imap/ssl}inbox';
// 要读取的邮箱
$user = 'youremail@qq.com';
// 邮箱密码或授权码。邮箱授权码生成路径如下:
// 进入 qq 邮箱,依次进入【设置】-【账号】-【pop3/imap/smtp/exchange/carddav/caldav服务-管理服务】-【安全设置】-【生成授权码】
$password = '邮箱密码或授权码';
$inbox = imap_open($mailbox, $user, $password) or die(imap_last_error());
if ($inbox) {
$nummsg = imap_num_msg($inbox);
echo '<dt>共有 '.$nummsg.' 条未读邮件</dt>';
for ($i = 1; $i <= $nummsg; ++$i) {
$hd = imap_headerinfo($inbox, $i);
$subject = parse_mailstr($hd->subject);
echo '<dd>';
echo "标 题:$subject <br />";
echo '发件人:'. $hd->from[0]->mailbox .'@'. $hd->from[0]->host .'<br />';
if (isset($hd->date)) {
$date = strtotime($hd->date);
$date = date('y-m-d h:i:s',$date);
echo "时 间:$date <br />";
}
echo '</dd>';
}
imap_close($inbox);
}
}html代码:
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width,user-scalable=yes" />
<meta name="renderer" content="webkit" />
<style>
dd{
border-bottom: 1px solid #888;
padding-bottom: 10px;
padding-top: 10px;
}
</style>
<title>php使用imap_open读取qq邮箱</title>
</head>
<body>
<dl>
<?php recevie_email();?>
</dl>
</body>
</html>获得邮箱授权码方法:
进入 qq 邮箱,依次进入【设置】-【账号】-【pop3/imap/smtp/exchange/carddav/caldav服务-管理服务】-【安全设置】-【生成授权码】。


到此这篇关于php使用imap_open实现读取qq邮箱的文章就介绍到这了,更多相关php读取qq邮箱内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论