回复Gmail API发送的电子邮件

时间:2018-02-11 08:26:12

标签: ruby-on-rails ruby gmail gmail-api

目前我正在尝试使用gmail api创建发送和回复,在文档here

必须根据RFC 2822标准设置

ReferncesIn-Reply-To,问题是当我尝试从指定的ID获取ReferencesIn-Reply-To时,如下所示:

demo request parameter

`{
 "id": "16183e0822247c79",
 "threadId": "16183e0822247c79",
 "labelIds": [
  "SENT"
 ],
 "snippet": "terkait",
 "historyId": "1640387",
 "internalDate": "1518335984000",
 "payload": {
  "partId": "",
  "mimeType": "multipart/mixed",
  "filename": "",
  "headers": [
   {
    "name": "Received",
    "value": "from 1059028371380 named unknown by gmailapi.google.com with HTTPREST; Sat, 10 Feb 2018 23:59:44 -0800"
   },
   {
    "name": "Date",
    "value": "Sat, 10 Feb 2018 23:59:44 -0800"
   },
   {
    "name": "From",
    "value": "jaisanas2@gmail.com"
   },
   {
    "name": "To",
    "value": "jaisanas3@gmail.com"
   },
   {
    "name": "Message-Id",
    "value": "\u003cCA+8aSZeXMOETdH8NYtd18UWk5eiQnvT0oEnEWy_1HL6mJPuKjw@mail.gmail.com\u003e"
   },
   {
    "name": "Subject",
    "value": "terkait"
   },
   {
    "name": "Mime-Version",
    "value": "1.0"
   },
   {
    "name": "Content-Type",
    "value": "multipart/mixed; boundary=\"--==_mimepart_5a7ff7f050e3_3263ffa0ceb1cc020ea\"; charset=UTF-8"
   },
   {
    "name": "Content-Transfer-Encoding",
    "value": "7bit"
   }
  ],
  "body": {
   "size": 0
  },
  "parts": [
   {
    "partId": "0",
    "mimeType": "multipart/alternative",
    "filename": "",
    "headers": [
     {
      "name": "Content-Type",
      "value": "multipart/alternative; boundary=\"--==_mimepart_5a7ff7f05063_3263ffa0ceb1cc01916\"; charset=UTF-8"
     },
     {
      "name": "Content-Transfer-Encoding",
      "value": "7bit"
     }
    ],
    "body": {
     "size": 0
    },
    "parts": [
     {
      "partId": "0.0",
      "mimeType": "text/html",
      "filename": "",
      "headers": [
       {
        "name": "Content-Type",
        "value": "text/html; charset=UTF-8"
       },
       {
        "name": "Content-Transfer-Encoding",
        "value": "7bit"
       }
      ],
      "body": {
       "size": 14,
       "data": "PHA-dGVya2FpdDwvcD4="
      }
     }
    ]
   }
  ]
 },
 "sizeEstimate": 929
}`

如果您看到结果没有标题In-Reply-ToRefernces,我的问题是可以使用API​​回复电子邮件吗?

这是我在ruby中的代码:

            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      = "Re: #{subject}"
            message.from         = token.email
            message.to           = "#{to}"
            # message.thread_id    = "#{thread_id}"
            message.message_id = "\u003cCA+8aSZeXMOETdH8NYtd18UWk5eiQnvT0oEnEWy_1HL6mJPuKjw@mail.gmail.com\u003e"

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

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

此代码在同一个帖子中成功发送电子邮件,但没有回复电子邮件,这就是我的gmail发送的电子邮件中的内容:

enter image description here

正如您所看到的,带有正文lauv的邮件未回复邮件terkait而是我刚发送了电子邮件lauv,我的问题是如何回复电子邮件?

1 个答案:

答案 0 :(得分:0)

在这里,您需要设置In-Reply-To和References标头。如果线程中只有一封电子邮件,则将message_id用作“回复”和“引用”