Reputation: 1250
Hello I am looking to write an independent script to log into an e-mail account [IMAP], get the headers for each e-mail, locate & store the (sender's or server's) IP address from each one. I recognize that this has to be a bit comprehensive to cover the top 3 webmail providers (Yahoo, Google & Hotmail) as well as the other common header formats.
Ideally, I would like to get the senders' IP addresses, but would settle for the servers' IP addresses.
I need to do this in PHP in a regular LAMP setup.
Any ideas would help. Thank you.
Upvotes: 2
Views: 3094
Reputation: 1967
I know this post is old but the above selected answer does get an IP but it does not necessarily get the correct sending server IP address. Below is the regex I used to get the actual sending servers IP:
/^Received:\sfrom(.*)[\[\(]\s*(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})[\]\)]/im
Upvotes: -1
Reputation: 8586
After you connected, got the messages list and now is foreaching a message:
$header=imap_fetchheader($imap,$msgn);
$regex='/client\-ip\=(.+?)\;/s';
preg_match_all($regex,$header,$matches);
$clientip=echo($matches[1]);
It works like a breeze for me.
Upvotes: 0
Reputation: 10351
The details of the servers the system transits through are shown in the top of the email header in reverse-chronological order - the most recent are at the top, the first servers are at the bottom.
A quick, off-the-cuff solution would be to use a RegExp to try and find the bottom "Received:..." line containing an IP address.
A very quick test suggests that:
$regExp = '/Received:.*((?:\d+\.){3}\d+)/';
will match the lines, and return IP addresses.
Then you'd just use something like preg_match_all()
to return an array of matched lines, and use the last one of the lot.
Upvotes: 4
Reputation: 19380
That IP is IP of server who mailed it, not the sender IP. Do you want servers IP or senders IP?
Upvotes: 1