View file magmail/imap8.php

File size: 4.1Kb
<?
$LocalHost = (strpos(" ".$GLOBALS['DOCUMENT_ROOT'],'d:/web')>0);
if ($LocalHost) $libpath='c:/Program Files/ImageMagick-5.5.7-Q8/';
else $libpath='/usr/local/bin/';


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)
{
global $anysubtype;
$primary_mime_type = array("TEXT", "MULTIPART", "MESSAGE", "APPLICATION", "AUDIO", "IMAGE", "VIDEO", "OTHER");
if($structure->subtype)
{
  if ($anysubtype) $subtype = "*";
  else $subtype = $structure->subtype;
  return $primary_mime_type[(int) $structure->type] . "/$subtype";
}
return "TEXT/PLAIN";
}

function get_part($stream, $msg_number, $mime_type, $structure = false, $part_number = false)
{
global $anysubtype;
$anysubtype = (strpos($mime_type,"/*") != 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);

$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')
  {
    $pass = "testbest";
    $login = "testbest";
 //   if (trim($subject) == "foto=$pass" or  trim($subject) == "FOTO=$pass")


    $dp = opendir("foto");

    $tmpfn = "/foto/".$login.".tmp";

    $fn = "/foto/".$login.".jpg";

    echo "$tmpfn:$fn";


    // get HTML text

     $imagedata = get_part($mbox, $i, "IMAGE/*");
     if ($imagedata != "")
     {
       echo "subject=$subject";
       $textdata = get_part($mbox, $i, "TEXT/PLAIN");
       echo "<br>text=".$textdata;


       $f = fopen($tmpfn,"w+");
 //      ftruncate($f,0);
       fwrite($f,$imagedata);
       fclose($f);

   //    $f = fopen($fn,"a+");
   //    ftruncate($f,0);
   //    fwrite($f,$imagedata);
   //    fclose($f);
       $width = 128;
       $cmd=$libpath."convert  -geometry \"".$width."x>\" $tmpfn $fn";
       exec($cmd);
   //    unlink($tmpfn);
     }
//     $imagedata = "test123";


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


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