【问题标题】:How to reply gmail using Gmail API in the same thread_id如何在同一 thread_id 中使用 Gmail API 回复 gmail
【发布时间】:2018-07-17 19:42:46
【问题描述】:

大家好,目前我正在使用 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>

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

Mail::Message 中是否存在参数 thread_id ?有的话怎么添加?

【问题讨论】:

  • 查看documentation 以进一步帮助您。据说If you are sending or migrating messages that are a response to another email or part of a conversation, your application should add that message to the related thread. This makes it easier for Gmail users who are participating in the conversation to keep the message in context.
  • 是的,我知道可以给你详细的例子可能是?

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


【解决方案1】:

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

在此处添加

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

并删除它:

message.thread_id    = "#{thread_id}"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-11
    • 1970-01-01
    • 2021-01-17
    • 2021-07-19
    • 2020-10-27
    • 2016-11-25
    • 2020-02-25
    • 2016-06-03
    相关资源
    最近更新 更多