Skip to content

Instantly share code, notes, and snippets.

@srosato
Last active August 15, 2023 19:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save srosato/b17d394373e3829219012e34431e042b to your computer and use it in GitHub Desktop.
Save srosato/b17d394373e3829219012e34431e042b to your computer and use it in GitHub Desktop.
Forward Gmail emails to Quickbooks using a Google Script.
{
"timeZone": "America/Toronto",
"dependencies": {
"enabledAdvancedServices": [
{
"userSymbol": "Gmail",
"serviceId": "gmail",
"version": "v1"
}
]
},
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8",
"oauthScopes": [
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/gmail.modify",
"https://mail.google.com/"
]
}
// create a script on https://script.google.com/
function processReceipts() {
const userEmail = Session.getActiveUser().getEmail().toLowerCase();
const newReceiptsLabel = GmailApp.getUserLabelByName('receipts-unprocessed');
const processedReceiptsLabel = GmailApp.getUserLabelByName('receipts-processed');
const destinationEmail = "your-address@qbodocs.com"
console.log(`Running as ${userEmail}`);
const threads = newReceiptsLabel.getThreads();
console.log(`Number of threads to process: ${threads.length}`)
for (const thread of threads) {
for (const message of thread.getMessages()) {
if (!message.getFrom().toLowerCase().includes('<' + userEmail + '>')) {
console.log(`From: ${message.getFrom()} Subject: ${message.getSubject()} Date: ${message.getDate()}`);
message.forward(destinationEmail, { from: userEmail });
message.markRead();
}
}
thread.addLabel(processedReceiptsLabel);
thread.removeLabel(newReceiptsLabel);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment