Re: Imap and attachments

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



here is a script I use to get jpg attachments with IMAP.. good luck

<?php
//------+-- -------------------+------ --  -
# view the contents of an array in html format
#---  -+    -

// return the email account of the user so we can assign it to a user
account
function get_phone_address($i)
{
 global $mbox;
 $header=imap_header($mbox,$i);
 echo 'FROM: '.$header->from[0]->mailbox."@".$header->from[0]->host.'<BR>';
 return $header->from[0]->mailbox."@".$header->from[0]->host;

}

function get_structure($mbox,$i)
{
$structure = imap_fetchstructure($mbox, $i);
return $structure;
}

function no_attachments($structure)
{
global $mbox;
$attach = sizeof($structure->parts); //# OF ATTACHMENTS
echo '<BR><BR>'.$attach.' attachments <BR>';
return $attach;
}

function get_attached_file($structure, $username, $k,$i)
{
global $mbox;
$encoding = $structure->parts[$k]->encoding;
$Name = strtolower($structure->parts[$k]->dparameters[0]->value);
//$NewFile[$k-1] = $Name;
echo $Name.'<BR>';
if(strstr($Name, '.jpg') && !strstr($Name, 'masthead.jpg'))
{
 echo 'In File Loop<BR>';
 $File = base64_decode(imap_fetchbody($mbox, $i, $k+1));

 $fp = fopen("C:/TEMP/".$username.$Name ,"w+");
 fwrite ($fp, $File, 100000000);
 fclose($fp);
}
 return $Name;
}
/*
****************************************************************************
**********************************
****************************************************************************
**********************************
*/
// CONNECT TO IMAP SERVER
$mbox = imap_open("{mail.myserver.net:143}INBOX", "USERNAME", "PASSWORD");

if($mbox)
{
 echo 'CONNECTED TO INBOX...<BR>';
}

$error = imap_errors();
$alerts = imap_alerts();
/*
****************************************************************************
**********************************
****************************************************************************
**********************************
*/

$msg_cnt = imap_num_msg($mbox);
htm($msg_cnt);

for($msg_num=1; $msg_num<=$msg_cnt; $msg_num++)
{
 $structure = get_structure($mbox, $msg_num);

 $num_attach = no_attachments($structure);

 $phone_address = get_phone_address($msg_num);
 //get_attached_file($structure,1,1);

 for($i=0; $i<$num_attach; $i++)
 {
  get_attached_file($structure, 'jim_', $i, $msg_num);
 }


}





imap_close($mbox);

print_r($error);
print_r($alerts);

?>









"Evert | Rooftop Solutions" <evert@xxxxxxxxxxxxxxxxxxx> wrote in message
news:4230B04B.9060100@xxxxxxxxxxxxxxxxxxxxxx
> Hi,
>
> I started using the IMAP functions and ran into a problem with some
> headers for attachments.
>
> The first one is:
>
> Content-type: image/psd; x-unix-mode=0644; name=FinalVersion6.psd
> Content-transfer-encoding: base64
> Content-disposition: inline; filename=FinalVersion6.psd
>
> Generated by Mac Mail. The filename wont show up in the result of for
example imap_fetchstructure.
>
> The second is:
>
> Content-Type: text/plain
> Content-Disposition: attachment;
> filename="Video(019).3gp"
> Content-Transfer-Encoding: base64
>
> I suspect this one has something to do with the linebreak. It's generated
by a modern nokia (7650 I think)
>
>
> I additionaly found a problem calling imap_fetchbody with a non-existent
msg number. This will result in a segfault.
>
> grt,
> Evert
>

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux