Gmail auto labels

February 01, 2018

Every day, I receive to much emails. Even if there is only one email, it is to much. Many times it is notifications (purchase on Amazon, Azure notification, notifications and notifications...).
This is my solution for automatic labels on my incoming mails.

Email alias

Many emails providers offers a hidden functionnality : you can add a "+" and what ever you want before the @.

With the powerfull search included in Gmail, it is easy to find who sent you emails. As an example, Amazon do not send all emails with the same mail address. With this simple tip, you can find ALL amazon mails with the search string : "".

For many years, I create filters from search string to apply labels on incoming emails. So what is the main problem ? Every time I subscribe with a new email alias I have to create a new label and a filter. So let's try a solution with a little piece of code.

Google App Script

Go to

Then create a new project.

This is a javascript based project, with all Google API availables. No authentication is needed, the script is already authenticated. At the first run, you will have a oauth window with authorizations for your gmail account.


                function entryPoint() {
                    var inboxThreads = GmailApp.getInboxThreads();
                    inboxThreads.forEach(function(thread) {
                      var message = thread.getMessages()[0]; // Get first message
                      var mailAddress;
                      if(message.getTo().indexOf("<") != -1){
                        // if there is a < , we need to extract the mail from < and >
                        mailAddress = message.getTo().substring(message.getTo().indexOf("<")+1, message.getTo().indexOf(">"));
                      } else{
                        // else, mail address is directly given
                        mailAddress = message.getTo();
                      // now we have the mail address, check if there is a +, check only with my own mail address 
                      if(message.getTo().indexOf("mathieu.passenaud+") != -1 ){
                        // extract the tag, between + and @
                        var labelName = mailAddress.substring(mailAddress.indexOf("+")+1, mailAddress.indexOf("@"));
                        // check if the label exists
                        var label = GmailApp.getUserLabelByName(labelName);
                        if(label == null){
                          label = GmailApp.createLabel(labelName);
Nothing extraordinary here. I need a work around between threads and message. I extract the first email to retreive the recipient.
If the label does not exists, I create it.
There is no way to create a filter.


There is no way to launch the function when a mail is received. There is two methods : a web trigger (call it as an API) or a time trigger (like a cron job). I chose the second solution :

That's all. Every time I subscribe to a mailling list or I create an account I register with a new mail address. This little trick keep my inbox clean and clear.