Google API:如何将文件附加到电子邮件,表格驱动器?

时间:2018-07-22 18:52:57

标签: python google-api google-drive-api gmail

我只找到这种解决方案 https://ctrlq.org/code/20132-gmail-api-send-mail-attachments 但这并不有效-因为文件下载到本地,然后作为附件发送。

bytes: Utilities.base64Encode(file.getBlob().getBytes())

1 个答案:

答案 0 :(得分:0)

请参阅Apps Script documentation中提供的示例,

  

来自Gmail的附件。这是常规的Blob,但它是   还有一个额外的getSize()方法,该方法比调用更快   getBytes()。length,不计入已读的Gmail   配额。

// Logs information about any attachments in the first 100 inbox threads.
var threads = GmailApp.getInboxThreads(0, 100);
var msgs = GmailApp.getMessagesForThreads(threads);
for (var i = 0 ; i < msgs.length; i++) {
  for (var j = 0; j < msgs[i].length; j++) {
    var attachments = msgs[i][j].getAttachments();
    for (var k = 0; k < attachments.length; k++) {
      Logger.log('Message "%s" contains the attachment "%s" (%s bytes)',
                 msgs[i][j].getSubject(), attachments[k].getName(), attachments[k].getSize());
    }
  }
}

要详细了解Gmail API Users.messages.attachments方法,可以参考here