I have an Appscript program that I've used for a long time. It sends me an email with an attachment. The attachment is a Google sheet that has been exported to an xlsx format.
All of a sudden, I'm getting the error: Exception: Service unavailable: Gmail
I use this about 10 times a day, so I don't think I'm hitting any rate limits.
Here is a minimal example:
let spreadsheet = SpreadsheetApp.getActive();
let spreadsheetId = spreadsheet.getId();
let sheetName = spreadsheet.getActiveSheet().getName();
let sheetId = spreadsheet.getSheetByName(sheetName).getSheetId();
let url = `https://docs.google.com/spreadsheets/d/${spreadsheetId}/export?format=xlsx&gid=${sheetId}`;
let params = { method: "GET", headers: { "authorization": "Bearer " + ScriptApp.getOAuthToken() } };
let response = UrlFetchApp.fetch(url, params).getBlob().setName(sheetName);
let email = "myname@mydomain.com";
GmailApp.sendEmail(email, 'subject', '', {
attachments: {
fileName: 'filename.xlsx',
content: response.getBytes(),
mimeType: `application/xlsx`,
}
});
Interestingly, it works if I remove the attachment. I tried it with different attachments and formats too and all return the same error. I also tried it in Incognito. I would really appreciate any help! Thanks!
Potential Workaround:
I encountered { [Exception: Service unavailable: Gmail] name: 'Exception' }
to your code but as I've tried the solution that I created and to make this work by making the body
parameter not blank
or filled
.
Sample Script:
GmailApp.sendEmail(email, subject, ' ',{
from: 'test@email.com',
attachments: {
fileName: filename,
content: response.getBytes(),
mimeType: `application/${format}`,
},
});
Reference: