有了账号名和获取密码后,就是程序编写和服务器的问题,正常情况下PHP没有安装IMAP扩展,所以大部分服务器需要开启IMAP扩展,
如果是Windows服务器,修改PHP.ini文件,把imap扩展前面的分号去掉,然后重启PHP服务即可。
如果是Linux服务器则稍微麻烦一些,这里我遇到的坑是WDCP安装的PHP版本没有带imap扩展文件,需要自己下载,然后上传到对应文件夹后,才能安装。
安装流程如下
首先,用Putty等工具进入linux
进入php源码包ext目录,再进入imap目录
cd /www/wdlinux/apache_php-5.5.38/include/php/ext/imap/
/www/wdlinux/apache_php-5.5.38/bin/phpize
./configure --with-php-config=/www/wdlinux/apache_php-5.5.38/bin/php-config --with-kerberos --with-imap --with-imap-ssl --with-libdir=lib64
make
make install
如果出错 安装 yum -y install libc-client-devel
修改php.ini
[imap]
extension_dir = /www/wdlinux/apache_php-5.5.38/lib/php/extensions/no-debug-non-zts-20121212
extension = imap.so
最后重启php即可
相关读取邮件的PHP代码如下:
<?php
ini_set("display_errors", "On");
error_reporting(E_ERROR);
header("Content-type: text/html; charset=utf-8");
$mailServer = 'imap.qq.com'; //IMAP server address
$mailLink = "{{$mailServer}:143}INBOX"; // 143 is the port when not SSL
$mailUser = $_GET['email']; // email address
$mailPass = $_GET['pwd'];// client authorization code
$mail_i = intval($_GET['index']);
if($mailUser != '' && $mailPass != '') {
$Email = new Email();
$conn = $Email->mailConnect($mailServer, 143, $mailUser, $mailPass);
}
function decode_title($str) {
$arr = imap_mime_header_decode($str);
return $arr[0]->text;
}
function decode_attach($str) {
$arr = imap_mime_header_decode($str);
return iconv($arr[0]->charset, "utf-8", $arr[0]->text);
}
?>
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
<title>天隆网络-接收邮件测试</title>
</head>
<body>
<div style="padding:40px;width:400px;">
<form method="get" action="">
<div class="input-group">
<span class="input-group-addon" id="basic-addon1">邮箱账号:</span>
<input type="text" class="form-control" value="<?php echo $mailUser;?>" name="email" placeholder="输入QQ邮箱账号:" aria-describedby="basic-addon1">
</div>
<div class="input-group">
<span class="input-group-addon" id="basic-addon1">邮箱密码:</span>
<input type="text" class="form-control" value="<?php echo $mailPass;?>" name="pwd" placeholder="QQ邮箱密码:" aria-describedby="basic-addon1">
</div>
<div class="input-group">
<span class="input-group-addon" id="basic-addon1">邮件开始数:</span>
<input type="number" class="form-control" value="<?php echo $mail_i;?>" name="index" placeholder="邮件开始数" value="1" aria-describedby="basic-addon1">
</div>
<button type="submit" class="btn btn-default">获取邮件</button>
<input type="hidden" name="action" value="get" />
</form>
邮箱密码是邮箱设置的imap密码,一次获取6封邮件
</div>
<?php
if($conn) {
echo '总邮件数:' . $Email->mailTotalCount() . '<br>';
for ($i = $mail_i; $i < $mail_i+6; $i++) {
$mailHeader = $Email->mailHeader($i);
$attach_list = $Email->getAttach($i, "attach/");
$subject = decode_title($mailHeader['subject']);
// print_r($mailHeader);
echo '<table border=1><tr><td><b>日期:</b>' . date('Y-m-d H:i:s', strtotime($mailHeader['date'])) . '</td></tr><tr><td><b>发件人:</b>' . $mailHeader['from'] . '</td></tr><tr><td><b>标题:</b>' . $subject . '</td></tr>';
echo '<tr><td>正文:</td></tr><tr><td>' . $Email->getBody($i) . '</td></tr><tr><td>是否已读:' . $Email->mailRead($i) . ' ID:' . $mailHeader['id'] . ' </td></tr>';
echo '<tr><td>附件:';
foreach ($attach_list as $attach) {
echo '<a href="attach/' . decode_attach($attach) . '" target="_blank">' . decode_attach($attach) . '</a>';
}
if (count($attach_list) == 0) {
echo '无附件';
}
echo '</td></tr>';
echo '</table>';
}
}
就这样实现了PHP获取QQ邮件的功能,截图如下