gpt4 book ai didi

python - 使用 Gmail API 发送回复到电子邮件线程

转载 作者:太空宇宙 更新时间:2023-11-03 21:37:28 26 4
gpt4 key购买 nike

我正在尝试回复我发送给自己的电子邮件,电子邮件的主题是“测试功能”我有一个函数 subject() ,它返回下面的 subject、message_id 和 thread_id('测试功能','DEFxmu7HPSRAti50ki2i6PK_DOOPLwMm5fiR+_dPkcOR7mep7hQ@mail.gmail.com', '166e2507fc661924')

我的完整代码是:

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'])

它发送电子邮件,但不发送到所需的线程,仅发送一封新电子邮件。

最佳答案

您需要将 threadId 添加到 create_message 函数的返回值中。

return {
'raw': base64.urlsafe_b64encode(message.as_string().encode()).decode(),
'threadId':thread_id
}

另外,删除 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
}

现在只需调用 send_message 传递从上面的函数创建的消息:假设 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)

关于python - 使用 Gmail API 发送回复到电子邮件线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53149409/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com