Reputation: 81
I've been able to send attachment in PDF format successfully but when I try to send attachment in Word format, I get this error:
"Request failed for https://docs.google.com returned code 404. Truncated server response:
I've tried several ways suggested in this site but I just couldn't make it work.
Will very much appreciate any help on this topic. I'm a newbie in Google Script.
Here's my code:
//=================================================
// Email Employment Letter as Word format, not PDF
//=================================================
function EmailCertAsWordDoc(requestor, name, newCopyID) {
var email = requestor;
var id = DocumentApp.openById(newCopyID);
var subject = "Auto-generated Employment Letter";
var body = "\n\nAttached is the Employment Letter that you created for " + name + ". \n\n\n\n\n ";
var url = "https://docs.google.com/feeds/download/documents/export/Export?id=" + id + "&format=docx&access_token=" + ScriptApp.getOAuthToken();
var docx = UrlFetchApp.fetch(url).getBlob();
if (MailApp.getRemainingDailyQuota() > 0)
GmailApp.sendEmail(email, subject, {
Body: body,
attachments:[docx]
});
}
Upvotes: 0
Views: 321
Reputation: 81
Here's my final working code after following ziganotschka's advice:
//=================================================
// Email Employment Letter as Word format, not PDF
//=================================================
function EmailCertAsWordDoc(email, name, newCopyID) {
var subject = "Auto-generated Employment Letter";
var body = "\n\nAttached is the Employment Letter that you created for " + name + ". \n\n\n\n\n ";
var url = 'https://docs.google.com/feeds/download/documents/export/Export?id=' + newCopyID + '&exportFormat=docx';
var options = {
headers: {
Authorization: "Bearer " + ScriptApp.getOAuthToken()
},
muteHttpExceptions: true
}
var response = UrlFetchApp.fetch(url, options);
var doc = response.getBlob();
//Create the docx file in my TEMP folder in Google Drive and send
var file = DriveApp.createFile(doc).setName('Employment Letter - '+ name + '.docx');
DriveApp.getFolderById('<ID of my TEMP folder in Google Drive>').addFile(file);
var blob = DriveApp.getFileById(file.getId());
if (MailApp.getRemainingDailyQuota() > 0)
GmailApp.sendEmail(email, subject, body, {
Body: body,
attachments:[blob]
});
//Remove the docx file from the TEMP folder
DriveApp.getFileById(file.getId()).setTrashed(true);
}
Upvotes: 0
Reputation: 26836
Two things:
To obtain the export url, you need to use the file id, no the file itself - in your case it is newCopyID
Sending word files as attachments is unfortunately a little bit more complicated than pdfs. To make it work, you can create a word file on your drive, and then send it as an URL within your email body.
Sample:
function EmailCertAsWordDoc(requestor, name, newCopyID) {
var email = requestor;
var fileName = DocumentApp.openById(newCopyID).getName();
var subject = "Auto-generated Employment Letter";
var body = "\n\nAttached is the Employment Letter that you created for " + name + ". \n\n\n\n\n ";
var url = "https://docs.google.com/feeds/download/documents/export/Export?id=" + newCopyID + "&format=docx&access_token=" + ScriptApp.getOAuthToken();
var docx = UrlFetchApp.fetch(url).getBlob();
var file=DriveApp.createFile(docx).setName(fileName).getUrl();
body+=" "+file;
if (MailApp.getRemainingDailyQuota() > 0)
GmailApp.sendEmail(email, subject, body);
}
Upvotes: 1