Extracting PHP IMAP attachments

2019-08-16 07:24发布

Hello I have a PHP IMAP functions which extract the attachments of a specific body of an email, I found out this article : http://www.linuxscope.net/articles/mailAttachmentsPHP.html but it shows an error : Warning: imap_bodystruct() [function.imap-bodystruct]: Bad message number I dont know what I am missing. Here is my code

session_start();

include('settings.php');

include('vars.php');

$struct = imap_fetchstructure($mbox,$getmsgid, FT_UID);

$contentParts = count($struct->parts);

if ( $contentParts >= 2 ) {

    for ( $ii=2; $ii<=$contentParts; $ii++ ) {

        $att[$ii-2] = imap_bodystruct($mbox,$getmsgid, $ii);

        echo $ii . '<br />';
    }

for ($k=0;$k<sizeof($att);$k++) {

    if ($att[$k]->parameters[0]->value == "us-ascii" || $att[$k]->parameters[0]->value  == "US-ASCII") {

        if ($att[$k]->parameters[1]->value != "") {

            $selectBoxDisplay[$k] = $att[$k]->parameters[1]->value;

        }

    }elseif ($att[$k]->parameters[0]->value != "iso-8859-1" && $att[$k]->parameters[0]->value != "ISO-8859-1") {

        $selectBoxDisplay[$k] = $att[$k]->parameters[0]->value;

        }
    }

   }

   if (sizeof($selectBoxDisplay) > 0) {

echo "<select name=\"attachments\" size=\"3\" class=\"tblContent\"    onChange=\"handleFile(this.value)\" style=\"width:170;\">";

    for ($j=0;$j<sizeof($selectBoxDisplay);$j++) {

        echo "\n<option value=\"$j\">". $selectBoxDisplay[$j]    ."</option>";

    }

echo "</select>";

  }

the settings.php contains my $mbox connection it works fine, the only problem here is the imap_bodystruct($mbox,$getmsgid, $ii); is there any problem with my syntax there?

Thanks you,

标签: php imap
2条回答
2楼-- · 2019-08-16 07:56

Here you're fetching the message by UID.

$struct = imap_fetchstructure($mbox,$getmsgid, FT_UID);

Presumably when you fetch the body parts, you'll need to do it by UID as well:

$att[$ii-2] = imap_fetchbody ($mbox, $getmsgid, $ii, FT_UID)

This should fetch the body part by UID and part number.

Your existing call $att[$ii-2] = imap_bodystruct($mbox,$getmsgid, $ii);, will attempt to fetch the message by message sequence number, which is not the same as UID. This function (and I'm unclear as to what it does) does not appear to have an option to fetch by UID.

Also keep in mind that for deeply complex MIME messages, the parts are not necessarily sequential (like: 1, 2, 3); they can have sub-parts: 1.1, 1.2, 2, 3. This is common if you have you an email with both HTML and plain text, and have an attachment.

查看更多
我只想做你的唯一
3楼-- · 2019-08-16 08:03

I found the answer :

replace the line from :

   $att[$ii-2] = imap_bodystruct($mbox,$getmsgid, $ii);

to :

   $att[$ii-2] = imap_bodystruct($mbox, imap_msgno($mbox, $getmsgid), $ii);

this display the attachments, replace this line :

  if (sizeof($selectBoxDisplay) > 0) {

  echo "<select name=\"attachments\" size=\"3\" class=\"tblContent\"    onChange=\"handleFile(this.value)\" style=\"width:170;\">";

   for ($j=0;$j<sizeof($selectBoxDisplay);$j++) {

    echo "\n<option value=\"$j\">". $selectBoxDisplay[$j]    ."</option>";

   }

   echo "</select>";

  }

to :

  foreach($selectBoxDisplay as $attachments => $attVal){
     echo $attVal . '<br />';
  }

Thanks,

查看更多
登录 后发表回答