imap_body()返回空字符串

时间:2018-03-06 11:50:37

标签: php email imap

Hello开发人员,

我目前正在尝试用imap阅读所有电子邮件。我试图用imap_headerinfo获取标题信息,并使用imap_fetchbody获取标题信息。 但是,我得到了所有的headerinfo,但我只得到一个空字符串作为fetchbody函数的返回。我希望有人可以帮助我

$this->inbox = imap_open($server, $user, $password);

    // Header und Body der Email auslesen
    $emails = imap_search($this->inbox,'ALL');
    foreach($emails as $k) {
        $this->email_contents[$k] = array(
            'header' => imap_headerinfo($this->inbox, $k),
            'body' => imap_fetchbody($this->inbox, $k, '1')
        );
    }
    imap_close($this->inbox);

fetch_structure

    object(stdClass)#65 (11) {
  ["type"]=>
  int(1)
  ["encoding"]=>
  int(0)
  ["ifsubtype"]=>
  int(1)
  ["subtype"]=>
  string(5) "MIXED"
  ["ifdescription"]=>
  int(0)
  ["ifid"]=>
  int(0)
  ["ifdisposition"]=>
  int(0)
  ["ifdparameters"]=>
  int(0)
  ["ifparameters"]=>
  int(1)
  ["parameters"]=>
  array(1) {
    [0]=>
    object(stdClass)#66 (2) {
      ["attribute"]=>
      string(8) "boundary"
      ["value"]=>
      string(36) "------------CC4C1146391EA6C129642420"
    }
  }
  ["parts"]=>
  array(2) {
    [0]=>
    object(stdClass)#67 (12) {
      ["type"]=>
      int(0)
      ["encoding"]=>
      int(1)
      ["ifsubtype"]=>
      int(1)
      ["subtype"]=>
      string(5) "PLAIN"
      ["ifdescription"]=>
      int(0)
      ["ifid"]=>
      int(0)
      ["lines"]=>
      int(30)
      ["bytes"]=>
      int(590)
      ["ifdisposition"]=>
      int(0)
      ["ifdparameters"]=>
      int(0)
      ["ifparameters"]=>
      int(1)
      ["parameters"]=>
      array(2) {
        [0]=>
        object(stdClass)#68 (2) {
          ["attribute"]=>
          string(7) "charset"
          ["value"]=>
          string(5) "utf-8"
        }
        [1]=>
        object(stdClass)#69 (2) {
          ["attribute"]=>
          string(6) "format"
          ["value"]=>
          string(6) "flowed"
        }
      }
    }
    [1]=>
    object(stdClass)#70 (13) {
      ["type"]=>
      int(5)
      ["encoding"]=>
      int(3)
      ["ifsubtype"]=>
      int(1)
      ["subtype"]=>
      string(4) "JPEG"
      ["ifdescription"]=>
      int(0)
      ["ifid"]=>
      int(0)
      ["bytes"]=>
      int(1036)
      ["ifdisposition"]=>
      int(1)
      ["disposition"]=>
      string(10) "attachment"
      ["ifdparameters"]=>
      int(1)
      ["dparameters"]=>
      array(1) {
        [0]=>
        object(stdClass)#71 (2) {
          ["attribute"]=>
          string(8) "filename"
          ["value"]=>
          string(11) "logo_sm.jpg"
        }
      }
      ["ifparameters"]=>
      int(1)
      ["parameters"]=>
      array(1) {
        [0]=>
        object(stdClass)#72 (2) {
          ["attribute"]=>
          string(4) "name"
          ["value"]=>
          string(11) "logo_sm.jpg"
        }
      }
    }
  }
}

1 个答案:

答案 0 :(得分:2)

imap-fetchbody()在处理附加的电子邮件时异常工作,并且它的行为不一致。

我打算重新写这篇文章,但说实话,作者atamido非常出色地说明了为什么会这样,所以我谦卑地引用他的主要评论:: http://php.net/manual/en/function.imap-fetchbody.php他还包括一个如何提取身体的例子

  

imap-fetchbody()将解码附加的电子邮件消息   其余的电子邮件部分,但处理时的工作方式   附加的电子邮件与主电子邮件消息不一致。           使用只有文本正文但没有任何mime附件的电子邮件,imap-fetchbody()将返回以下内容   对于每个要求的部件号:

(empty) - Entire message
0 - Message header
1 - Body text
    With an email message that is a multi-part message in MIME format, and contains the message text in plain text and HTML, and has
     

一个file.ext附件,imap-fetchbody()将返回类似的内容   以下是每个要求的部件号:

(empty) - Entire message
0 - Message header
1 - MULTIPART/ALTERNATIVE
1.1 - TEXT/PLAIN
1.2 - TEXT/HTML
2 - file.ext
     

现在,如果您将上述电子邮件附加到包含纯文本和HTML的邮件文本的电子邮件中,则imap_fetchbody()将使用此类型的   零件号系统:

(empty) - Entire message
0 - Message header
1 - MULTIPART/ALTERNATIVE
1.1 - TEXT/PLAIN
1.2 - TEXT/HTML
2 - MESSAGE/RFC822 (entire attached message)
2.0 - Attached message header
2.1 - TEXT/PLAIN
2.2 - TEXT/HTML
2.3 - file.ext
     

请注意,file.ext现在与纯文本和   HTML,并且无法访问MULTIPART / ALTERNATIVE   附上的消息。

     

以下是以前帖子中某些代码的修改版本   这将构建一个包含可访问的易于访问的数组   如果没有多部分,则附加消息部分和消息正文   默剧。 $ structure变量是。的输出   imap_fetchstructure()函数。返回的$ part_array有   字段' part_number'其中包含要直接送入的部件号   进入imap_fetchbody()函数。

<?php
function create_part_array($structure, $prefix="") {
    //print_r($structure);
    if (sizeof($structure->parts) > 0) {    // There some sub parts
        foreach ($structure->parts as $count => $part) {
            add_part_to_array($part, $prefix.($count+1), $part_array);
        }
    }else{    // Email does not have a seperate mime attachment for text
        $part_array[] = array('part_number' => $prefix.'1', 'part_object' => $obj);
    }
   return $part_array;
}
// Sub function for create_part_array(). Only called by create_part_array() and itself. 
function add_part_to_array($obj, $partno, & $part_array) {
    $part_array[] = array('part_number' => $partno, 'part_object' => $obj);
    if ($obj->type == 2) { // Check to see if the part is an attached email message, as in the RFC-822 type
        //print_r($obj);
        if (sizeof($obj->parts) > 0) {    // Check to see if the email has parts
            foreach ($obj->parts as $count => $part) {
                // Iterate here again to compensate for the broken way that imap_fetchbody() handles attachments
                if (sizeof($part->parts) > 0) {
                    foreach ($part->parts as $count2 => $part2) {
                        add_part_to_array($part2, $partno.".".($count2+1), $part_array);
                    }
                }else{    // Attached email does not have a seperate mime attachment for text
                    $part_array[] = array('part_number' => $partno.'.'.($count+1), 'part_object' => $obj);
                }
            }
        }else{    // Not sure if this is possible
            $part_array[] = array('part_number' => $prefix.'.1', 'part_object' => $obj);
        }
    }else{    // If there are more sub-parts, expand them out.
        if (sizeof($obj->parts) > 0) {
            foreach ($obj->parts as $count => $p) {
                add_part_to_array($p, $partno.".".($count+1), $part_array);
            }
        }
    }
}
?>