从 IMAP 邮箱中的电子邮件获取 IP

发布于 2024-10-08 19:32:51 字数 247 浏览 1 评论 0原文

你好 我希望编写一个独立的脚本来登录电子邮件帐户 [IMAP],获取每封电子邮件的标题,找到并查看电子邮件的标题。存储每个人的(发送者或服务器)IP 地址。我认识到这必须有点全面才能涵盖排名前 3 的网络邮件提供商(雅虎、谷歌和 Hotmail)以及其他常见的标头格式。

理想情况下,我想获取发件人的 IP 地址,但会选择服务器的 IP 地址。

我需要在常规 LAMP 设置中用 PHP 执行此操作。

任何想法都会有所帮助。谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

以往的大感动 2024-10-15 19:32:52

系统经过的服务器的详细信息按时间倒序显示在电子邮件标题的顶部 - 最近的服务器位于顶部,第一个服务器位于底部。

一个快速、即兴的解决方案是使用 RegExp 尝试找到包含 IP 地址的底部“Received:...”行。

一个非常快速的测试表明:

$regExp = '/Received:.*((?:\d+\.){3}\d+)/';

将匹配线路,并返回 IP 地址。

然后,您只需使用 preg_match_all() 之类的方法来返回匹配行的数组,并使用该批次中的最后一个。

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.

三月梨花 2024-10-15 19:32:52

该IP是发送邮件的服务器的IP,而不是发件人的IP。您想要服务器 IP 还是发送者 IP?

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

浮生未歇 2024-10-15 19:32:52

连接后,获取消息列表,现在正在查找消息:

$header=imap_fetchheader($imap,$msgn);
$regex='/client\-ip\=(.+?)\;/s';
preg_match_all($regex,$header,$matches);
$clientip=echo($matches[1]);

这对我来说轻而易举。

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.

德意的啸 2024-10-15 19:32:52

我知道这篇文章很旧,但上面选择的答案确实获得了 IP,但不一定获得正确的发送服务器 IP 地址。下面是我用来获取实际发送服务器 IP 的正则表达式:

/^Received:\sfrom(.*)[\[\(]\s*(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})[\]\)]/im

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文