今天程序员在最近实施的项目中需要增加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);
?>