I tried this and worked well (not sure if this is the only approach):
function getattach(){ var firstThread = GmailApp.getInboxThreads(0,1)[0]; var message = firstThread.getMessages()[0]; var attach = message.getAttachments(); Logger.log(attach[0].getDataAsString() ) if(attach.length>0){ var file=DocsList.createFile(attach[0]) var pdf=file.getAs('application/pdf').getBytes();
EDIT 1: deleted
EDIT 2: there is a new version with the body in the pdf application, html is also supported (using the DocsList services), temporary documents are deleted. In a word: quite satisfying; -)
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(' emailadress@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) } }
source share