As an accountant I have had to do my fair share of book keeping over the years and have produced cupboards full of paperwork in my time. When Xero came along the ability to store documents electronically was a big plus. However Xero only likes these files as attachments and many invoices are sent just in the body of an email which you can’t just forward to your Xero inbox.
If you use Gmail there is a solution to this problem which costs nothing and is easy to implement.
Gmail Labels
The first step is to set-up filters in Gmail to label your receipts with either of two labels:
#Receipt – for email based receipts
#ReceiptAttch – for receipts sent via email as an attachment (you are probably already forwarding these to Xero)
As receipts come in. open the email and choose ‘Filter messages like these’ from the More tab and set the appropriate label.
Now we are labelling the two types of receipts we need to forward the receipts received as attachments and convert the non attachment receipts to pdf so they can also be forwarded to Xero. This can be done via setting up two Google apps scripts.
Google Apps Scripts
Google enables users to add additional functions to their apps suite and offer lots of advice here, this is a free service and these scripts can be run within your own apps accounts without any additional cost or software to install if your are signed into your Google account just visit script.google.com. These scripts can be saved in your google docs just like any other document.
There are many developers offering scripts which you are free to use and edit and below I have included versions I have edited for a popular Gmail as PDF script and it works as follows:
When an email is received by Gmail your filters will label it but leave as unread in your inbox.
The scripts will check your inbox for any messages with these labels.
If a label of #Receipt is found the script will run and convert the email body to a PDF file.
It will email the file to your Xero inbox.
The email will be marked as read.
The #Receipt label will be removed as it has now been processed and a new label added sentToXero.
The message is archived to remove it from your inbox.
If the label #ReceiptAttch is found the same process is run but without converting the email body to email.
Script Setup
To set up this script for use with your gmail account vist script.google.com and choose create a blank project.
Copy and paste the code below remembering to change the MailApp.sendEmail line to the email address for your Xero account.
Save the project and then via the current projects triggers set the frequency you want the script to run e.g. every hour.
That’s it all invoices you receive in your inbox will be sent to Xero as a new file and can be processed as new bills.
Convert Gmail Receipt to PDF and Forward to Xero Script
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(); var subject = messages[y].getSubject();
// Create an HTML File from the Message Body var bodydochtml = DriveApp.createFile('body.html', body, "text/html") var bodyId = bodydochtml.getId()
// Convert the HTML to PDF var bodydocpdf = bodydochtml.getAs('application/pdf').getBytes();
var body_to_send = { fileName: subject+'.pdf', content: bodydocpdf, mimeType: 'application/pdf' };
var attachmentList = []; attachmentList.push(body_to_send);
// Trash the temporary file bodydochtml.setTrashed(true);
// Process all attachments for (var att = 0; att < attach.length; att++) {
var file = DriveApp.createFile(attach[att]); var pdf = file.getAs('application/pdf').getBytes();
var attach_to_send = { fileName: 'receipt.pdf', content: pdf, mimeType: 'application/pdf' }; attachmentList.push(attach_to_send);
// Trash the temporary file file.setTrashed(true); } }
// Send the PDF to any email address
MailApp.sendEmail('xero.inbox.YOURINBOX@xerofiles.com',
'Receipt for processing in Xero',
'see attachment', {
attachments: attachmentList
});
//Mark as Read
for (var r = 0; r < messages.length; r++) {
GmailApp.markMessageRead(messages[r]);
}
// Set new Label as message sent to Xero
GmailApp.getUserLabelByName("sentToXero").addToThread(thread[x]);
// Message Processed; Archive and Remove #Receipt Label
GmailApp.moveThreadToArchive(thread[x]);
GmailApp.getUserLabelByName(gLabel)
.removeFromThread(thread[x]);
}
}
Gmail Receipt as attachment Forward to Xero Script
function send_as_Attchment() { var gLabel = "#ReceiptAttch"; 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(); var subject = messages[y].getSubject();
}
// Send the PDF to any email address
MailApp.sendEmail('xero.inbox.YOURINBOX@xerofiles.com',
'Receipt for processing in Xero',
'see attachment', {
attachments: attach
});
//Mark as Read
for (var r = 0; r < messages.length; r++) {
GmailApp.markMessageRead(messages[r]);
}
// Set new Label as message sent to Xero
GmailApp.getUserLabelByName("sentToXero").addToThread(thread[x]);
// Message Processed; Archive and Remove #Receipt Label
GmailApp.moveThreadToArchive(thread[x]);
GmailApp.getUserLabelByName(gLabel)
.removeFromThread(thread[x]);
}
}
Thanks so much for this helpful script. I have been faffing around trying to find quick ways of doing this for far too long!
This will save a load of time, Kevin.
best, Helen
Actually since running these scripts not all of the tagged emails are going through. I get some error messages on the xero side that say
Sorry, the file you have tried to upload is not supported
Pity… I was pretty excited about this!!!
Do you have any clues on how we could fix this?
Me again 🙂
I’ve worked out what was going wrong. The emails have to be ‘unread’ for the tagging to work. Xero was rejecting the PDF’s generated for any that had been ‘read’. This was mostly for the manual one-off’s that I was sending through. I’m hoping that the new ones that come through will not suffer this problem. Touch wood all seems to be fine right now – working nicely.
thanks again.
Glad to hear the script is working for you, just add your filters with the correct labels for attachments and non-attachments as you receive emailed receipts over the next month and they will appear in Xero going forward. I have had the odd issue when you get a duplicate blank receipt in the Xero inbox but these can just be deleted. I’m also please to see that Xero have now added a window to display the receipt when posting a new bill much more efficient.
Fixing the ReferenceError: “DocsList” is not defined in the original version of this script.
DocsList has been deprecated by Google to fix this in the function send_Gmail_as_PDF() script you need to replace the two references to DocsList to DriveApp as follows:
1. Opened the script
2. Edit
3. Find and Replace
Find = DocsList
Replace = DriveApp
4. Replace all
Then save the script.
thanks for the update – I was a bit worried about the errors coming back but happy to see you had the fix here.
cheers
Helen
Hey there, just trying to get this set up and it took me a while to realise I needed to set up another label “sentToXero”.
This is somewhat obvious now but I thought it might be worth adding that in the instructions for n00bs like me who want to get this running.
Otherwise this is really awesome – you’ve just saved me a ton of ‘future mike’ time. :*
Using this and it works great! Thanks!
Just wanted to say a big thanks Kevin – this has just saved me hours of converting emails to pdfs! Also to Helen and Mike – I wouldn’t have figured those bits out myself either! It works a treat. Cheers 🙂