Skip to content

Instantly share code, notes, and snippets.

@rickcnagy
Last active December 28, 2015 12:49
Show Gist options
  • Save rickcnagy/7503349 to your computer and use it in GitHub Desktop.
Save rickcnagy/7503349 to your computer and use it in GitHub Desktop.
Short Google Apps Script to email all files marked as notUploaded to an email address, such as a Box.com email upload address
// for Google Apps Script
function gmailPDFUpload ()
{
var uploadedLabel = GmailApp.getUserLabelByName('uploaded');
var notUploadedLabel = GmailApp.getUserLabelByName('notUploaded');
var notUploadedThreads = notUploadedLabel.getThreads();
for (index in notUploadedThreads) {
var notUploadedThread = notUploadedThreads[index]
var notUploadedMessages = notUploadedThread.getMessages();
for (index in notUploadedMessages) {
uploadMessage(notUploadedMessages[index]);
}
// label so it doesn't get uploaded again
notUploadedThread.removeLabel(notUploadedLabel);
notUploadedThread.addLabel(uploadedLabel);
notUploadedThread.markRead();
}
}
// adapted from goo.gl/ZNIZX
function uploadMessage(message)
{
var UPLOAD_EMAIL_ADDRESS = 'upload.Unfiled.oattbrecuj@u.box.com'
var attach = message.getAttachments();
var body = message.getBody();
var subject = message.getSubject();
// 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();
var subjectName = subject + '.pdf';
Logger.log(subjectName);
var body_to_send = {fileName: subjectName,
content:bodydocpdf, mimeType:'application/pdf'};
// Send the PDF to any email address
GmailApp.sendEmail(UPLOAD_EMAIL_ADDRESS,
subject,
'see attachment',
{attachments:[body_to_send]});
// Trash the temporary PDF and HTML files
DocsList.getFileById(bodyId).setTrashed(true)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment