如何在邮件中附加Google云端硬盘文件并通过gmail API发送(不下载文件)? [英] How to attach a Google Drive file in message and send through gmail API (without downloading the file)?

查看:555
本文介绍了如何在邮件中附加Google云端硬盘文件并通过gmail API发送(不下载文件)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在一个项目中,我需要向用户发送邮件并附加来自Google文档的一些文档.

I'm currently working on a project where I need to send mails to users and attach some documents from Google Docs.

我有要发送的文件的文件ID.我不想下载文件,然后将其附加到邮件中.有什么方法可以直接从Google云端硬盘附加文件,而无需将其下载到我们的本地存储中吗?

I have the file id's of the documents to be sent. I don't want to download the file and then attach it to the message. Is there any way to attach files directly from google drive without downloading them to our local storage ?

我尝试过的方法-

  1. 我首先尝试导出文件,然后将类似对象的字节存储在变量中,然后将其传递给create_message()方法.但是mimeType.guess_type()需要一个类似于object的字符串,它既可以是路径也可以是URL.

  1. I first tried to export the file and then store the byte like object in a variable and then pass it to the create_message() method. But the mimeType.guess_type() expects an string like object which is either the path or an url.

然后我尝试直接将驱动器URL传递给create_message()方法,但没有成功.

Then I tried to directly pass the drive url to the create_message() method but no success.

这是我的create_message方法-

Here's my create_message method -

def create_message_with_attachment(self, sender, to, subject, message_text,files):
    """Create a message for an email.

    Args:
    sender: Email address of the sender.
    to: Email address of the receiver.
    subject: The subject of the email message.
    message_text: The text of the email message.
    file: The path to the file to be attached.

    Returns:
    An object containing a base64url encoded email object.
    """
    message = MIMEMultipart()
    message['to'] = to
    message['from'] = sender
    message['subject'] = subject

    msg = MIMEText(message_text)
    message.attach(msg)

    for fil in files:
        content_type, encoding = mimetypes.guess_type(fil)

        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(fil, 'rb')
            msg = MIMEText(fp.read(), _subtype=sub_type)
            fp.close()
        elif main_type == 'image':
            fp = open(fil, 'rb')
            msg = MIMEImage(fp.read(), _subtype=sub_type)
            fp.close()
        elif main_type == 'audio':
            fp = open(fil, 'rb')
            msg = MIMEAudio(fp.read(), _subtype=sub_type)
            fp.close()
        else:
            fp = open(fil, 'rb')
            msg = MIMEBase(main_type, sub_type)
            msg.set_payload(fp.read())
            fp.close()
        filename = os.path.basename(fil)
        msg.add_header('Content-Disposition', 'attachment', filename=filename)
        message.attach(msg)
    b64_bytes = base64.urlsafe_b64encode(message.as_bytes())
    b64_string = b64_bytes.decode()
    body = {'raw': b64_string}
    return body

files参数是array,因为我想在3-4左右发送多个附件.

The files parameter is array because i want to send multiple attachments around 3-4.

到目前为止,还没有运气.有人可以建议其他方法来实现这一目标吗?

So far there has been no luck. Can anyone suggest other methods to achieve this ?

推荐答案

Google Apps的问题在于,您无法在下载或导出数据时保留数据类型-需要将其转换为其他MIMEType.因此,如果您在发送带有附件的电子邮件时遇到问题,建议您执行以下解决方法: 在您的电子邮件中包含文件的链接,而不是文件本身.在这种情况下,您必须执行以下步骤:

The problem with Google Apps is that you cannot preserve the data type when downloading or exporting them - they need to be converted to a different MIMEType. Thus, If you have problems sending e-mails with an attachment, I suggest you to do the following workaround: Include in your e-mail a link to the file, rather than the file itself. In this case you have to perform the following steps:

与相应的用户共享感兴趣的文件.您可以通过创建和更新权限,以编程方式执行此操作.对于您的情况,您需要指定emailAddress,fileID,并在请求正文中包含要赋予电子邮件收件人的角色.如果更新现有权限,则还需要指定PermissionId. 在电子邮件正文中包含所需文件的 webViewLink .您可以通过列出文件并指定webViewLink来获取webViewLink作为您希望在回复中获得的字段

Share the file of interest with the respective user. This is something you can do programmatically by creating and updating permissions. In your case, you need to specify emailAddress, the fileID, as well as include in the request body the role you want to give to the email recipient. In case you update an existing permission, you also need to specify the permissionId. Include the webViewLink of the desired file in your email body. You can obtain the webViewLink by listing files and specifying the webViewLink as a field you want to obtain in your response

这篇关于如何在邮件中附加Google云端硬盘文件并通过gmail API发送(不下载文件)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆