简体   繁体   English

使用导入功能将 mime .eml 文件导入 gmail API

[英]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)

...
  • You want to import an eml file using Gmail API.您想使用 Gmail API 导入 eml 文件。
  • You have already been able to get and put values for Gmail API.您已经能够获取和放置 Gmail API 的值。
  • You want to achieve this using google-api-python-client.您想使用 google-api-python-client 来实现这一点。
    • 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.请将此视为几种可能的答案之一。

Modification point:修改点:

  • In this case, the method of "Users.messages: insert" is used.在这种情况下,使用“Users.messages:insert”的方法。

Modified script:修改后的脚本:

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

Note:笔记:

  • In above modified script, {'labelIds': ['INBOX']} is used as the metadata.在上述修改后的脚本中, {'labelIds': ['INBOX']}用作元数据。 In this case, the imported eml file can be seen at INBOX of Gmail.在这种情况下,可以在 Gmail 的收件箱中看到导入的 eml 文件。 If you want to change this, please modify this.如果你想改变这个,请修改这个。

Reference:参考:

If I misunderstood your question and this was not the result you want, I apologize.如果我误解了您的问题并且这不是您想要的结果,我深表歉意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM