如何使用官方gem从Gmail获取邮件

时间:2018-08-06 13:54:55

标签: ruby

我正在使用gmail宝石,但是将从2018年8月19日起弃用。 因此,我试图更改Google官方的宝石。

我想用这样的特定标签获取所有未读邮件。

gmail = Gmail.connect('example@gmail.com', 'password')
gmail.label('mylabel').find(:unread).each do |message|
   # to something
end

根据此文档,我可以从我的帐户中获取标签。 https://developers.google.com/gmail/api/quickstart/ruby

# Initialize the API
service = Google::Apis::GmailV1::GmailService.new
service.client_options.application_name = APPLICATION_NAME
service.authorization = authorize

# Show the user's labels
user_id = 'me'
result = service.list_user_labels(user_id)
puts 'Labels:'
puts 'No labels found' if result.labels.empty?
result.labels.each { |label| puts "- #{label.name}" }

但是仅通过阅读rubydoc中的文档,我无法确定如何通过Gmail API获取邮件。

https://www.rubydoc.info/github/google/google-api-ruby-client/Google/Apis/GmailV1

如何实现该功能? 在哪里可以找到该库的示例?

1 个答案:

答案 0 :(得分:0)

答案是found in the GmailService class

  

#list_user_messages (user_id,include_spam_trash:无, label_ids:无,max_results:无,page_token:无,q:无,字段:无,quota_user:无, user_ip:无,选项:无)

     

参数

     
      
  • user_id
  •   
  • label_ids Array ,String )—仅返回标签与所有指定标签ID匹配的邮件。
  •   
  • ...
  •   

因此,要获取带有特定标签的邮件,请致电list_user_messages,使用单个标签ID或标签ID数组:

service.list_user_messages(user_id, label_ids: "LID")
service.list_user_messages(user_id, label_ids: ["LID1", "LID2", "LID3"])
相关问题