很多站长朋友们都不太清楚phpforimap,今天小编就来给大家整理phpforimap,希望对各位有所帮助,具体内容如下:
本文目录一览: 1、 如何扩展PHP的IMAP模块 2、 如何用PHP里的IMAP函数,实现邮件的发送,希 3、 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);
?>
关于phpforimap的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。