Automate Gmail for Invoices via Secure Email
With 1,2 Billion users (probably underestimated) Gmail is one of the most popular email services. It has a huge set of functions and lets you search tour emails like a datastore. If you have to manage business information, some automation is a must. This contribution is a companion for a Medium article in Italian and addressed to Italy because we have electronic invoicing that may travel on secure email. And Gmail may help for managing and automating all the related communications.
If you search for something you just type in some words and Gmail will look for them in all your messages.
If you want to do something more complex, you open the dropdown menu like in the following figure and make the desired selections;
all this selections may be supplied as text.
Laura Spencer explains everything and here there is Gmail help
Labelling is the easiest way to classify. Label=TAG. The same stuff of Twitter. You decide the label and they magically appear on the menu on the left side.
When you manage automated email, their number may became overwhelming. If you don't want to see them in your inbox, but without deleting them, the solution is archiving. They remain searchables and in the "All Mails" section.
The procedure "FindMails" will let you find a precise set of messages and label or activate some action on them. The labels are supplied as text and created automatically if they don't already exist. Moreover you can mark them as important, or with stars or you may archive them. It returns an array with all the found messages (not threads!).
function TrovaMails(cerca, labels, nolabels, actions) {
var listaMsg=[];
var threads = GmailApp.search(cerca);
if (!threads || threads.length==0) {Logger.log(' no data found '); return []; }
/* Trovate mail! */
var mylabels=[]; var mynolabels=[]; actions=actions||[];
if (labels) for (var l1 = 0 ; l1 < labels.length; l1++) {
mylabels.push(GmailApp.getUserLabelByName(labels[l1])||GmailApp.createLabel(labels[l1])) ;
};
if (nolabels) for (var nl = 0 ; nl < nolabels.length; nl++) {
mynolabels.push(GmailApp.getUserLabelByName(nolabels[nl]) || GmailApp.createLabel(nolabels[nl])) ;
};
var msgs = GmailApp.getMessagesForThreads(threads);
for (var i = 0 ; i < msgs.length; i++) { for (var j = 0; j < msgs[i].length; j++) {
var msg=msgs[i][j]; listaMsg.push(msg);
for (var x = 0 ; x < actions.length; x++) {
if (actions[x]=="archive") msg.getThread().moveToArchive();
else if (actions[x]=="read") msg.markRead();
else if (actions[x]=="unread") msg.markUnread();
else if (actions[x]=="star") msg.star();
else if (actions[x]=="unstar") msg.unstar();
//else if (msg[actions[x]]) msg[actions[x](); works but dangerous commented ...as you like ;)
}
}}
for (var t in threads) {
for (var i = 0 ; i < mylabels.length; i++) threads[t].addLabel(mylabels[i]);
for (var i = 0 ; i < mynolabels.length; i++) threads[t].removeLabel(mynolabels[i]);
}
return listaMsg;
}When the document presents some formal errors, the Government refuse it with a message and a short explanation.
Sounds OK, but you don't have any other information about the original document BUT the file name (that have to be composed by your code + a counter).
So, having the original message AND the invoice document would be nice, definitely. Function gscarti does exactly that:
Finds the refusals and return in msgs the messages
var cerca='in:inbox subject:(POSTA CERTIFICATA: Notifica di scarto) -label:processed '; var resp='';
var msgs=TrovaMails(cerca, ["processed"],null, ["archive"]);
for each refusal finds the original message and stars it
var cercaInvio='is:sent has:attachment to:'+PECSDI+' '+nomefile;
var msgs2=TrovaMails(cercaInvio,["scartate"],null, ["star"]);
and merges main content and attachments
newmailBody=newmailBody+'----------------------'+msgs2[0].getBody();
var newAtt=msgs2[0].getAttachments();
for (var aa = 0 ; aa < newAtt.length; aa++) { newmailAllegati.push(newAtt[aa]); }
User terror: "I am going to lose something, for sure!".... So a nice Report with all the movements of the last month would be useful. Here we are!
All the data is written into the JReport js Object
First loop for the Invoices
var searchstring= 'is:sent has:attachment to:'+PECSDI+' newer:' +unmesefa;
var msgs1=TrovaMails(searchstring);
Second loop for notification and the Object will be updated
searchstring= 'from:'+PECSDI+' subject:(notifica || mancata) newer:' +unmesefa;
var msgs2=TrovaMails(searchstring);
At the end the Object is transformed in HTML and put a mail content and attachment
var hBody='';
for(var fn in JReport) {
var JR=JReport[fn];
hBody+='<tr><td><b>'+fn+'</b></td><td>'+JR.subject+'</td><td>'+JR.sent||''+'</td><td>'+JR.esito||''+'</td><td>'+JR.dataesito||''+'</td></tr>'
}
GScript is not ES6 compliant (incredibly). The code would be nicer and clearer.
.jpg)