我正在尝试回复我发送给自己的电子邮件,该电子邮件的主题为“测试功能” 我有一个函数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'])
它发送电子邮件,但不发送到所需线程,仅发送新电子邮件。
答案 0 :(得分:0)
基于此documentation,您可以将消息添加到线程中,作为inserting或sending消息的一部分。
要成为主题的一部分,消息或草稿必须满足以下条件:
- 所请求的
threadId
必须在您随请求提供的Message
或Draft.Message
上指定。- 必须按照RFC 2822标准设置
References
和In-Reply-To
标头。Subject
标头必须匹配。
检查此链接以获取其他参考:How To Send A Reply With Gmail API