通过Gmail从URL发送.zip [英] Sending a .zip from URL through Gmail

查看:153
本文介绍了通过Gmail从URL发送.zip的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

function myFunction() {
  var url = "https://cdn-04.anonfile.com/t3ScWad7b9/fc36c282-1522257874/CRASH_FILES__2018.03.24.14.06.27_.zip";
  var blob = UrlFetchApp.fetch(url).getBlob();
  GmailApp.sendEmail("derekantrican@gmail.com", "", "", {attachments: [blob]});
}

如您所见,该函数从URL获取文件(.zip)并将其附加到电子邮件中,然后发送给它.问题是Google的服务器阻止了.zip:

As you can see, the function gets a file (a .zip) from the url and attaches it to an email that it then sends. The problem is that Google's servers are blocking the .zip:

了解详情"可在此处引导: https://support.google. com/mail/answer/6590?hl = zh_CN

"Learn more" leads here: https://support.google.com/mail/answer/6590?hl=en

此.zip文件(您可以自己从URL下载)仅包含两个.log文件和一个.xml文件-上面的url均不禁止这些文件.

This .zip (you can download from the URL yourself) only contains two .log files and a .xml file - none of which are banned on the url above.

我也尝试过先上传到Google云端硬盘,然后发送:

I've also tried uploading to Google Drive first, then sending:

function myFunction(){
  var url = "https://cdn-04.anonfile.com/t3ScWad7b9/fc36c282-1522257874/CRASH_FILES__2018.03.24.14.06.27_.zip";
  var zipBlob = UrlFetchApp.fetch(url).getBlob();
  zipBlob.setContentType("application/zip");
  var file = DriveApp.createFile(zipBlob);
  GmailApp.sendEmail("derekantrican@gmail.com", "", "", {attachments: [file.getBlob()]});
}

相同的结果.还有其他建议吗?

Same result. Any other suggestions?

推荐答案

您是否真的检查了保存在Google云端硬盘中的"zip"文件的内容?该问题可能是由于您附加了HTML页面而不是zip文件.您提供的链接用于登录页面,而不是下载本身,因此,页面的内容正是您调用UrlFetchApp.fetch()时所提供的内容.

Have you actually checked the contents of the 'zip' file that gets saved to your Google Drive? The issue is probably due to you attaching an HTML page, not the zip file. The link you provided is for the landing page, not the download itself, so the content of the page is exactly what is being served back when you call UrlFetchApp.fetch().

将"GET"请求发送到您的链接后,这是保存到我的Google云端硬盘中的内容:

Here's what was saved to my Google Drive after sending a 'GET' request to your link:

该页面要求用户手动单击按钮以启动下载.没有重定向,因此无法使用以下模式获取文件:

The page requires a user to manually click on the button to initiate the download. There are no redirects, so you can't get the file by using this pattern:

UrlFetchApp.feth(url, {followRedirects: true});

问题是,您可以获取文件的实际链接吗?好吧,有点.在Chrome中,通过按 Ctrl + J (Windows)或 CMD + Shift + J (MacOS).文件名旁边显示的URL是指向文件的实际直接链接.但是,它们的寿命很短,并且会在几秒钟内过期.

The question is, can you get the actual link to the file? Well, kind of. In Chrome, open your downloads page by pressing Ctrl + J (Windows) or CMD + Shift + J (MacOS). The URLs displayed next to file names are the actual direct links to the files. However, these are very short-lived and expire within seconds.

您可以抓取链接并将其快速粘贴到代码中以确保其正常工作

You can grab the link and quickly paste it inside your code to make sure it works

var blob = UrlFetchApp.fetch(url).getBlob();

  Logger.log(blob.getContentType()); //logs 'application/zip'
  Logger.log(blob.getName()); // logs CRASH_FILES__2018.03.24.14.06.27_.zip

 DriveApp.createFile(blob);

结果(请注意,由于服务器生成了新的唯一链接,它在几秒钟后停止工作):

Result (note that it stops working after a few seconds as the new unique link is generated by the server):

这篇关于通过Gmail从URL发送.zip的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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