我试图在python图像中使用gmail api发送gmail附件文件,但在发送文本文件时出现此错误
[该代码在官方gmail api网站上提到** def创建带有附件的消息(自我、, 发件人、收件人、主题、邮件(文本、文件): message=MIMEMultipart() 消息['to']=to 邮件['from']=发件人 消息['subject']=主题我试图在python图像中使用gmail api发送gmail附件文件,但在发送文本文件时出现此错误,python,api,gmail,gmail-api,attachment,Python,Api,Gmail,Gmail Api,Attachment,[该代码在官方gmail api网站上提到** def创建带有附件的消息(自我、, 发件人、收件人、主题、邮件(文本、文件): message=MIMEMultipart() 消息['to']=to 邮件['from']=发件人 消息['subject']=主题 msg = MIMEText(message_text) message.attach(msg) content_type, encoding = mimetypes.guess_type(file) if cont
msg = MIMEText(message_text)
message.attach(msg)
content_type, encoding = mimetypes.guess_type(file)
if content_type is None or encoding is not None:
content_type = 'application/octet-stream'
main_type, sub_type = content_type.split('/', 1)
if main_type == 'text':
fp = open(file, 'rb')
msg = MIMEText(fp.read(), _subtype=sub_type)
fp.close()
elif main_type == 'image':
fp = open(file, 'rb')
msg = MIMEImage(fp.read(), _subtype=sub_type)
fp.close()
elif main_type == 'audio':
fp = open(file, 'rb')
msg = MIMEAudio(fp.read(), _subtype=sub_type)
fp.close()
else:
fp = open(file, 'rb')
msg = MIMEBase(main_type, sub_type)
msg.set_payload(fp.read())
fp.close()
filename = os.path.basename(file)
msg.add_header('Content-Disposition', 'attachment', filename=filename)
message.attach(msg)
return {'raw': base64.urlsafe_b64encode(message.as_bytes()).decode()}
def send_message(self, user_id, message):
try:
message = (self.service.users().messages().send(userId=user_id, body=message)
.execute())
print('Message Id: %s' % message['id'])
return message
except errors.HttpError as error:
print('An error occurred: %s' % error)**][1]
正如你在问题中提到的,错误,请将错误也包括在问题中。