I am trying to reply to an email I sent to myself, the subject of the email is "Testing Function" I have a function subject() which returns subject, message_id, and thread_id below ('Testing Function', 'DEFxmu7HPSRAti50ki2i6PK_DOOPLwMm5fiR+_dPkcOR7mep7hQ@mail.gmail.com', '166e2507fc661924')
My full code is:
def create_message(sender, to, message_id, thread_id, subject, message_text):
message = MIMEText(message_text)
message['from'] = sender
message['to'] = to
message['In-Reply-To'] = message_id
message['References'] = message_id
message['threadId'] = thread_id
message['subject'] = subject
return {'raw': base64.urlsafe_b64encode(message.as_string().encode()).decode()}
def send_message(service, user_id, message):
message = (service.users().messages().send(userId="me",
body=message).execute())
print('Message Id: %s' % message['id'])
return message
def send_email(orders):
SCOPES = 'https://mail.google.com/'
credentials = auth.get_user_oauth2_credentials(scopes=SCOPES,
client_id='xxxxx',
client_secret='xxxxxx')
service = discovery.build('gmail','v1', credentials=credentials)
message_text = orders[0]
created_message = create_message('[email protected]','[email protected]',
subject()[1],subject()[2], subject()[0], message_text)
send_message(service, 'me', created_message)
send_email(['Msg Received'])
It sends the email but not to the desired thread, just sends a new email.
You need to add threadId to the return of your create_message function.
return {
'raw': base64.urlsafe_b64encode(message.as_string().encode()).decode(),
'threadId':thread_id
}
Also, remove message['threadId'] = thread_id
def create_message(sender, to, message_id, thread_id, subject, message_text):
message = MIMEText(message_text)
message['from'] = sender
message['to'] = to
message['In-Reply-To'] = message_id
message['References'] = message_id
message['subject'] = subject
return {
'raw': base64.urlsafe_b64encode(message.as_string().encode()).decode(),
'threadId':thread_id
}
Now just call send_message passing message create from the function above:
Assuming subject()[1] = "$your_thread_id"
SCOPES = 'https://mail.google.com/'
credentials = auth.get_user_oauth2_credentials(scopes=SCOPES,
client_id='xxxxx',
client_secret='xxxxxx')
service = discovery.build('gmail','v1', credentials=credentials)
message_text = orders[0]
created_message = create_message('[email protected]','[email protected]',
subject()[1],subject()[2], subject()[0], message_text)
send_message(service, 'me', created_message)
Based from this documentation, you can add a message to a thread as part of inserting or sending a message.
In order to be part of a thread, a message or draft must meet the following criteria:
- The requested
threadIdmust be specified on theMessageorDraft.Messageyou supply with your request. 2. TheReferencesandIn-Reply-Toheaders must be set in compliance with the RFC 2822 standard. 3. TheSubjectheaders must match.
Check this link for additional reference: How To Send A Reply With Gmail API
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With