derekantrican
derekantrican

Reputation: 2285

Sending a .zip from URL through Gmail

Here's my code:

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("[email protected]", "", "", {attachments: [blob]});
}

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:

blocked

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

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.

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("[email protected]", "", "", {attachments: [file.getBlob()]});
}

Same result. Any other suggestions?

Upvotes: 0

Views: 1206

Answers (1)

Anton Dementiev
Anton Dementiev

Reputation: 5716

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().

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

enter image description here

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});

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.

enter image description here

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): enter image description here

Upvotes: 1

Related Questions