【问题标题】:Can't fetch plain text mails content using IMAP无法使用 IMAP 获取纯文本邮件内容
【发布时间】:2012-06-07 05:52:48
【问题描述】:

我正在使用IMAP 函数从特定邮件ID 读取邮件。但我无法读取plain text 邮件的邮件内容。它非常适合HTML Mails。运行代码后,所有plain text 邮件仍保持为未读状态,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,$email_number,0);
                 $msg = imap_fetchbody($inbox,$email_number,1.2); 

                 $message='';
                if($msg=='')
                {
                    $message = imap_fetchbody($inbox,$email_number,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);
               } 

任何人请帮助我...提前谢谢

【问题讨论】:

    标签: php email imap


    【解决方案1】:

    你的问题是这一行:

    $msg = imap_fetchbody($inbox,$email_number,1.2); 
    

    1.2为TEXT/HTML邮件部分,用于html邮件正文。

    1.1 是 TEXT/PLAIN - 纯文本电子邮件正文 - 对于纯文本消息,您需要使用这个。

    【讨论】:

    • 嗨,我使用第三个参数值作为 1.2,但是使用这个值我得到了空的正文内容。您能否分享一下为什么我的正文内容为空的解决方案?
    • 是否有要获取的 html 正文?也许电子邮件只是纯文本
    • 是的,正文内容是纯文本,可能是 html。这将取决于回复者。在搜索时,1.2 用于两种类型的内容。但它实际上不起作用。
    猜你喜欢
    • 2011-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-04
    • 1970-01-01
    • 2015-07-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多