0

I want to send reply message from Gmail Api and it is going fine and the message is threaded or appended to the reciever mailbox (A & B user). But If I add new CC user (we name as C) then the new User should see threaded messages which was previously communicated between A & B.

Please help me out if anybody know the solution

<?php
$client = getClient();
$gmail = new Google_Service_Gmail($client);
$message = new Google_Service_Gmail_Message();
$optParam = array();
$referenceId = '';
$thread = $gmail->users_threads->get('someid@gmail.com', $threadId);
$optParam['threadId'] = '16c632fd24536690';
$threadMessages = $thread->getMessages($optParam);
$messageId = $threadMessages[0]->getId();
$subject = "Re: Thread mail test";
$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
$mail->From = $from_email;
$mail->FromName = $from_name;
$mail->addCustomHeader('In-Reply-To', 
'<CAAdsdfsdf890sdjfklG4rJzoepBbWn3Crhq9sdfGq6kg@mail.gmail.com>');
$mail->addCustomHeader('References', 
'<CAAdsdfsdf890sdjfklG4rJzoepBbWn3Crhq9sdfGq6kg@mail.gmail.com>');
$mail->addAddress($to);
$mail->addcc($cc);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->preSend();
$mime = $mail->getSentMIMEMessage();
$raw = rtrim(strtr(base64_encode($mime), '+/', '-_'), '='); 
$message->setRaw($raw);
$message->setThreadId($threadId);
$response = $gmail->users_messages->send($userId, $message);
?>
Nitin Jain
  • 83
  • 1
  • 14
  • This isn't how the GMail UI works either. If you want to share old messages with someone not included on them, you should send them the email body containing the conversation history. – jdp Aug 06 '19 at 16:21
  • @jdp Then how to append the conversation history from API such that new user can see conversation history in trimmed content. Like when we reply from actual Gmail inbox then conversation history goes in trimmed content and message above trimmed content. – Nitin Jain Aug 06 '19 at 16:30

2 Answers2

1

Using Google Apps Script instead of Gmail API

would allow you to use message.forward which would send all the messages of a thread. You could implement it by

  • List all thread messages with thread.getMessages()
  • Get the last thread message with threadMessages[threadMessages.length-1]
  • Forward the last thread message to desired recipients (A, B and C) with message.forward()

If you want to stick to Gmail API, a workaround I can think of would imply to

  • get all thread messages
  • Get their raw contents
  • Append the raw contents of each thread message to the body of the message to send

An example:

function myFunction() {
var myThread = GmailApp.getThreadById("PASTE HERE THE THREAD ID");
var threadMessages = MyThread.getMessages();
var lastMessage = threadMessages[threadMessages.length-1];
lastMessage.forward("emailA", "emailB", "emailC");
}
ziganotschka
  • 25,866
  • 2
  • 16
  • 33
  • @zignotschka Your code is helpful. But, I have not done Google apps script earlier so I gone through the google documents to run your code. But unfortunately I did not found it easy to run `thread.getMessages()` function in HTML file. Can you please suggest some small code which include Gmail API authentication & then run it from html file directly? – Nitin Jain Aug 09 '19 at 17:26
  • 1
    In Apps Script you can use GmailApp https://developers.google.com/apps-script/reference/gmail/gmail-app instead of Gmail API. You can use deploy its methods directly within the .gs file and do not need an HTML file. I updated my answer and included a sample. Hope this helps! – ziganotschka Aug 09 '19 at 19:28
  • I have run your sample code and it works perfectly!! and last thread messages are being forwarded..but when I run the reply code which also should append the previous thread plus reply mail but it's getting error as messageThread undefined. Here is my sample code `var firstThread = GmailApp.getInboxThreads(0,1)[0]; var message = firstThread.getMessages[0]; messageThread.replyAll("incapable of HTML", { htmlBody: "some HTML body text", cc:'someemail' });` – Nitin Jain Aug 09 '19 at 20:27
  • `messageThread` is a class, to use the method `replyAll`, you need to apply it to a defined object of this class, e.g. `firstThread.replyAll`. Keep in mind your first thread is not a constant and will change when new message threads are created in your mailbox through incoming/ sent messages. I suggest that once you retrieve the correct thread with `GmailApp.getInboxThreads(X,1)[Y].getId()` - you store this Id and access the thread with `myThread = GmailApp.getThreadById('USE ALWAYS THE SAME ID FOR THE SAME THREAD')`. – ziganotschka Aug 09 '19 at 23:05
  • Now I could understand the basics of apps script functionality with your code. Now as I was doing app script or gmail Api because I have website hosted ticketing system and any mail comes in gmail will be automatically fetch by ticketing software and stores it in database & creates ticket. so now from gmail body & header I could fetch Message-id in show original message which is like ``. so my question is how should I fetch message or thread Id from the above message-id in app script? – Nitin Jain Aug 10 '19 at 21:14
  • How did you fetch the thread Id before in PHP? You can do it in Apps Script by using the Gmail API. Navigate from the Apps Script UI to Resources->Advanced Google Services and enable `Gmail API`.The method Gmail.Users.threads: list allows you to incorporate a query parameter q to filter the results https://developers.google.com/gmail/api/v1/reference/users/threads/list. Same for message Ids. – ziganotschka Aug 11 '19 at 20:32
0

The Simple way to send the Message in thread is as

1. Message Body should be like this

enter image description here

2. Convert message to base64 encode.

3. Use https://developers.google.com/gmail/api/v1/reference/users/messages/send to send an email using thread-id.

4.enjoy coding.

Pravin Pagare
  • 159
  • 1
  • 3