代码之家  ›  专栏  ›  技术社区  ›  ironsand

如何使用官方gem从Gmail获取消息

  •  1
  • ironsand  · 技术社区  · 6 年前

    我用的是 gmail 创业板,但从2018年8月19日起将被弃用。 所以我试图改变谷歌官方的宝石。

    我想得到所有的未读邮件与一个特定的标签,像这样。

    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 回复  |  直到 6 年前
        1
  •  0
  •   Casper    6 年前

    答案是 found in the GmailService class :

    #列出用户信息 (用户id,包括垃圾邮件垃圾:无, 标签号:无 ,max_results:nil,page_token:nil,q:nil,fields:nil,quota_user:nil,user_ip:nil,options:nil)

    参数 :

    • 用户id
    • 标签ID ( 数组,字符串 )只返回标签与所有指定标签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"])