douyuan1049 2013-06-30 01:47
浏览 174
已采纳

获取PHP imap_fetchbody正确的部件号/索引正文部件列表

I'm having difficulty trying to determine how to consistently reference part numbers (a part number such as 2.0, 2.1, 2.2 etc would refer to just the data for a plain/text message, an image/jpeg, etc). Below I'm using '1.'.$i and '2.'.$j in an attempt to reference objects in a given message. I feel like I'm close though missing something...

$mbox = imap_open('{mail.example.com:143/novalidate-cert}INBOX','someone+example.com','password');

if ($mbox) { $structure = imap_fetchstructure($mbox,$message_id);

$i = 0; $j = 0;

foreach ($structure->parts as $k1 => $d1) {//Next line, how do we refer to the part correct, e.g. "'1.'.$i"?

if (isset($d1->parts))
{
 foreach ($d1->parts as $j2 => $d2)
 {//Next line, how do we refer to the part correct, e.g. "'2.'.$j"?
  $a = print_r(imap_fetchbody($mbox,$cms->page2,'2.'.$j),1);
  echo '<div><img alt="" src="data:image/jpeg;base64,'.$a.'" /></div>';
}}}}

Also I am not interested in using any frameworks, I want to figure out how to do this myself. I'll be happy to refine my question when asked for relevant clarifications.

  • 写回答

1条回答 默认 最新

  • dongzhukuai8177 2013-08-29 21:02
    关注

    Before migrating emails to the database I append everything to an $email array.

    Here is how the iteration over various parts of the email works. There MAY be some older bits in here that I no longer use though this works just fine. Missing functions are what I use to prepare different parts before they are written to the database.

       $message = imap_body($mbox,$i);
       $structure = imap_fetchstructure($mbox,$i);
    
       $f = array('!');
       $r = array('');
       $message = str_ireplace($f,$r,$message);
       $find = array('=00');
       $replace = array('');
       $message = str_ireplace($find,$replace,$message);
       $message = mb_convert_encoding($message,'us-ascii','UTF-8');
       $message = utf8_encode($message);
    
       $dontattach = array('alternative','html','plain','related');
       $ii = 1;
       $j = 1;
       $k = 1;
       $fallback = 0;
    
       if (isset($structure->parts))
       {
        foreach ($structure->parts as $k1 => $d1)
        {
         if ($d1->subtype=='GIF')
         {
          if (!isset($email['attachments'])) {$email['attachments'] = array();}
          if (isset($d1->id)) {$id = $d1->id;} else {$id = '';}
          $a = mail_service_attachment($mbox,$i,$ii,$j,$d1->parameters[0]->value,'image/gif',$id);
          array_push($email['attachments'],$a);
         }
         else if ($d1->subtype=='HTML')
         {
          $a = mail_service_html($mbox,$i,$d1->encoding,$d1->parameters[0]->value,$ii,$j);
          if (isset($a['css'])) {$email['css'] = $a['css'];}
          $email['body_xml'] = $a['xml'];
          $fallback++;
         }
         else if ($d1->subtype=='JPEG')
         {
          if (!isset($email['attachments'])) {$email['attachments'] = array();}
          if (isset($d1->id)) {$id = $d1->id;} else {$id = '';}
          $a = mail_service_attachment($mbox,$i,$ii,$j,$d1->parameters[0]->value,'image/jpeg',$id);
          array_push($email['attachments'],$a);
         }
         else if ($d1->subtype=='PLAIN')
         {
          $a = mail_service_plain($mbox,$i,$d1->encoding,$d1->parameters[0]->value,$ii,$j,$k);
          $email['body_clean'] = $a['xml'];
          $email['body_text'] = $a['plain'];
          $fallback++;
         }
         else if ($d1->subtype=='PNG')
         {
          if (!isset($email['attachments'])) {$email['attachments'] = array();}
          if (isset($d1->id)) {$id = $d1->id;} else {$id = '';}
          $a = mail_service_attachment($mbox,$i,$ii,$j,$d1->parameters[0]->value,'image/png',$id);
          array_push($email['attachments'],$a);
         }
         else if (!in_array(strtolower($d1->subtype),$dontattach))
         {
          if (!isset($email['attachments'])) {$email['attachments'] = array();}
          if (isset($d1->id)) {$id = $d1->id;} else {$id = '';}
          $a = mail_service_attachment($mbox,$i,$ii,$j,$d1->parameters[0]->value,strtolower($d1->subtype),$id);
          array_push($email['attachments'],$a);
         }
    
         if (isset($d1->parts))
         {
          foreach ($d1->parts as $j2 => $d2)
          {
           if ($d2->subtype=='GIF')
           {
            if (!isset($email['attachments'])) {$email['attachments'] = array();}
            if (isset($d2->id)) {$id = $d2->id;} else {$id = '';}
            $a = mail_service_attachment($mbox,$i,$ii,$j,$d2->parameters[0]->value,'image/gif',$id);
            array_push($email['attachments'],$a);
           }
           else if ($d2->subtype=='HTML')
           {
            $a = mail_service_html($mbox,$i,$d2->encoding,$d2->parameters[0]->value,$ii,$j);
            if (isset($a['css'])) {$email['css'] = $a['css'];}
            $email['body_xml'] = $a['xml'];
            $fallback++;
           }
           else if ($d2->subtype=='JPEG')
           {
            if (!isset($email['attachments'])) {$email['attachments'] = array();}
            if (isset($d2->id)) {$id = $d2->id;} else {$id = '';}
            $a = mail_service_attachment($mbox,$i,$ii,$j,$d2->parameters[0]->value,'image/jpeg',$id);
            array_push($email['attachments'],$a);
           }
           else if ($d2->subtype=='PLAIN')
           {
            $a = mail_service_plain($mbox,$i,$d2->encoding,$d2->parameters[0]->value,$ii,$j,$k);
            $email['body_clean'] = $a['xml'];
            $email['body_text'] = $a['plain'];
            $fallback++;
           }
           else if ($d2->subtype=='PNG')
           {
            if (!isset($email['attachments'])) {$email['attachments'] = array();}
            if (isset($d2->id)) {$id = $d2->id;} else {$id = '';}
            $a = mail_service_attachment($mbox,$i,$ii,$j,$d2->parameters[0]->value,'image/png',$id);
            array_push($email['attachments'],$a);
           }
           else if (!in_array(strtolower($d2->subtype),$dontattach))
           {
            if (!isset($email['attachments'])) {$email['attachments'] = array();}
            if (isset($d2->id)) {$id = $d2->id;} else {$id = '';}
            $a = mail_service_attachment($mbox,$i,$ii,$j,$d2->parameters[0]->value,strtolower($d2->subtype),$id);
            array_push($email['attachments'],$a);
           }
           $j++;
           $k = 1;
          }
         }
    
         $ii++;
         $j = 1;
         $k = 1;
        }
       }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在sql server里完成筛选
  • ¥15 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了