使用Gmail API将回复发送到电子邮件主题 [英] Send Reply to email thread using Gmail API
问题描述
我正在尝试回复我发送给自己的电子邮件,该电子邮件的主题为测试功能"我有一个函数subject(),它在下面返回subject,message_id和thread_id(测试功能",'DEFxmu7HPSRAti50ki2i6PK_DOOPLwMm5fiR+_dPkcOR7mep7hQ@mail.gmail.com','166e2507fc661924')
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')
我的完整代码是:
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.
推荐答案
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
threadId
must be specified on theMessage
orDraft.Message
you supply with your request. - The
References
andIn-Reply-To
headers must be set in compliance with the RFC 2822 standard. - The
Subject
headers must match.
检查此链接以获取其他参考:如何使用以下方式发送回复Gmail API
Check this link for additional reference: How To Send A Reply With Gmail API
这篇关于使用Gmail API将回复发送到电子邮件主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!