How to reply gmail using Gmail API in the same thread_id

Hi guys currently i'm building reply message using ruby, here is my code

    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

but i got error like below:

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

My question is how to add thread_id inside Mail::Message object ? Some person using class RMail::Message like this one

is there exist parameter thread_id inside Mail::Message ? if there exist how to add it?

Upvotes: 1

Views: 606

Answers (1)

Shruti
Shruti

Reputation: 303

thread_id is a part of the Gmail message object not the Mail object.

Add it here

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

and remove this:

message.thread_id    = "#{thread_id}"

Upvotes: 1

Related Questions