[英]Importing mime .eml file to gmail API using the import function
I am a python developer and somewhat new to using Google's gMail API to import .eml files into a gMail account.我是一名 Python 开发人员,对使用 Google 的 gMail API 将 .eml 文件导入 gMail 帐户有点陌生。
I've gotten all of the groundwork done getting my oAuth credentials working, etc.我已经完成了所有基础工作,使我的 oAuth 凭据工作等。
However, I am stuck where I load in the data-file.但是,我被困在加载数据文件的位置。 I need help loading the message data in to place in a variable..
我需要帮助将消息数据加载到变量中。
How do I create the message_data variable reference - in the appropriate format - from my sample email file (which is stored in rfc822 format) that is on disk?如何从磁盘上的示例电子邮件文件(以 rfc822 格式存储)以适当的格式创建message_data变量引用?
Assuming I have a file on disk at /path/to/file/sample.eml ... how do I load that to message_data in the proper format for the gMail API import call?假设我在 /path/to/file/sample.eml 的磁盘上有一个文件......我如何以正确的格式将其加载到 message_data 以用于 gMail API 导入调用?
...
# how do I properly load message_data from the rfc822 disk file?
media = MediaIoBaseUpload(message_data, mimetype='message/rfc822')
message_response = service.users().messages().import_(
userId='me',
fields='id',
neverMarkSpam=True,
processForCalendar=False,
internalDateSource='dateHeader',
media_body=media).execute(num_retries=2)
...
service
in your script can be used for uploading the eml file.service
可用于上传 eml 文件。 If my understanding is correct, how about this answer?如果我的理解是正确的,这个答案怎么样? Please think of this as just one of several possible answers.
请将此视为几种可能的答案之一。
Before you run the script, please set the filename with the path of the eml file.在运行脚本之前,请将文件名设置为 eml 文件的路径。
eml_file = "###" # Please set the filename with the path of the eml file.
user_id = "me"
f = open(eml_file, "r", encoding="utf-8")
eml = f.read()
f.close()
message_data = io.BytesIO(eml.encode('utf-8'))
media = MediaIoBaseUpload(message_data, mimetype='message/rfc822', resumable=True)
metadata = {'labelIds': ['INBOX']}
res = service.users().messages().insert(userId=user_id, body=metadata, media_body=media).execute()
print(res)
In above script, the following modules are also required.在上面的脚本中,还需要以下模块。
import io
from googleapiclient.http import MediaIoBaseUpload
{'labelIds': ['INBOX']}
is used as the metadata.{'labelIds': ['INBOX']}
用作元数据。 In this case, the imported eml file can be seen at INBOX of Gmail.If I misunderstood your question and this was not the result you want, I apologize.如果我误解了您的问题并且这不是您想要的结果,我深表歉意。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.