I am trying to automatically save the receipts (from Amazon) I receive in GMail to Dropbox. So I have written a script that:
- automatically select emails with a certain label
- converts the body of the email to html
- converts the html to pdf
- email the pdf of the body and attachment to IFTTT (which automatically saves the attachments to dropbox)
- deletes the temporary files
- removes the label
The script works and generates the bodydochtml, but the PDF conversion and email don't work. I am staring at this script for hours. Where is the error in my script?
Thank you!
Function send_Gmail_as_PDF(){
var gLabel = "#Receipt";
var thread = GmailApp.search("label:" + gLabel);
for (var x=0; x<thread.length; x++) {
var messages = thread[x].getMessages();
for (var y=0; y<messages.length; y++) {
var attach = messages[y].getAttachments();
var body = messages[y].getBody();
// Create an HTML File from the Message Body
var bodydochtml = DocsList.createFile('body.html', body, "text/html")
var bodyId=bodydochtml.getId()
// Convert the HTML to PDF
var bodydocpdf = bodydochtml.getAs('application/pdf').getBytes();
// Does the Gmail Message have any attachments?
if(attach.length>0){
var file=DocsList.createFile(attach[0]);
var pdf=file.getAs('application/pdf').getBytes();
var attach_to_send = {fileName: 'pdftest.pdf',
content:pdf, mimeType:'application/pdf'};
var body_to_send = {fileName: 'body.pdf',
content:bodydocpdf, mimeType:'application/pdf'};
// Send the PDF to any email address
MailApp.sendEmail('[email protected]',
'transfer email as pdf : body & attachment',
'see attachment', {attachments:[attach_to_send,body_to_send]});
// Trash the temporary PDF and HTML files
file.setTrashed(true);
DocsList.getFileById(bodyId).setTrashed(true)
}
}
}
// Message Processed; Remove the Google Drive Label
GmailApp.getUserLabelByName(gLabel)
.removeFromThread(thread[x]);
}