本文目錄一覽:
- 1、如何擴展PHP的IMAP模塊
- 2、如何用PHP里的IMAP函數,實現郵件的發送,希
- 3、php IMAP讀取郵件信息
- 4、PHP的IMAP交換問題問題,怎麼解決
- 5、PHP載入IMAP模塊問題
- 6、php編譯安裝後如何安裝IMAP擴展
如何擴展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
如何用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); //獲取信件正文
}
/**
*
* 匹配提取信件頭部信息
* @param String $str
*/
function matchMailHead($str){
$headList = array();
$headArr = array(
‘from’,
‘to’,
‘date’,
‘subject’
);
foreach ($headArr as $key){
if(preg_match(‘/’.$key.’:(.*?)[\n\r]/is’, $str,$m)){
$match = trim($m[1]);
$headList[$key] = $key==’date’?date(‘Y-m-d H:i:s’,strtotime($match)):$match;
}
}
return $headList;
}
php IMAP讀取郵件信息
?php
$mbox = imap_open(“{imap.gmail.com:993/imap/ssl}INBOX”, “xxxx@gmail.com”, “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_dump($attachment);
echo “br/br/———————————————————————br/br/”;
}
}
imap_close($mbox);
?
PHP的IMAP交換問題問題,怎麼解決
用get_extension_funcs(“imap”); 函數進行測試,若返回boolean值為空,說明php_imap.dll文件沒有載入進去!
PHP載入IMAP模塊問題
extension_loaded(“imap”);
用get_extension_funcs(“imap”); 函數進行測試,若返回boolean值為空,說明php_imap.dll文件沒有載入進去,
php編譯安裝後如何安裝IMAP擴展
今天程序員在最近實施的項目中需要增加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 服務搞定!
註:上面的路徑是本台伺服器的相關路徑,其它的伺服器需要根據您的實際情況進行更改,理解整個部署的思路即可哈!
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/206808.html