Fått till lite kod nu:
$nc = imap_open($val->name, $user, $pass);
$emails = imap_search($nc,'ALL');
if($emails){
echo '('.count($emails).') Mails ( '.$val->name.' )<br/>';
rsort($emails);
foreach ($emails as $email_id) {
$head = getHeaders($nc, $email_id); // Get Header Info Return Array Of Headers **Key Are (subject,to,toOth,toNameOth,from,fromName)
echo "Subjects :: ".$head['subject']."<br>";
echo "TO :: ".$head['to']."<br>";
echo "To Other :: ".$head['toOth']."<br>";
echo "ToName Other :: ".$head['toNameOth']."<br>";
echo "From :: ".$head['from']."<br>";
echo "FromName :: ".$head['fromName']."<br>";
echo "<br><BR>";
echo "<br>*******************************************************************************************<BR>";
echo getBody($nc, $email_id); // Get Body Of Mail number Return String Get Mail id in interger
$str=GetAttech($nc, $email_id,"./"); // Get attached File from Mail Return name of file in comma separated string args. (mailid, Path to store file)
$ar=explode(",",$str);
foreach($ar as $key=>$value){
echo ($value=="")?"":"Atteched File :: ".$value."<br>";
}
echo "<br>------------------------------------------------------------------------------------------<BR>";
}
}else{
echo 'No Mail<br/>';
}
imap_close($nc);
och tillhörande funktioner
function getHeaders($connect,$mid) // Get Header info ARG= $con och $id
{
$mail_header=imap_header($connect,$mid);
$sender=$mail_header->from[0];
$sender_replyto=$mail_header->reply_to[0];
if(strtolower($sender->mailbox)!='mailer-daemon' && strtolower($sender->mailbox)!='postmaster')
{
$mail_details=array(
'from'=>strtolower($sender->mailbox).'@'.$sender->host,
'fromName'=>$sender->personal,
'toOth'=>strtolower($sender_replyto->mailbox).'@'.$sender_replyto->host,
'toNameOth'=>$sender_replyto->personal,
'subject'=>$mail_header->subject,
'to'=>strtolower($mail_header->toaddress)
);
}
return $mail_details;
}
function getBody($connect,$mid) // Get Message Body
{
$body = get_part($connect, $mid, "TEXT/HTML");
if ($body == "")
$body = get_part($connect, $mid, "TEXT/PLAIN");
if ($body == "") {
return "";
}
return $body;
}
function get_mime_type(&$structure) //Get Mime type Internal Private Use
{
$primary_mime_type = array("TEXT", "MULTIPART", "MESSAGE", "APPLICATION", "AUDIO", "IMAGE", "VIDEO", "OTHER");
if($structure->subtype) {
return $primary_mime_type[(int) $structure->type] . '/' . $structure->subtype;
}
return "TEXT/PLAIN";
}
function get_part($stream, $msg_number, $mime_type, $structure = false, $part_number = false) //Get Part Of Message Internal Private Use
{
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;
}
function GetAttech($connect,$mid,$path) // Get Atteced File from Mail
{
$struckture = imap_fetchstructure($connect,$mid);
$ar="";
foreach($struckture->parts as $key => $value)
{
$enc=$struckture->parts[$key]->encoding;
if($struckture->parts[$key]->ifdparameters)
{
$name=$struckture->parts[$key]->dparameters[0]->value;
$message = imap_fetchbody($connect,$mid,$key+1);
if ($enc == 0)
$message = imap_8bit($message);
if ($enc == 1)
$message = imap_8bit ($message);
if ($enc == 2)
$message = imap_binary ($message);
if ($enc == 3)
$message = imap_base64 ($message);
if ($enc == 4)
$message = quoted_printable_decode($message);
if ($enc == 5)
$message = $message;
$fp=fopen($path.$name,"w");
fwrite($fp,$message);
fclose($fp);
$ar=$ar.$name.",";
}
}
$ar=substr($ar,0,(strlen($ar)-1));
return $ar;
}