Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reply sent email by Gmail API

Currently I'm trying to create send and reply using gmail api, in documentation here

Refernces and In-Reply-To must be set in compliance with the RFC 2822 standard, the problem is when I try to get References and In-Reply-To from specified id like below:

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": "[email protected]"
   },
   {
    "name": "To",
    "value": "[email protected]"
   },
   {
    "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
}`

When you see the result there are no headers In-Reply-To and Refernces, my questions is it is possible to reply email using API ?

Here is my code in 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)

This code successfully send email in the same thread, but not reply email, here is what looks like inside my gmail sent emails:

enter image description here

As you can see, message with body lauv does not reply message terkait instead I just sent email lauv, my question is how to reply email?

like image 387
Jais Anasrulloh Ja'fari Avatar asked Nov 21 '25 17:11

Jais Anasrulloh Ja'fari


1 Answers

Here you need to set In-Reply-To and References headers. If there is only one email in the thread use the message_id as In-Reply-To and References

like image 137
Vidura Dantanarayana Avatar answered Nov 24 '25 08:11

Vidura Dantanarayana



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!