imap_body()返回空字符串

问题描述 投票:0回答:1

你好开发者,

我目前正在尝试用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"
        }
      }
    }
  }
}
php email imap
1个回答
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。

以下是以前帖子中某些代码的修改版本,这些代码将构建一个易于访问的数组,其中包括可访问的附加消息部分和消息正文(如果没有多部分mime)。 $ 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);
            }
        }
    }
}
?>
© www.soinside.com 2019 - 2024. All rights reserved.