3

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('th14@gmail.com','th14@gmail.com', 
        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.

TH14
  • 622
  • 10
  • 24

2 Answers2

2

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('th14@gmail.com','th14@gmail.com', 
    subject()[1],subject()[2], subject()[0], message_text)
send_message(service, 'me', created_message)
Marcelo Gazzola
  • 907
  • 12
  • 28
  • 2
    Thank you for your help! A small addition for anyone else who might be confused, the `message_id` that you have to include when creating the reply email is NOT `message['id']`, but instead (and quite confusingly IMHO) it's `header['value']`, where `header` is the unique header in `message['headers']` whose `header['name']` is 'Message-ID'. And the two are very much not the same. Oops – Juan Carlos Ortiz Mar 29 '22 at 16:26
1

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:

  1. The requested threadId must be specified on the Message or Draft.Message you supply with your request. 2. The References and In-Reply-To headers must be set in compliance with the RFC 2822 standard. 3. The Subject headers must match.

Check this link for additional reference: How To Send A Reply With Gmail API

Community
  • 1
  • 1
abielita
  • 13,147
  • 2
  • 17
  • 59