SEARCH

The How-To Geek Forums Have Migrated to Discourse

How-To Geek Forums / Geek Stuff

obtaining sender address using imap

(7 posts)
  • Started 6 years ago by jmcguire
  • Latest reply from jmcguire
  • Topic Viewed 5027 times

jmcguire
jmcguire
Posts: 552

I have tried imap_headers, imap_fetchheader, imap_headerinfo, and imap_fetch_overview.

They all return the name of the sender, so if the person has a name applied to their account it would return, John Smith, instead of johnsmith@email.com.

I need to get the e-mail address of the sender. I am using the imap commands in PHP script. Does anyone know how to do this?

Thanks very much in advance.

Posted 6 years ago
Top
 
jd2066
jd2066
Posts: 3814

Based on what is listed at http://us2.php.net/manual/en/f.....erinfo.php you should be able to use the "imap_headerinfo" function to get that information.
Here is something based on the example in the comments:
$header = imap_header($mbox, $message_id);
$to = $header->to;
foreach ($to as $id => $object) {
$toname = $object->personal;
$toaddress = $object->mailbox . "@" . $object->host;
}
That should return the address for the last TO field in the variable $toaddress.
For emails sent to one address that works.
If you have emails sent to multiple people with the TO field you will only get the last TO field and not the other ones.

Posted 6 years ago
Top
 
jmcguire
jmcguire
Posts: 552

That would help, because imap_headerinfo has a fromaddress value, but I cannot get imap_headerinfo to return anything at all. Sorry for saying otherwise in my original post, I overlooked that. The other fields do return, but imap_headerinfo will not return any value, whether I use a specific value, or the basic command.

Posted 6 years ago
Top
 
jd2066
jd2066
Posts: 3814

Here is the original code from that page:
$header = imap_header($mbox, $message_id);
$from = $header->from;
foreach ($from as $id => $object) {
$fromname = $object->personal;
$fromaddress = $object->mailbox . "@" . $object->host;
}
Does that not return anything for $fromaddress?
It should but I don't know for sure as I haven't used the IMAP functions myself.

Posted 6 years ago
Top
 
jmcguire
jmcguire
Posts: 552

Thanks jd2066. I got it working.

Posted 6 years ago
Top
 
jd2066
jd2066
Posts: 3814

You're Welcome. Just wondering, how did you finally get it to work?

Posted 6 years ago
Top
 
jmcguire
jmcguire
Posts: 552

I had to use nested loops, putting the code you posted, inside the code that got the body. When I first got it working, it did find the e-mail address, but it only found the first one. When I posted it inside my code to find the body, then the msg_num counter worked and while it still only found one at a time, it found the correct one for the corresponding body.

Thanks for your help, it was much appreciated.

Posted 6 years ago
Top
 



Topic Closed

This topic has been closed to new replies.