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

时间:2013-06-30 01:47:01

标签: php imap email-attachments

我很难确定如何一致地引用部件号(部件号如2.0,2.1,2.2等仅指简单/文本消息,图像/ jpeg等的数据)。下面我正在使用'1.'.$i'2.'.$j来尝试引用给定消息中的对象。虽然我错过了一些东西,但我觉得我很接近...

  

$ mbox =   imap_open( '{mail.example.com:143/novalidate-cert}INBOX', '有人+ example.com', '密码');

     

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

     

$ i = 0; $ j = 0;

     

foreach($ structure->部分为$ k1 => $ d1){//下一行,我们如何   参考正确的部分,例如“ '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>';
}}}}

此外,我对使用任何框架都不感兴趣,我想弄清楚如何自己做这个。当被要求进行相关澄清时,我会很乐意改进我的问题。

1 个答案:

答案 0 :(得分:-1)

在将电子邮件迁移到数据库之前,我将所有内容附加到$email数组。

以下是电子邮件各个部分的迭代工作原理。这里可能有一些较旧的位,我不再使用,虽然这很好用。缺少函数是我在将不同部分写入数据库之前使用它们的原因。

   $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;
    }
   }