I am trying to automatically save the receipts (from Amazon) I receive in GMail to Dropbox. So I have written a script that:
this code I wrote a while ago does the job if the attachement is in a format that can be converted to pdf. It gets the message in the last thread and sends both body and attachment only if an attachment is present. This is a test script.
function getAttachAndBody(){
var firstThread = GmailApp.getInboxThreads(0,1)[0];
var message = firstThread.getMessages()[0];
var attach = message.getAttachments();
var body = message.getBody();//is a string
var bodydochtml = DocsList.createFile('body.html', body, "text/html")
var bodyId=bodydochtml.getId()
var bodydocpdf = bodydochtml.getAs('application/pdf').getBytes();
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'};
// MailApp.sendEmail('xxxxxxx@gmail.com', 'transfer email as pdf : body ', 'see attachment', {attachments:[body_to_send]});
MailApp.sendEmail('xxxxxxx@gmail.com', 'transfer email as pdf : body & attachment', 'see attachment', {attachments:[attach_to_send,body_to_send]});
file.setTrashed(true);
DocsList.getFileById(bodyId).setTrashed(true)
}
}