开发者

Get IP from e-mails in IMAP mailbox

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 address开发者_如何学Ces, 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.


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.


That IP is IP of server who mailed it, not the sender IP. Do you want servers IP or senders IP?


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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜