Select Page

Xero FilesAs 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  Google Apps Project Triggerscurrent 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]);
 
 }
}