通过Gmail从URL发送.zip文件遭Google服务器拦截求助
I've written a Google Apps Script to fetch a zip file from a URL and send it as an attachment via Gmail, but Google's servers are blocking the zip file from being delivered. Here's the code I'm using:
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]}); }
The code successfully fetches the file, but Gmail's security systems flag and block the zip attachment before it can be sent.
Likely Causes
Google's email security filters are probably flagging the zip for one of these reasons:
- The file is hosted on an anonymous file-sharing platform (Anonfile) that’s marked as high-risk.
- The zip lacks clear metadata (like a defined filename or MIME type), triggering suspicion.
- The empty subject and body of the email trigger spam/security checks.
- Content inside the zip might contain patterns associated with malware or suspicious files.
Potential Fixes to Try
1. Add Metadata to the Blob and Fill Email Fields
Empty emails with just an attachment are far more likely to be flagged. Explicitly setting the zip's filename and MIME type can also help Google recognize it as legitimate:
function myFunction() { var url = "https://cdn-04.anonfile.com/t3ScWad7b9/fc36c282-1522257874/CRASH_FILES__2018.03.24.14.06.27_.zip"; var response = UrlFetchApp.fetch(url); // Rename blob and set correct MIME type var zipBlob = response.getBlob() .setName("crash_files_20180324.zip") .setContentType("application/zip"); // Add subject and body to the email GmailApp.sendEmail( "derekantrican@gmail.com", "Crash Files Attachment", "Please find the requested crash files attached below.", {attachments: [zipBlob]} ); }
2. Host the File on a Trusted Service
Anonymous hosting platforms like Anonfile are often flagged by security systems. Try uploading the zip to Google Drive first, then send it from there:
function sendFromDrive() { // Replace with your actual Google Drive file ID var crashFileId = "YOUR_DRIVE_FILE_ID"; var driveFile = DriveApp.getFileById(crashFileId); GmailApp.sendEmail( "derekantrican@gmail.com", "Crash Files Attachment", "Please find the requested crash files attached below.", {attachments: [driveFile]} ); }
3. Use Gmail API Instead of GmailApp
GmailApp has stricter default security restrictions. The Gmail API offers more control, which might bypass the block. First, enable the Gmail API in your script (Resources > Advanced Google Services), then use this code:
function sendWithGmailAPI() { 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().setName("crash_files_20180324.zip"); // Build the email message var message = Gmail.newMessage(); message.payload = Gmail.newMessagePart(); message.payload.headers = [ {name: "To", value: "derekantrican@gmail.com"}, {name: "Subject", value: "Crash Files Attachment"} ]; // Encode email body as base64 message.payload.body = { type: "text/plain", data: Utilities.base64Encode("Please find the requested crash files attached.") }; // Prepare the attachment var attachmentPart = Gmail.newMessagePart(); attachmentPart.mimeType = "application/zip"; attachmentPart.filename = zipBlob.getName(); attachmentPart.body = { data: Utilities.base64Encode(zipBlob.getBytes()), size: zipBlob.getBytes().length }; message.payload.parts = [attachmentPart]; // Send the email Gmail.Users.Messages.send(message, "me"); }
4. Check the Zip File Content
If none of the above work, the zip itself might contain files that trigger malware detection. Try:
- Recompressing the files with a different tool (e.g., 7-Zip instead of Windows built-in compression).
- Scanning the zip with an antivirus tool to rule out actual malicious content.
- Removing any executable files or suspicious-looking content from the zip before sending.
内容的提问来源于stack exchange,提问作者derekantrican

