Просмотр файла magmail/imap6.php

Размер файла: 3.08Kb
<?
function mime_decode($s) {
  $elements = imap_mime_header_decode($s);
  $decoded = "";
  for($i = 0;$i < count($elements);$i++) {
    $charset = $elements[$i]->charset;
    $text =$elements[$i]->text;
    if(!strcasecmp($charset, "utf-8") ||
       !strcasecmp($charset, "utf-7"))
    {
      $text = iconv($charset, "windows-1251", $text);
    }
    else
    {
      $text = convert_cyr_string($text,"k","w");
    }
    $decoded = $decoded . $text;
  }
  return $decoded;
}

function get_mime_type(&$structure)
{
$primary_mime_type = array("TEXT", "MULTIPART", "MESSAGE", "APPLICATION", "AUDIO", "IMAGE", "VIDEO", "OTHER");
if($structure->subtype)
 {
 return $structure->subtype;
 }
return "PLAIN";
}

function get_part($stream, $msg_number, $mime_type, $structure = false, $part_number = false)
{
if(!$structure)
 {
 $structure = imap_fetchstructure($stream, $msg_number);
 }
if($structure)
 {
 if($mime_type == get_mime_type($structure))
  {
  if(!$part_number)
   {
   $part_number = "1";
   }
  $text = imap_fetchbody($stream, $msg_number, $part_number);
  if($structure->encoding == 3)
   {
   return imap_base64($text);
   }
  else if($structure->encoding == 4)
   {
   return imap_qprint($text);
   }
  else
   {
   return $text;
   }
  }
 if($structure->type == 1) /* multipart */
  {
  while(list($index, $sub_structure) = each($structure->parts))
   {
   if($part_number)
    {
    $prefix = $part_number . '.';
    }
   $data = get_part($stream, $msg_number, $mime_type, $sub_structure, $prefix . ($index + 1));
   if($data)
    {
    return $data;
    }
   }
  }
 }
return false;
}



//require("functionChat.ssp");
$maxlen = 5000;
$LocalHost = (strpos(" ".$GLOBALS['DOCUMENT_ROOT'],'d:/web')>0);


if ($LocalHost) $Host="{mail.valuehost.ru:143}INBOX";
else $Host="{mail.valuehost.ru:143/imap/notls}";
$Login="[email protected]";
$Password="";

$mbox = imap_open ($Host, $Login, $Password) or die ("can't connect: " . imap_last_error());

//imap_expunge ($mbox);

$num = imap_num_msg($mbox);
echo "Messages in mailbox: $num<br>\n";

for( $i=1; $i<=$num; $i++ ) {

  $head = imap_header($mbox,$i);
  $date = $head->date;
  $from = mime_decode($head->fromaddress);
  $subject = mime_decode($head->subject);


  $frombox = $head->from[0]->mailbox;
  $fromhost = $head->from[0]->host;
  $tobox = $head->to[0]->mailbox;
  $tohost = $head->to[0]->host;


  if (strtolower($tobox) == 'testbest')
  {
    // get plain text
     echo "subject=$subject";
     $textdata = get_part($mbox, $i, "PLAIN");
     echo "<br>text=".$textdata;
    // get HTML text

     $imagedata = get_part($mbox, $i, "JPEG");
     $imagedata = "test123";


     $image = "somefile.dat";
     $imagedata = $image;

     $f = fopen($image,"a+");
     ftruncate($f,0);
 //    $f = fopen($image,"w+");
     fwrite($f,$imagedata);
     fclose($f);
//     echo "image=$imagedata";
     echo "<img src=\"$image\"> ";
 //    echo "<br>html=".$htmldata;
 //   imap_delete($mbox,$i);
  }
}


//imap_expunge ($mbox);
imap_close ($mbox);
?>