4

I am having trouble sending a reply to a previously sent message. In other words, I need to send an email (emailA), and then send another email in reply to emailA (emailB) regardless of whether the recipient responded to emailA.

I can create a draft, and send a draft using the API. After sending the draft, I get an ID, the output looks like this:

{u'labelIds': [u'SENT'], u'id': u'16303ecfd567afa3', u'threadId': u'16303ecad298cdd1'}

Then, I convert that 'id' into a Message-ID, using the following code:

mime_msg = GetMimeMessage(service, "neil@navforward.com", message1['id'])

The output, given the above example, is:

<CACJxmNStP826HnWHKP_G=0ju7bGotmCPQ2C9RRWxPGMs9WMmew@mail.gmail.com>

Then, to send the next email (emailB), I add that Message-ID to the email's headers as follows:

  message = MIMEText(message_text)
  message['to'] = to
  message['from'] = sender
  message['subject'] = subject

  message['threadId'] = thread_id
  message["In-Reply-To"] = message_id
  message["References"] = message_id

I also ensure that the subject is identical.

Although the email appears as a reply in my gmail inbox, after sending to recipients outside of the gmail ("outside address" - Outlook/hotmail), the email does not appear as a reply.

Further, if I manually send an email and a reply to that email to the same outside address, the reply shows as expected (so this is not a problem with how the other domain's display).

Further, if I send the first email (emailA) via the API, and then create a reply draft, the draft appears as a reply in my gmail inbox, but when I manually send, the email still does not appear as a reply to the outside address.

Neil Aggarwal
  • 511
  • 1
  • 10
  • 29

1 Answers1

-1

Sending messages

If you're trying to send a reply and want the email to thread, make sure that:

The Subject headers match The References and In-Reply-To headers follow the RFC 2822 standard.

Community
  • 1
  • 1
ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
  • 2
    Thanks Noogui! The problem is basically that. The code above at least appears to do exactly what you suggest. But it doesn't work. Was hoping someone could help me identify what I am doing wrong despite the fact that I am at least trying to follow the GMAIL API documentation exactly. – Neil Aggarwal Apr 27 '18 at 21:46
  • 1
    Any resolutions? I am still struggling with the same. – Shivam Sahil Apr 14 '21 at 07:30