Forum Moderators: coopster

Message Too Old, No Replies

extracting sender info from e-mail

code inside

         

jason_m

8:13 pm on Jun 2, 2010 (gmt 0)

10+ Year Member



using [php.net ] as a guide i am attempting to use the sender attribute of the imap functionalities to extract the sender e-mail address.

any help would be great! thanks!


<?php
$host='{mail.mysite.com/notls/imap4}INBOX'; //Host to connect
$user='myemail@mysite.com';
$pass='mypw';
$from='myemail@jmysite.com'; //Mail to send from

$mail=@imap_open($host,$user,$pass) or die("Can't connect: " . imap_last_error());
if($mail){
$mails=imap_num_msg($mail);
if($mails==0) {
echo "<i>no mails.</i>";
} else {
echo "$mails mails<p>";

for($i=1;$i<=$mails;$i++) {
$chead=imap_headerinfo($mail,$i);
$mid=ltrim($chead->Msgno);

echo "<a href='page.php?see=$mid'>";

echo $chead->subject;
echo "</a>";
echo "<br>";
$a=$chead->sender;
echo($a[0]); //this was an attempt to extract anything from $chead->sender. not working

echo "<br>\n";
}
}
}
?>

Matthew1980

8:31 pm on Jun 2, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Jason_m,

I've not used this before, but a quick read of php dot net tells you are the function returns objects (and quotes a long list that is there too), so:-

echo $chead->subject;
echo "</a>\n\r";
echo "<br>\n\r";
echo $chead->sender;
echo "<br>\n";

That would seem to be the way to access that information, other than that I couldn't say, but from the code you gave, that was the only thing that looked odd ;)

Although, I wonder if you could print_r($chead) to see exactly what info is available to you, just a thought though

Sorry I can't be more constructive.

Cheers,
MRb

jason_m

8:34 pm on Jun 2, 2010 (gmt 0)

10+ Year Member



matt,
thanks for the response. unfortunately what you are suggesting returns "Array"
did try what you mentioned, and appreciate the idea, but tried that already.

thanks!

jason_m

8:37 pm on Jun 2, 2010 (gmt 0)

10+ Year Member



thanks matt.
you indirectly cracked my case.

see below

<?php
...

$mail=@imap_open($host,$user,$pass) or die("Can't connect: " . imap_last_error());
if($mail){
$mails=imap_num_msg($mail);
if($mails==0) {
echo "<i>no mails.</i>";
} else {
echo "$mails mails<p>";

for($i=1;$i<=$mails;$i++) {
$chead=imap_headerinfo($mail,$i);
$mid=ltrim($chead->Msgno);

echo "<a href='page.php?see=$mid'>";

echo $chead->subject;
echo "</a>";
echo "<br>";
$a=$chead->sender;
print_r($a);

echo "<br>\n";
echo $a[0]->mailbox;
echo "@";
echo $a[0]->host;
}
}
}
?>

Matthew1980

8:39 pm on Jun 2, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there jason_m,

Well print_r($chead->sender) and see what key it gives for the info your after then just do this:-

echo $chead->sender['key_you_need'];

Seems the logical way to go if you are getting an array returned :p

Other than that I'm not sure :/

[EDIT:] You can just access those keys from the object, no need to reassign :) Glad your there though..

Cheers,
MRb

jason_m

8:49 pm on Jun 2, 2010 (gmt 0)

10+ Year Member



haha, yeah realized it was a bit silly. definitely needs some clean up but at least got me to where i wanted. thanks again.