2015-07-21 142 views
2

我一直在尝试使用imap_fetchbody($stream, $msgno, $option) 取回电子邮件正文,但没有成功。Php Imap抓取电子邮件

然后我试图使用imap_fetchstructure($stream, $msgno),并用自己的亚型喜欢手动解码每个类型: -

1. Alternative 
2. Related 
3. Mixed 

对于前两个我可以像这样

**1. Alternative** 

if ($structure->subtype == 'ALTERNATIVE') { 
    if (isset($structure->parts)) { 
     $body2 = imap_fetchbody($stream, $email_id, 2); 
     if ($body2 == null) { 
      $body2 = imap_fetchbody($stream, $email_id, 1); 
     } 
     $body = quoted_printable_decode($body2); 
    } 
} 

**2. Related** 

if ($structure->subtype == 'RELATED') { 
    if (isset($structure->parts)) { 
     $parts = $structure->parts; 
     $i = 0; 
     $body2 = imap_fetchbody($stream, $email_id, 1.2); 
     if ($body2 == null) { 
      $body2 = imap_fetchbody($stream, $email_id, 1); 
     } 
     $body = quoted_printable_decode($body2); 
     foreach ($parts as $part) { 
      if ($parts[$i]) { 
      } 
      $i++; 
      if (isset($parts[$i])) { 
       if ($parts[$i]->ifid == 1) { 
        $id = $parts[$i]->id; 
        $imageid = substr($id, 1, -1); 
        $imageid = "cid:" . $imageid; 
        if ($parts[$i]->ifdparameters == 1) { 
         foreach ($parts[$i]->dparameters as $object) { 
          if (strtolower($object->attribute) == 'filename') { 
           $filename = $object->value; 
          } 
         } 
        } 
        if ($parts[$i]->ifparameters == 1) { 
         foreach ($parts[$i]->parameters as $object) { 
          if (strtolower($object->attribute) == 'name') { 
           $name = $object->value; 
          } 
         } 
        } 
        $body = str_replace($imageid, $filename, $body); 
       } 
      } 
     } 
    } 
} 

解码但是,当涉及或者我不知道或者我想做什么或者我在哪里犯错。

**3. mixed** 

if ($structure->subtype == 'MIXED') { 
    if (isset($structure->parts)) { 
     $parts = $structure->parts; 
      // subtype = ALTERNATIVE 
      if ($parts[0]->subtype == 'ALTERNATIVE') { 
       if (isset($structure->parts)) {                   
        $body2 = imap_fetchbody($stream, $email_id, 1.2); 
        if ($body2 == null) { 
         $body2 = imap_fetchbody($stream, $email_id, 1); 
        } 
        $body = quoted_printable_decode($body2); 
       } 
      } 
      // subtype = RELATED 
      if ($parts[0]->subtype == 'RELATED') { 
       if (isset($parts[0]->parts)) { 
        $parts = $parts[0]->parts; 
        $i = 0; 
        $body2 = imap_fetchbody($stream, $email_id, 1.1); 
        if ($body2 == null) { 
         $body2 = imap_fetchbody($stream, $email_id, 1); 
        } 
        $body = quoted_printable_decode($body2); 
        $name = ""; 
        foreach ($parts as $part) { 
         if ($parts[0]) { 
         } 
         $i++; 
         if (isset($parts[$i])) { 
          if ($parts[$i]->ifid == 1) { 
           $id = $parts[$i]->id; 
           $imageid = substr($id, 1, -1); 
           $imageid = "cid:" . $imageid; 
           if ($parts[$i]->ifdparameters == 1) { 
            foreach ($parts[$i]->dparameters as $object) { 
             if (strtolower($object->attribute) == 'filename') { 
              $filename = $object->value; 
             } 
            } 
           } 
           if ($parts[$i]->ifparameters == 1) { 
            foreach ($parts[$i]->parameters as $object) { 
             if (strtolower($object->attribute) == 'name') {                     
              $name = $object->value; 
             } 
            } 
           } 
          } 
          $body = str_replace($imageid, $name, $body); 
         } 
        } 
       } 
      } 
     } 
    } 
+0

我可以得到这个答案。 –

回答

0

您忘记了PLAIN的子类型。你也必须解码它

if ($parts[0]->subtype == 'PLAIN') { 
      if (isset($structure->parts)) {           
        $message2 = imap_fetchbody($stream, $email_id, 1); 
        $message = quoted_printable_decode(base64_decode($message2)); 
      } 
    }