I'm using the following script to check my Gmail inbox for emails I've labelled (manually or with rules) as a Purchase Invoice and auto-forward them to my accounting software.
However, .pdf attachments (the Invoice) aren't forwarding correctly: looking at the forwarded email, the attachment isn't a true .pdf file (see image below script) and my accounting software cannot access the .pdf.
Any guidance on what is going wrong?
function FwdInvoices() {
var getLabel = GmailApp.getUserLabelByName('Purchase Invoice');
var setLabel = GmailApp.getUserLabelByName('Purchase Invoice/Processed');
var recipient = 'xxxxxxx@mail.autoentry.com';
var userId = "me";
var label = "Purchase Invoice/Processed";
var labelId = Gmail.Users.Labels.list(userId).labels.filter(function(e){return e.name == label})[0].id;
var threads = getLabel.getThreads();
for (var i = 0; i < threads.length; i++) {
var thread = threads[i];
var message = thread.getMessages()[0];
var messageId = message.getId()
message.forward(recipient);
thread.removeLabel(getLabel);
//thread.addLabel(setLabel) - not using this - see below why
//add label to message only (not thread - if label applied to whole thread the sent message will have label applied and show up in 'Processed' label view.
Gmail.Users.Messages.modify({"addLabelIds": [labelId]}, userId, messageId)
//thread.moveToArchive()
}
}
Here's how the attachement looks, almost as if it's a file linked via drive instead of the actual file. If I click on it, I get prompted to select a cloud .pdf viewer, rather than the usual immediate .pdf preview.
However, you can easily include them manually with advanced options.
Sample:
var message = thread.getMessages()[0];
var messageId = message.getId();
var attachments = message.getAttachments();
message.forward(recipient, {attachments:attachments});