加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 无法使用IMAP获取纯文本邮件内容

发布时间:2020-12-13 17:19:38 所属栏目:PHP教程 来源:网络整理
导读:我正在使用IMAP功能从特定邮件ID中读取邮件.但我无法读取纯文本邮件的邮件内容.它适用于 HTML Mails.运行代码后,所有纯文本邮件仍然是未读的,html邮件标记为已读,其他内容如发件人邮件ID和主题我可以阅读.只有阅读内容的问题.这是我尝试过的代码 include('im
我正在使用IMAP功能从特定邮件ID中读取邮件.但我无法读取纯文本邮件的邮件内容.它适用于 HTML Mails.运行代码后,所有纯文本邮件仍然是未读的,html邮件标记为已读,其他内容如发件人邮件ID和主题我可以阅读.只有阅读内容的问题.这是我尝试过的代码

include('imap.php');
    $hostname = '{xxx.org:143/novalidate-cert}INBOX';
    $username = 'xxx-xxx@xx.org';
    $password = 'xxxxx';
    $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to : ' . imap_last_error());
    $emails = imap_search($inbox,'UNSEEN');

    if($emails) {
      $output = '';
      rsort($emails);
      foreach($emails as $email_number) {
        $structure = imap_fetchstructure($inbox,$email_number); 
            $savedir = dirname(__FILE__).'/uploads/';
                    $attachments = array();
                    if(isset($structure->parts) && count($structure->parts)) {

                        for($i = 0; $i < count($structure->parts); $i++) {

                            $attachments[$i] = array(
                                'is_attachment' => false,'filename' => '','name' => '','attachment' => ''
                            );
                            if($structure->parts[$i]->ifdparameters) {
                                foreach($structure->parts[$i]->dparameters as $object) {
                                    if(strtolower($object->attribute) == 'filename') {
                                        $attachments[$i]['is_attachment'] = true;
                                        $attachments[$i]['filename'] = $object->value;
                                    }
                                }
                            }

                            if($structure->parts[$i]->ifparameters) {
                                foreach($structure->parts[$i]->parameters as $object) {
                                    if(strtolower($object->attribute) == 'name') {
                                        $attachments[$i]['is_attachment'] = true;
                                        $attachments[$i]['name'] = $object->value;
                                    }
                                }
                            }

                            if($attachments[$i]['is_attachment']) {
                                $attachments[$i]['attachment'] = imap_fetchbody($inbox,$email_number,$i+1);
                                if($structure->parts[$i]->encoding == 3) { // 3 = BASE64
                                    $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
                                }
                                elseif($structure->parts[$i]->encoding == 4) { // 4 = QUOTED-PRINTABLE
                                    $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);



                                }

                                 $savepath = $savedir . $attachments[$i]['filename'];
                                file_put_contents($savepath,$attachments[$i]['attachment']);

                            }
                        }
                    }





                $name = $structure->parts[1]->dparameters[0]->value; 
                $overview = imap_fetch_overview($inbox,0);
                 $msg = imap_fetchbody($inbox,1.2); 

                 $message='';
                if($msg=='')
                {
                    $message = imap_fetchbody($inbox,2.0);


                }else{
                 $message=$msg;

                 }


                 $sub=$overview[0]->subject;
                  $from=$overview[0]->from;
                  $arr = explode('<',$from);
                    $from_mail = $arr[1];
                    if($from_mail!='')
                    {
                        $from=str_replace('>','',$from_mail);
                    }


               $randstr='';
               srand((double)microtime()*1000000);

               $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
               while(strlen($randstr)<5) {
                  $randstr.=substr($chars,(rand()%(strlen($chars))),1);
               }

请任何人帮助我……提前致谢

解决方法

你的问题是这一行:

$msg = imap_fetchbody($inbox,1.2);

1.2是TEXT / HTML电子邮件部分,用于html电子邮件正文.

1.1是TEXT / PLAIN – 纯文本邮件正文 – 对于纯文本邮件,您需要使用此邮件.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读