【问题标题】:Is there any google API which could save gmail message attachments to google drive?是否有任何谷歌 API 可以将 gmail 消息附件保存到谷歌驱动器?
【发布时间】:2018-07-08 20:55:45
【问题描述】:

如何将 gmail 邮件附件保存到 Google Drive。有没有这样的google API?

【问题讨论】:

标签: google-chrome-extension google-drive-api gmail-api


【解决方案1】:

要添加详细信息,这里是您可以使用的 Google APIs 的主页。正如 Gaurav 所说,您可以使用 Drive 和 Gmail API。这是一个使用Apps Script的sn-p:

// GLOBALS
//Array of file extension which you would like to extract to Drive
var fileTypesToExtract = ['jpg', 'tif', 'png', 'gif', 'bmp', 'svg'];
//Name of the folder in google drive i which files will be put
var folderName = 'GmailToDrive';
//Name of the label which will be applied after processing the mail message
var labelName = 'GmailToDrive';



function GmailToDrive(){
  //build query to search emails
  var query = '';
  //filename:jpg OR filename:tif OR filename:gif OR fileName:png OR filename:bmp OR filename:svg'; //'after:'+formattedDate+
  for(var i in fileTypesToExtract){
 query += (query == '' ?('filename:'+fileTypesToExtract[i]) : (' OR filename:'+fileTypesToExtract[i]));
  }
  query = 'in:inbox has:nouserlabels ' + query;
  var threads = GmailApp.search(query);
  var label = getGmailLabel_(labelName);
  var parentFolder;
  if(threads.length > 0){
 parentFolder = getFolder_(folderName);
  }
  for(var i in threads){
 var mesgs = threads[i].getMessages();
 for(var j in mesgs){
   //get attachments
   var attachments = mesgs[j].getAttachments();
   for(var k in attachments){
     var attachment = attachments[k];
     var isImageType = checkIfImage_(attachment);
     if(!isImageType) continue;
     var attachmentBlob = attachment.copyBlob();
     var file = DocsList.createFile(attachmentBlob);
     file.addToFolder(parentFolder);
   }
 }
 threads[i].addLabel(label);
  }
}

//This function will get the parent folder in Google drive
function getFolder_(folderName){
  var folder;
  try {folder = DocsList.getFolder(folderName)}
  catch(e){ folder = DocsList.createFolder(folderName);}
  return folder;
}

//getDate n days back
// n must be integer
function getDateNDaysBack_(n){
  n = parseInt(n);
  var today = new Date();
  var dateNDaysBack = new Date(today.valueOf() - n*24*60*60*1000);
  return dateNDaysBack;
}

function getGmailLabel_(name){
  var label = GmailApp.getUserLabelByName(name);
  if(label == null){
 label = GmailApp.createLabel(name);
  }
  return label;
}

//this function will check for filextension type.
// and return boolean
function checkIfImage_(attachment){
  var fileName = attachment.getName();
  var temp = fileName.split('.');
  var fileExtension = temp[temp.length-1].toLowerCase();
  if(fileTypesToExtract.indexOf(fileExtension) != -1) return true;
  else return false;
}

希望这会有所帮助。

【讨论】:

  • 我可以在 Chrome 扩展中使用 Apps 脚本吗?实际上,我有一个 chrome 扩展程序,它列出了特定电子邮件的所有附件。为了列出附件,我使用了 Gmail API。现在我需要将这些附件保存到 Google Drive。
  • 太棒了!!我将尝试按照描述的程序进行操作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-07
  • 2021-06-13
相关资源
最近更新 更多