今天程序員在最近實施的項目中需要增加IMAP驗證,今天歷史原因,公司很多伺服器的linux操作系統及各應用程序版本都不一樣,安裝路徑也很雜亂,再加上剛接手伺服器不久,導致今天在安裝IMAP的PHP擴展時,走了很多彎路;幫把今天的操作經理寫下來供大家參考學習及備忘之。
環境:
[root@bjdx246 lib]# lsb_release -a
LSB Version: :core-3.1-amd64:core-3.1-ia32:core-3.1-noarch:graphics-3.1-amd64:graphics-3.1-ia32:graphics-3.1-noarch
Distributor ID: RedHatEnterpriseServer
Description: Red Hat Enterprise Linux Server release 5.4 (Tikanga)
Release: 5.4
Codename: Tikanga
[root@bjdx246 lib]# php -v
PHP 5.2.5 (cli) (built: May 29 2013 16:49:51)
Copyright (c) 1997-2007 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies
[root@bjdx246 /]# rpm -qa |grep php
php-ldap-5.1.6-43.el5_10
php-cli-5.1.6-43.el5_10
php-common-5.1.6-43.el5_10
再使用phpinfo.php 查看PHP相關信息後,開始安裝php-imap,步驟如下:
cd /usr/local/src/php-5.2.5/ext/imap
yum -y install libc-client-*
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config --with-kerberos --with-imap-ssl
make
make install
完成後,屏幕上會有提示/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613 目錄下多出一個 imap.so的文件;在上面編譯過程中,出現很多報錯,根據報錯一個網路找答案即可;
編輯 /usr/local/php/lib/php.ini ; 添加一行 extension=imap.so
最後 重啟 apache 服務搞定!
註:上面的路徑是本台伺服器的相關路徑,其它的伺服器需要根據您的實際情況進行更改,理解整個部署的思路即可哈!
2. IMAP是什麼,怎麼開通
imap就是一種郵件協議,你開啟之後就可以在手機上登陸管理你的郵箱了。拿QQ郵箱做例子,
我就是在網上的QQ郵箱的:設置-賬戶里開啟了imap,就可以用iphone自帶的郵件軟體收我的QQ郵件了
3. 什麼是IMAP。要怎麼開通IMAp
1.什麼是IMAP?
IMAP是Internet Message Access Protocol的縮寫,是用於訪問伺服器上所存儲的郵件的Internet協議。
2.IMAP的功能
同POP3相比,IMAP提供的郵件「摘要瀏覽」方式極大地提高了郵件瀏覽速度,可有效地節省客戶寶貴的時間。這對於經常接收大量郵件和希望阻止垃圾郵件的用戶來說此功能是非常實用的。用戶建立IMAP帳號後,可以指定哪些文件夾 顯示,哪些文件夾隱藏,利用IMAP提供的摘要瀏覽功能使用戶在閱讀完所有郵件的到達時間、發件人、主題、大小等信息後才做出是否下載的決定,同時還可以享受選擇性下載附件的決定,比如:用戶收到了一封有3個附件的信件, 用戶可以根據自己的需要只下載其中的1個,從而節省了大量的寶貴時間,避免了使用POP3方式收信時必須將郵件全部收到本地後才能進行判斷的被動。
開通imap,需要在郵箱的設置裡面進行設置,各個郵箱大同小異,以sina為例
4. 如何擴展PHP的IMAP模塊
如果對php進行模塊擴展,重新編譯PHP,這個過程比較痛苦,我的方法都是採用編譯模塊為*.so的方式,簡單,方便,不用去其他地方找模塊源碼包,php源碼自帶了。
1、進入安裝目錄
cd /path/ext/imap
/usr/local/webserver/php/bin/phpize
./configure --with-php-config=/usr/local/webserver/php/bin/php-config
就是到這步報錯了,如果你碰到這樣的錯誤:
This c-client library is built with Kerberos support.
Add --with-kerberos to your configure line. Check config.log for details
utf8_mime2text() has new signature
以上2個錯誤都是由於缺少 libc-client-* 軟體包引起,由於我是Centos系統,就直接yum升級吧
yum -y install libc-client-*
安裝完畢後,再次編譯,
./configure --with-php-config=/usr/local/webserver/php/bin/php-config
這次的錯誤不一樣,如下:
configure: error: Kerberos libraries not found.
Check the path given to --with-kerberos (if no path is given, searches in /usr/kerberos, /usr/local and /usr )
既然提示少參數,就加上該參數吧,
./configure --with-php-config=/usr/local/webserver/php/bin/php-config --with-kerberos=/usr
注意:這里有3個路徑可以選擇,於是就一個一個試一下,很幸運的是前面2個都不能編譯通過,只有 --with-kerberos=/usr 可以,但是還是有報錯,如下:
This c-client library is built with SSL support
看來離希望越來越近了,於是就加上 --with-imap-ssl=/usr 參數,終於編譯通過了,真不容易。
最後完整的編譯 imap 模塊參數如下:
./configure --with-php-config=/usr/local/webserver/php/bin/php-config --with-kerberos=/usr --with-imap-ssl=/usr
make
make install
5. 如何開啟imap服務
您好,若需要開啟IMAP服務:1、QQ郵箱開啟IMAP服務點此查看;2、126/163郵箱教程點此查看;3、騰訊郵箱教程點此查看。
6. 郵箱登錄時顯示IMAP服務未開啟,什麼是IMAP服務必須開啟嗎
摘要 您好,IMAP全稱是Internet Mail Access Protocol,即互動式郵件存取協議,它是跟POP3類似郵件訪問標准協議之一。不同的是,開啟了IMAP後,您在電子郵件客戶端收取的郵件仍然保留在伺服器上,同時在客戶端上的操作都會反饋到伺服器上,如:刪除郵件,標記已讀等,伺服器上的郵件也會做相應的動作。所以無論從瀏覽器登錄郵箱或者客戶端軟體登錄郵箱,看到的郵件以及狀態都是一致的。
7. PHP載入IMAP模塊問題
extension_loaded("imap");
用get_extension_funcs("imap"); 函數進行測試,若返回boolean值為空,說明php_imap.dll文件沒有載入進去,
8. 如何用PHP里的IMAP函數,實現郵件的發送,希
//以騰訊企業郵箱做了測試
$mailServer="imap.exmail.qq.com";//IMAP主機
$mailLink="{{$mailServer}:143}INBOX";//imagp連接地址:不同主機地址不同
$mailUser='***';//郵箱用戶名
$mailPass='***';//郵箱密碼
$mbox=imap_open($mailLink,$mailUser,$mailPass);//開啟信箱imap_open
$totalrows=imap_num_msg($mbox);//取得信件數
for($i=1;$i<$totalrows;$i++){
$headers=imap_fetchheader($mbox,$i);//獲取信件標頭
$headArr=matchMailHead($headers);//匹配信件標頭
$mailBody=imap_fetchbody($mbox,$i,1);//獲取信件正文
}
/**
*
*匹配提取信件頭部信息
*@paramString$str
*/
functionmatchMailHead($str){
$headList=array();
$headArr=array(
'from',
'to',
'date',
'subject'
);
foreach($headArras$key){
if(preg_match('/'.$key.':(.*?)[ ]/is',$str,$m)){
$match=trim($m[1]);
$headList[$key]=$key=='date'?date('Y-m-dH:i:s',strtotime($match)):$match;
}
}
return$headList;
}
9. php IMAP讀取郵件信息
<?php
$mbox = imap_open("{imap.gmail.com:993/imap/ssl}INBOX", "[email protected]", "xxxx") or die("can't connect: " . imap_last_error());
$emails = imap_search($mbox,'ALL');
ini_set("max_execution_time",300);
if($emails) {
rsort($emails);
foreach($emails as $email_number) {
$overview = imap_fetch_overview($mbox,$email_number,0);
$pos = explode('@',$overview[0]->from);
$phone = substr($pos[0],-11); // 發件人手機號碼
$struct = imap_fetchstructure($mbox, $email_number);
print_r($struct);
$parts = $struct->parts;
$i = 0;
if (!$parts) { /* Simple message, only 1 piece */
$attachment = array(); /* No attachments */
$content = imap_body($mbox, $email_number);
} else { /* Complicated message, multiple parts */
$endwhile = false; $stack = array(); /* Stack while parsing message */
$content = ""; /* Content of message */
$attachment = array(); /* Attachments */ while (!$endwhile) {
if (!$parts[$i]) {
if (count($stack) > 0) {
$parts = $stack[count($stack)-1]["p"];
$i = $stack[count($stack)-1]["i"] + 1;
array_pop($stack);
} else {
$endwhile = true;
}
}
if (!$endwhile) {
/* Create message part first (example '1.2.3') */
$partstring = "";
foreach ($stack as $s) {
$partstring .= ($s["i"]+1) . ".";
}
$partstring .= ($i+1);
$file_data = imap_fetchbody($mbox, $email_number, $partstring);
$attachment[] = array("filename" =>$parts[$i]->parameters[0]->value,
"filedata" => $file_data
);
if($parts[$i]->subtype == 'JPEG')
{
$file_name = md5(time().rand(5,200)).'.jpg';
file_put_contents($file_name,base64_decode($file_data));
}elseif($parts[$i]->subtype == 'GIF'){
$file_name = md5(time().rand(5,200)).'.gif';
file_put_contents($file_name,base64_decode($file_data));
}elseif($parts[$i]->subtype == 'PLAIN'){
$txt_name = time().rand(5,200).'.txt';
file_put_contents($txt_name,base64_decode($file_data));
}
}
if ($parts[$i]->parts) {
$stack[] = array("p" => $parts, "i" => $i);
$parts = $parts[$i]->parts;
$i = 0;
} else {
$i++;
}
} /* while */
} /* complicated message */
echo "userphone $phone, result:
";
echo "Content: $content
";
echo "Attachments:"; var_mp($attachment);
echo "<br/><br/>---------------------------------------------------------------------<br/><br/>";
}
}
imap_close($mbox);
?>