有了賬號名和獲取密碼后,就是程序編寫和服務(wù)器的問題,正常情況下PHP沒有安裝IMAP擴展,所以大部分服務(wù)器需要開啟IMAP擴展,
如果是Windows服務(wù)器,修改PHP.ini文件,把imap擴展前面的分號去掉,然后重啟PHP服務(wù)即可。
如果是Linux服務(wù)器則稍微麻煩一些,這里我遇到的坑是WDCP安裝的PHP版本沒有帶imap擴展文件,需要自己下載,然后上傳到對應(yīng)文件夾后,才能安裝。
安裝流程如下
首先,用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即可
相關(guān)讀取郵件的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標(biāo)簽*必須*放在最前面,任何其他內(nèi)容都*必須*跟隨其后! -->
<title>方維網(wǎng)絡(luò)-接收郵件測試</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">郵件開始數(shù):</span>
<input type="number" class="form-control" value="<?php echo $mail_i;?>" name="index" placeholder="郵件開始數(shù)" value="1" aria-describedby="basic-addon1">
</div>
<button type="submit" class="btn btn-default">獲取郵件</button>
<input type="hidden" name="action" value="get" />
</form>
郵箱密碼是郵箱設(shè)置的imap密碼,一次獲取6封郵件
</div>
<?php
if($conn) {
echo '總郵件數(shù):' . $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>發(fā)件人:</b>' . $mailHeader['from'] . '</td></tr><tr><td><b>標(biāo)題:</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>';
}
}
就這樣實現(xiàn)了PHP獲取QQ郵件的功能,截圖如下