使用ruby-gmail阅读Gmail邮件 [英] Reading a Gmail Message with ruby-gmail

查看:109
本文介绍了使用ruby-gmail阅读Gmail邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个来自 ruby​​-gmail gem的实例方法,它允许我读取:




  • 正文


  • 主题




的Gmail邮件。



阅读文档后,找到这里,我找不到任何东西!?



Gmail :: Message 类部分中找到 .message 实例方法;但它只是返回,因为缺乏一个更好的术语,电子邮件mumbo-jumbo,身体。

我的尝试:

 #!/ usr / local / bin / ruby​​ 
需要'gmail'

gmail = Gmail.connect('username' ,'password')

emails = gmail.inbox.emails(:from =>'someone@mail.com')

emails.each do | email |
email.read
email.message
end

现在:


  1. email.read does not work li>
  2. email.message 返回上面提到的mumbo-jumbo

其他人在SO上提出这个问题,但没有得到答案。

解决方案

可能并不是你的问题的答案,但我会告诉你我过去做了什么。我尝试使用ruby-gmail gem,但它没有按照我希望它在阅读邮件方面做的事情。或者,至少,我无法实现它的工作。相反,我使用内置的 Net :: IMAP 类登录并收到消息。

  require'net / imap'
imap = Net :: IMAP.new('imap.gmail.com',993,true)
imap.login('< username> ;','< password>)
imap.select('INBOX')
subject_id = search_mail(imap,'SUBJECT','< mail_subject>')
subject_message = imap .fetch(subject_id,'RFC822')[0] .attr ['RFC822']
mail = Mail.read_from_string subject_message
body_message = mail.html_part.body

从这里开始,您的消息存储在 body_message 中,并且是HTML。如果你想要整个电子邮件正文,你可能需要学习如何使用Nokogiri来解析它。如果你只是想知道一些周围角色的信息,你可以使用正则表达式来找到你感兴趣的部分。



我找到了与ruby-gmail gem相关的单个页面,讨论使用ruby-gmail来阅读Gmail邮件。我今天晚上进行了一次粗略的尝试,但显然谷歌加强了我帐户的安全性,而且我没法修改我的Gmail配置(根据我收到的警告电子邮件),无法使用irb。所以我无法验证在该页面上显示的内容,但正如我所提到的,我过去的尝试是不成熟的,而 Net :: IMAP 适合我。



编辑:
我找到这个,这很酷。您需要添加

  require'cgi'


到你的班级。



我能够以这种方式实现它。在我的 body_message 之后,从该链接页面调用 html2text 方法必须将body_message转换为字符串):

  plain_text = html2text(body_message)
puts plain_text #Prints格式良好的纯文本文本到终端

以下是稍加修改的方法:

  def html2text(html)
text = html.to_s。
gsub(/(& nbsp; | \\\
| \s)+ / im,'').squeeze('').strip。
gsub(/ <([^ \ s] +)[^>] *(src | href)= \ s *(。?)([^> \s] *)\\ \\ 3 [^>] *> \ 4< \ \\\\\\\'',

links = []
linkregex = /<[^>]*(src|href)=\s*(.?)([^>\s]*)\2[^>]*>\s * / i
,而linkregex.match(文本)
链接<< $〜[3]
text.sub!(linkregex,[#{links.size}])
结束

text = CGI.unescapeHTML(

gsub(/<(script | style)[^>]>。*< \ / \ 1> / im,'')。
gsub ($< /(< |> / *,> / i,___ \ n)。$
gsub(/< blockquote(| [^>] *) > / i,'>')。
gsub(/(br)(| [^>] *)> / i,\\\
)。
gsub /<(\ / h [\ d] + | p)(| [^>] *)> / i,\\\
\\\
)。
gsub(/< [^>]> /,'')
).lstrip.gsub(/ \ n [] + /,\\\
)+\\\


for i in(0 ... links.size).to_a
text = text +\\\
[#{i + 1}] <#{CGI.unescapeHTML(links [i])} >中除非
链接[i] .nil?
end
links = nil
text
end

你也在你原来的问题中提到过你有这样的步骤:

  email.message *返回mumbo-jumbo * 

如果mumbo-jumbo是HTML,则可以使用现有的代码和这个html2text方法而不是像我在发布原始答案时所讨论的那样切换到 Net :: IMAP


I am looking for an instance method from the ruby-gmail gem that would allow me to read either:

  • the body or

  • subject

of a Gmail message.

After reviewing the documentation, found here, I couldn't find anything!?

There is a .message instance method found in the Gmail::Message class section; but it only returns, for lack of a better term, email "mumbo-jumbo," for the body.

My attempt:

#!/usr/local/bin/ruby
require 'gmail'

gmail = Gmail.connect('username', 'password')

emails = gmail.inbox.emails(:from => 'someone@mail.com')

emails.each do |email|
  email.read
  email.message
end

Now:

  1. email.read does not work
  2. email.message returns that, "mumbo-jumbo," mentioned above

Somebody else asked this question on SO but didn't get an answer.

解决方案

This probably isn't exactly the answer to your question, but I will tell you what I have done in the past. I tried using the ruby-gmail gem but it didn't do what I wanted it to do in terms of reading a message. Or, at least, I couldn't get it to work. Instead I use the built-in Net::IMAP class to log in and get a message.

require 'net/imap'
imap = Net::IMAP.new('imap.gmail.com',993,true)
imap.login('<username>','<password>')
imap.select('INBOX')
subject_id = search_mail(imap, 'SUBJECT', '<mail_subject>')
subject_message = imap.fetch(subject_id,'RFC822')[0].attr['RFC822']
mail = Mail.read_from_string subject_message
body_message = mail.html_part.body

From here your message is stored in body_message and is HTML. If you want the entire email body you will probably need to learn how to use Nokogiri to parse it. If you just want a small bit of the message where you know some of the surrounding characters you can use a regex to find the part you are interested in.

I did find one page associated with the ruby-gmail gem that talks about using ruby-gmail to read a Gmail message. I made a cursory attempt at testing it tonight but apparently Google upped the security on my account and I couldn't get in using irb without tinkering with my Gmail configuration (according to the warning email I received). So I was unable to verify what is stated on that page, but as I mentioned my past attempts were unfruitful whereas Net::IMAP works for me.

EDIT: I found this, which is pretty cool. You will need to add in

require 'cgi'

to your class.

I was able to implement it in this way. After I have my body_message, call the html2text method from that linked page (which I modified slightly and included below since you have to convert body_message to a string):

plain_text = html2text(body_message)
puts plain_text #Prints nicely formatted plain text to the terminal

Here is the slightly modified method:

def html2text(html)
  text = html.to_s.
    gsub(/(&nbsp;|\n|\s)+/im, ' ').squeeze(' ').strip.
    gsub(/<([^\s]+)[^>]*(src|href)=\s*(.?)([^>\s]*)\3[^>]*>\4<\/\1>/i,
'\4')

  links = []
  linkregex = /<[^>]*(src|href)=\s*(.?)([^>\s]*)\2[^>]*>\s*/i
  while linkregex.match(text)
    links << $~[3]
    text.sub!(linkregex, "[#{links.size}]")
  end

  text = CGI.unescapeHTML(
    text.
      gsub(/<(script|style)[^>]*>.*<\/\1>/im, '').
      gsub(/<!--.*-->/m, '').
      gsub(/<hr(| [^>]*)>/i, "___\n").
      gsub(/<li(| [^>]*)>/i, "\n* ").
      gsub(/<blockquote(| [^>]*)>/i, '> ').
      gsub(/<(br)(| [^>]*)>/i, "\n").
      gsub(/<(\/h[\d]+|p)(| [^>]*)>/i, "\n\n").
      gsub(/<[^>]*>/, '')
  ).lstrip.gsub(/\n[ ]+/, "\n") + "\n"

  for i in (0...links.size).to_a
    text = text + "\n  [#{i+1}] <#{CGI.unescapeHTML(links[i])}>" unless
links[i].nil?
  end
  links = nil
  text
end

You also mentioned in your original question that you got mumbo-jumbo with this step:

email.message *returns mumbo-jumbo*

If the mumbo-jumbo is HTML, you can probably just use your existing code with this html2text method instead of switching over to Net::IMAP as I had discussed when I posted my original answer.

这篇关于使用ruby-gmail阅读Gmail邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆