Gmail API (Python) - 无法添加对主题的回复

Gmail API (Python) - Unable to add a reply to the thread

我想通过 API 添加对 gmail 线程的回复。

Google says to:

simply add a threadId key paired with a thread ID to a message's metadata, the message object.

这就是我正在尝试的:

def create_message(sender, to, cc, bcc, subject, message_text, file=None, thread=None):
    message = MIMEMultipart()
    print(thread)
    message['to'] = to
    if cc:
        message['cc'] = cc
    if bcc:
        message['bcc'] = bcc
    if thread:
        message['threadId'] = thread
    message['from'] = sender
    message['subject'] = subject
    msg = MIMEText(message_text, 'html')
    message.attach(msg)
    if file:
        message = attach_file(message, file)
    return {'raw': base64.urlsafe_b64encode(message.as_string().encode()).decode()}

但它不起作用。我不知道该怎么做。

我终于成功了。 threadId 需要从 message 字典移动到 output 本身。

希望这能为其他人解决问题:

def create_message(sender, to, cc, bcc, subject, message_text, file=None, thread=None):
    message = MIMEMultipart()
    message['to'] = to
    if cc:
        message['cc'] = cc
    if bcc:
        message['bcc'] = bcc
    message['from'] = sender
    message['subject'] = subject
    msg = MIMEText(message_text, 'html')
    message.attach(msg)
    if file:
        message = attach_file(message, file)
    output =  {'raw': base64.urlsafe_b64encode(message.as_string().encode()).decode()}
    if thread:
        output['threadId'] = thread
    return output