I'm trying to send replies via the Gmail API in Python and can't seem to get the Gmail web client to thread the messages. However, they are being threaded in Outlook, which is very strange.
Here's how I'm creating the messages:
def create_message(sender, to, subject, message_text, reply_to, thread_id, message_id):
message = MIMEText(message_text, 'html')
message['to'] = to
message['from'] = sender
message['subject'] = subject
if reply_to:
message['threadId'] = thread_id
message['In-Reply-To'] = message_id
message['References'] = message_id
return {'raw': base64.urlsafe_b64encode(message.as_string())}
Where both thread_id
and message_id
are the MIME Message-ID, formatted like <CAGvK4+WJmQGjg6R_QT4rJQApFPeH2xV14DnAVcksTtrc7giE8A@mail.gmail.com>
.
I tried changing the message['threadId']
to the threadId
returned by createMessage
(something like 168f38ab8c831d11
) while keeping the In-Reply-To
and References
headers as the MIME Message-ID, but that doesn't work either (in fact, that makes the message not thread in both Gmail and Outlook).
Any help is appreciated! I've tried just about every combination of setting these headers that I can think of with no luck. Thank you!