Jump to content

IMAP Header Problem


ballouta

Recommended Posts

Hello

 

i have this code, it is all working but the From email address is not showing, instead it is showing the sender's name!

 

Online Demo! http://www.motif-services.com/hc.php

 

the Problem here: Header From : Mohammad Daouk

$SaveFileDirectory = "/home/motifser/public_html/malek/";


function WriteToFile($file,$contents)	{
// IF the contents is empty lets just make it one space
if ($contents=='' || empty($contents))
	$contents = " ";

// $file is the full path to the file
$fh = fopen($file, "w") or die("Could not open file!");
fwrite($fh, $contents) or die("Could not write to file");
fclose($fh);
}


//check for new messages
$mailbox = imap_open("{localhost:143/notls}INBOX", "chehab@motif-services.com", "123")
or die("Can't connect: " . imap_last_error());;

// Check messages
$check = imap_check($mailbox);

// viewing number of messages in inbox
$chec = imap_mailboxmsginfo($mailbox);
//echo "Number of Messages: " . $chec->Nmsgs . "<br />\n";
//$cont=$chec->Nmsgs;



$headers = @imap_headers($mailbox) or die("Couldn't get emails");
$numEmails = sizeof($headers); echo "You have $numEmails messages in your mailbox <br><br><br>";


for($j = $numEmails; $j >=1; $j--) 
{ 
$mailHeader = @imap_headerinfo($mailbox, $j);
$from = $mailHeader->fromaddress; 
$subject = strip_tags($mailHeader->subject); 
$date = $mailHeader->date;
//echo "Email from $from, subject $subject, date $date<br>"; 



if ($check->Nmsgs=='0')	{
echo "There are no messages that need to be downloaded"; 	
}	else	{

$emailIndex = $j;
echo "Processing email " . $emailIndex . "<br>";
$header = imap_header($mailbox, $emailIndex);
print("<PRE>");
print("Header Date : " . $date . "<BR>");
//print("Header To : " . $header->to) . "<BR>";
print("Header From : " . $from . "<BR>");
//print("Header cc : " . $header->cc . "<BR>");
print("Header ReplyTo : " . $from . "<BR>");
print("Header Subject : " . $subject . "<BR></PRE>");

/* Lets find the email body */
$struct = imap_fetchstructure($mailbox, $emailIndex);
$parts = $struct->parts;
$i = 0;


if (!$parts) { /* Simple message, only 1 piece */
  $attachment = array(); /* No attachments */
  $Email_Comments = imap_body($mailbox, $emailIndex);
} else { /* Complicated message, multiple parts */

  $endwhile = false;

  $stack = array(); /* Stack while parsing message */
  $Email_Comments = "";    /* Content of message */
  $attachment = array(); /* Attachments */

  while (!$endwhile) {
    if (!$parts[$i]) {
      if (count($stack) > 0) {
        $parts = $stack[count($stack)-1]["p"];
        $i     = $stack[count($stack)-1]["i"] + 1;
        array_pop($stack);
      } else {
        $endwhile = true;
      }
    }

    if (!$endwhile) {
      /* Create message part first (example '1.2.3') */
      $partstring = "";
      foreach ($stack as $s) {
        $partstring .= ($s["i"]+1) . ".";
      }
      $partstring .= ($i+1);
   
      if (strtoupper($parts[$i]->disposition) == "ATTACHMENT") { /* Attachment */
        $attachment[] = array("filename" => $parts[$i]->parameters[0]->value,
                              "filedata" => imap_fetchbody($mailbox, $emailIndex, $partstring));
      } elseif (strtoupper($parts[$i]->subtype) == "PLAIN") { /* Message */
        $Email_Comments .= imap_fetchbody($mailbox, $emailIndex, $partstring);
		echo "Email Body: $Email_Comments <br/><br/>";
		$daouk=$Email_Comments; // my code
		$emailIndex +=1;
      }
    }

    if ($parts[$i]->parts) {
      $stack[] = array("p" => $parts, "i" => $i);
      $parts = $parts[$i]->parts;
      $i = 0;
    } else {
      $i++;
    }
  } /* while */
} /* complicated message */
	foreach ($attachment as $attach)	{
		WriteToFile($SaveFileDirectory . $attach['filename'], imap_base64($attach['filedata']));
		echo "Attachment found: " . $attach['filename'] . "<br><hr>";
	}
} // end else

//imap_delete($mailbox, $j);
//imap_expunge($mailbox);
} // end for

 

 

Link to comment
Share on other sites

Taking a wild guess here... but look at this, then look at the 'Return Values' section and it seems that you want to use:

$from = $mailHeader->from; 

not

$from = $mailHeader->fromaddress; 

but since i've not tried it, I may be wrong to believe this...

Link to comment
Share on other sites

I will make this modification and test my code.

 

In this time , i would like to know the best and good way to loop over the messages found in my inbox.

In my code, i use a for loop, but I don't think it is a good way of programming in this issue!

 

1. I want to loop over the messages (prefer anthor way)

2. Fetch and copy the attched files to a specific Folder (this piece of code is working)

3. Delete this message after copying its attachment file.

 

Thanks alot

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.