我正在尝试回复一封发给自己的电子邮件,该邮件的主题是“测试功能”。 我有一个名为subject()的函数,它返回以下主题、消息ID和线程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'])
它发送了电子邮件,但不是发送到所需的线程,而只是发送了一封新邮件。