Extracting PHP IMAP attachments

蹲街弑〆低调 提交于 2019-12-25 05:06: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,


回答1:


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,




回答2:


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.



来源:https://stackoverflow.com/questions/11773169/extracting-php-imap-attachments

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!