PHP实现邮件接收,php 邮件接收代码,接收邮件处理主题和内容

$server = "{bjmail.*.com/pop3}"; //邮件服务器

$mailbox = "inbox"; //收件箱

$mailaccount="zhao**";//用户名

$mailpasswd=" "; //密码

$stream = @imap_open($server.$mailbox,$mailaccount,$mailpasswd);//打开IMAP 连结

$mail_number = imap_num_msg($stream);//信件的个数

if($mail_number < 1) { echo "No Message for $email"; }//如果信件数为0,显示信息

for($i=$mail_number;$i>=$mail_number;$i--)

{

$headers = @imap_header($stream, $i);

$mail_header= imap_headerinfo($stream, $i);//邮件头部

//var_dump ($mail_header);

$subject = $mail_header->subject;//邮件标题

$subject=decode_mime($subject);

echo $subject;

//编码为简体中文的标题的处理方法

// if(stristr($subject, "=?gb2312"))

// { //编码为简体中文的标题

// $subject=substr($subject,11);

// $subject=substr($subject,0,-2);

// $subject = base64_decode($subject);

// }

echo $from = $mail_header->fromaddress;//发件人

echo $date = $mail_header->date;//日期

$body = imap_fetchbody($stream, $i, 1);

$body = imap_base64($body);

$body = nl2br($body);

echo $body;

// $body = imap_qprint($body);

// echo $body;

// $body = imap_binary($body);

// $body = imap_base64($body);

//echo $body;

}

function decode_mime($string)

{

$pos = strpos($string,'=?');

if (!is_int($pos)) {

return $string;

}

$preceding = substr($string, 0, $pos); // save any preceding text

$search = substr($string, $pos+2); /* the mime header spec says this is the longest a single encoded Word can be */

$d1 = strpos($search, '?');

if (!is_int($d1)) {

return $string;

}

$charset = substr($string, $pos+2, $d1); //取出字符集的定义部分

$search = substr($search, $d1+1); //字符集定义以后的部分=>$search;

$d2 = strpos($search, '?');

if (!is_int($d2)) {

return $string;

}

$encoding = substr($search, 0, $d2); 两个? 之间的部分编码方式 :q 或 b

$search = substr($search, $d2+1);

$end = strpos($search, '?='); //$d2+1 与 $end 之间是编码了 的内容:=> $endcoded_text;

if (!is_int($end)) {

return $string;

}

$encoded_text = substr($search, 0, $end);

$rest = substr($string, (strlen($preceding . $charset . $encoding . $encoded_text)+6)); //+6 是前面去掉的 =????= 六个字符

switch ($encoding) {

case 'Q':

case 'q':

//$encoded_text = str_replace('_', '%20', $encoded_text);

//$encoded_text = str_replace('=', '%', $encoded_text);

//$decoded = urldecode($encoded_text);

$decoded=quoted_printable_decode($encoded_text);

if (strtolower($charset) == 'windows-1251') {

$decoded = convert_cyr_string($decoded, 'w', 'k');

}

break;

case 'B':

case 'b':

$decoded = base64_decode($encoded_text);

if (strtolower($charset) == 'windows-1251') {

$decoded = convert_cyr_string($decoded, 'w', 'k');

}

break;

default:

$decoded = '=?' . $charset . '?' . $encoding . '?' . $encoded_text . '?=';

break;

}

return $preceding . $decoded .decode_mime($rest);

//return $preceding . $decoded . $this->decode_mime($rest);

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
receivemail是一个专门用来接收邮件PHP类,支持POP3和IMAP等邮件协议。 可以接收邮件邮件附件。 include("receivemail.class.php"); 02 // 创建一个 reciveMail 对象 03 $obj = new receiveMail('abc@abc.com','xxxxxx','abc@abc.com','pop.abc.com','pop3','110',false); 04 // 连接到邮件服务器 05 $obj->connect(); //If connection fails give error message and exit 06 // 读取未读邮件数 07 $tot = $obj->getTotalMails(); //Total Mails in Inbox Return integer value 08 09 echo "收到$tot封邮件::"; 10 for($i = $tot; $i > 0; $i--) 11 { 12 $head = $obj->getHeaders($i); // 读取获取邮件头信息,返回数组 **数组键值为 (subject,to,toOth,toNameOth,from,fromName) 13 echo "主题 :: ".$head['subject'].""; 14 echo "收件人 :: ".$head['to'].""; 15 echo "抄送 :: ".$head['toOth'].""; 16 echo "发件人 :: ".$head['from'].""; 17 echo "发件人名称 :: ".$head['fromName'].""; 18 echo ""; 19 echo "*******************************************************************************************"; 20 echo $obj->getBody($i); // 邮件正文 21 $str = $obj->GetAttach($i,"./"); // 获取邮件附件,返回的文件名以逗号隔开。 例如. (mailid, Path to store file) 22 $ar = explode(",",$str); 23 foreach($ar as $key=>$value) 24 echo ($value == "") ? "" : "Atteched File :: " . $value . ""; 25 echo "------------------------------------------------------------------------------------------"; 26 //$obj->deleteMails($i); // Delete Mail from Mail box 27 } 28 $obj->close_mailbox(); //Close Mail Box
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值