如何在同一个thread_id中使用Gmail API回复gmail

时间:2018-02-07 13:31:29

标签: ruby google-api gmail-api google-api-ruby-client

大家好,目前我正在使用ruby构建回复消息,这是我的代码

    def reply(user_id, subject, to, body, thread_id)
        client = google_client user_id
        token = Token.find_by_user_id(user_id)
        access_token = token.access_token
        gmail = Google::Apis::GmailV1::GmailService.new
        gmail.authorization = client
        message              = Mail.new
        message.date         = Time.now
        message.subject      = "#{subject}"
        message.from         = token.email
        message.to           = "#{to}"
        message.thread_id    = "#{thread_id}"

        message.part content_type: 'multipart/alternative' do |part|
            part.html_part = Mail::Part.new(body: "#{body}", content_type: 'text/html; charset=UTF-8')
        end

        # attach file
         open('/Users/jaisanasrulloh/Downloads/image.png') do |file| 
            message.attachments['image.jpg'] = file.read 
         end

        msg = message.encoded
        message_object = Google::Apis::GmailV1::Message.new(raw:message.to_s)
        gmail.send_user_message('me', message_object)
    end

但我收到如下错误:

NoMethodError: undefined method `thread_id=' for #<Mail::Message:0x007fcedb6b9c78>

我的问题是如何在thread_id对象中添加Mail::Message?有人使用类RMail::Message,如one

Mail::Message内是否存在参数thread_id?如果有如何添加它?

1 个答案:

答案 0 :(得分:0)

thread_id是Gmail邮件对象的一部分,而不是Mail对象。

在此处添加

message_object = Google::Apis::GmailV1::Message.new(raw:message.to_s, thread_id:thread_id)

并删除此内容:

message.thread_id    = "#{thread_id}"