I'm trying to automate the creation of drafts via the Gmail API, and I want these drafts to be responses to existing emails. To do this, I believe I need to set the "threadId" header (Gmail specific), the "References" header, and the "In-Reply-To" header. Additionally, for Gmail to consider the message to be a reply, the "Subject" header must match the original email.
I'm hardcoding all of these headers into a MIMEText object, and then base-64 encoding (urlsafe) the message as a string and having the Gmail API deliver it. However, the "threadId", "In-Reply-To", and "References" headers don't appear to ever make it in the email that's sent, as they don't exist in the MIME shown when clicking "Show original" in the Gmail UI.
new = MIMEText("reply body text")
new["In-Reply-To"] = "[Message-ID of email to reply to]" #looks like <[email protected]>
new["References"] = "[Message-ID of email to reply to]" #looks like <[email protected]>
new["threadId"] = "[threadId of message to reply to]" #looks like 14ec476abbce3421
new["Subject"] = "Testsend2"
new["To"] = "[Email to send to]"
new["From"] = "[Email to send from]"
messageToDraft = {'raw': base64.urlsafe_b64encode(new.as_string())}
message = {'message': messageToDraft}
draft = service.users().drafts().create(userId="me", body=message).execute()
threadId
should not be sent as part of theraw
key, for it to work it has to be separate.Somessage
should contain {raw
: my_raw,threadId
: my_thread_id} – guival