如何在 Ruby 中通过 IMAP 获取电子邮件的收件人地址

发布于 2024-08-21 07:19:35 字数 1026 浏览 10 评论 0原文

我们有一个用户的邮件帐户被垃圾邮件淹没。我怀疑他拥有大量电子邮件帐户,因此受到的影响比他只有一两个地址时受到的影响要大。

我想编写一些代码来扫描他的邮箱并报告邮寄地址的数量(最好是发送到他邮箱的地址,例如,如果他是密件抄送的,并且收件人:是 [电子邮件受保护],我想知道到达他的地址是哪个)。

如果信封中存在“To:”,我可以获得“To:”,但如果没有,则需要依靠“Cc:”等替代方案,并且可能实际上在消息标头中获取 regex-y 来标识此消息的发送对象。我能处理好。我只是不确定如何分解 Ruby msg 对象。

因此,对我来说更好的问题可能是,“知道我有一个从 imap.fetch 返回的对象 msg,我如何检查可用的各种方法,例如 msg.to 和 msg.from?”

也许还有“我应该使用像 Ruby 的 Mail 这样的奇特邮件库吗?”

到目前为止的代码 - 我真的对 Ruby 很陌生,好吗?

imap = Net::IMAP.new(server)
imap.login(user, pass)
imap.select(folder)

imap.search(['ALL']).each do |message_id|
  msg = imap.fetch(message_id,'ENVELOPE')[0].attr['ENVELOPE']
  if msg.to
    puts "#{msg.to[0].mailbox}@#{msg.to[0].host}: \t#{msg.from[0].name}: \t#{msg.subject}"
  else 
#    puts msg.inspect
    msg = imap.fetch(message_id,'RFC822')[0].attr['RFC822']
    puts msg.inspect
    quit
  end
#   p msg.methods
end

We have a user whose mail account is deluged with spam. I'm suspecting that he fields a large number of email accounts and therefore is subject to more than he might be with only one or two addresses.

I want to knock up some code which will scan his mailbox and report on the number of addresses that were mailed to (ideally the ones which delivered to his mailbox, eg if he's BCC'd and the To: is [email protected], I'd like to know which address was the one which reached him).

I can get the To: if it's present in the envelope, but if not then I need to fall back on alternatives like Cc: and maybe actually get regex-y in the message headers to ID who this message was delivered for. I can handle that. I'm just not sure about how to pull apart the Ruby msg object.

So maybe a better question for me would be, "knowing that I have an object msg returned from imap.fetch, how may I inspect the various methods available such as msg.to and msg.from?"

And perhaps also "should I be using a fancy mail library like Ruby's Mail?"

Code so far - I'm REALLY new to Ruby, OK?

imap = Net::IMAP.new(server)
imap.login(user, pass)
imap.select(folder)

imap.search(['ALL']).each do |message_id|
  msg = imap.fetch(message_id,'ENVELOPE')[0].attr['ENVELOPE']
  if msg.to
    puts "#{msg.to[0].mailbox}@#{msg.to[0].host}: \t#{msg.from[0].name}: \t#{msg.subject}"
  else 
#    puts msg.inspect
    msg = imap.fetch(message_id,'RFC822')[0].attr['RFC822']
    puts msg.inspect
    quit
  end
#   p msg.methods
end

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

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

发布评论

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

评论(1

不气馁 2024-08-28 07:19:35

我对我拥有的脚本做了类似的事情。它可能会帮助你。要记住的关键是返回的所有元素都是数组,即使它们只有 1 个元素。

imap.search(["ALL"]).each do |msg|
  envelope = imap.fetch(msg, "ENVELOPE")[0].attr["ENVELOPE"]
  sender = envelope.from[0].mailbox # This will give you the mailbox name without @domain.com
  recipient = envelope.to[0].mailbox # You can also do envelope.to.each { operations } to get the full list of recipients
end

信封包含您要查看的所有数据。

信封.from[0].mailbox 是不带@domain.com 信封的发件人

。to[0] 是不带@domain.com 信封的第一个收件人

。subject 是主题

,依此类推

I do something similar to this for a script that I have. It might help you out. The key to remember is that all elements returned are arrays, even if they only have 1 element.

imap.search(["ALL"]).each do |msg|
  envelope = imap.fetch(msg, "ENVELOPE")[0].attr["ENVELOPE"]
  sender = envelope.from[0].mailbox # This will give you the mailbox name without @domain.com
  recipient = envelope.to[0].mailbox # You can also do envelope.to.each { operations } to get the full list of recipients
end

The envelope is what contains all of the data you are trying to look at.

envelope.from[0].mailbox is the sender without the @domain.com

envelope.to[0] is the first recipient without the @domain.com

envelope.subject is the subject

and so on and so on

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