Introduction: The Problem of Missing Triggers in Gmail

For those who love to automate tasks, Gmail is a powerful tool, but it has a major limitation: there is no native trigger to detect every new email.

There is a notification for new messages in the smartphone app, but it is not easily integrable to create a trigger that fires with each new message received.

This lack can be frustrating for those who want to trigger automatic actions or integrations with other applications.

Fortunately, I found a solution: you can use the Google Apps Script tool to create a script that simulates a trigger upon receiving each new email. In this article, I’ll show you how I implemented it and how you can do it too. The Goal: Automating Gmail for Custom Actions

Imagine receiving a new email and automatically triggering an action: sending a notification to your phone, updating a spreadsheet, or activating a webhook in IFTTT. Thanks to Google Apps Script, we can detect each new email by assigning it a specific label (e.g., “IN”) and using a script that performs custom actions.

The practical applications of this solution are endless:

  • Custom real-time notifications.
  • Automations to organize the inbox.
  • Integration with other tools like Slack, Trello, or Google Sheets.
  • And more… the only limit can be your imagination!

Preparation: What You Need to Get Started

To put this solution into practice, you will need:

  • A Gmail filter: to automatically assign the “IN” label to each new email.
  • Google Apps Script: to create and manage the script.
  • A webhook service: such as the one provided by IFTTT, to connect the Gmail notification to other tools.

How My Script Works

My script operates in three main steps:

  1. Identify emails with the “IN” label: Every 15 minutes, the script scans Gmail for new emails with this label.
  2. Gather relevant information: Sender, subject, date, and other details are extracted.
  3. Trigger a webhook: The information is sent via a webhook call, which can trigger an action on IFTTT or other tools.

Step-by-Step Implementation

1. Configure the Gmail filter

    Go to Gmail and create a filter for incoming emails. For detailed instructions, you can refer to the official Google support page.

    • Filter criteria: to filter all incoming emails, you could set “Size greater than 1 byte”.
    • Action: assign the “IN” label to the filtered emails.

    2. Create the script in Google Apps Script

      Access Google Drive with the same Gmail account, and create a new script by clicking on New -> More -> Google Apps Script. Then, copy and paste the following code into the editing window:

      function notificaNuoviMessaggi() {
        var userLabelString = "IN";
        const threads = GmailApp.search("label:inbox label:IN "); // Cerca tutte la mail nella inbox (NON archiviate), che hanno anche la label 'IN', aggiunta tramite filtro
        var userLabel = GmailApp.getUserLabelByName(userLabelString);
        for (var i = threads.length - 1; i >= 0; i--) {  // In questo modo i messaggi sono ordinati dal più vecchio al più recente
          const thread = threads[i];
          const messages = thread.getMessages();
          for (const message of messages) {
            var oggetto = message.getSubject();
            var mittente = message.getFrom();
            // estraggo solo l'indirizzo email:
            var email = mittente.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);
            // Scateno il trigger di IFTTT
            const response = UrlFetchApp.fetch("https://maker.ifttt.com/trigger/notificaGmail/with/key/TUA_API_KEY?value1=" + oggetto + "&value2=" + email);
            // Attendo 1 secondo tra una chiamata e l'altra:
            Utilities.sleep(1000);
          }
          // Rimuovo la label 'IN' dai thread già elaborati:
          thread.removeLabel(userLabel);
        }
      }

      Save the script and create a new trigger to run the function every 15 minutes.

      3. Connect the Webhook to IFTTT

        Create an applet on IFTTT that uses the webhook as a trigger. Configure the action you want (e.g., send a notification, add data to a Google Sheet, etc.).

        The Benefits of This Solution

        This setup offers numerous advantages:

        • Customized Automation: You can tailor the script to your specific needs.
        • Scalability: It works with any number of emails or integrations.
        • Time-Saving: It reduces repetitive manual tasks.
        • Powerful Integration: It can be connected to dozens of tools like Trello, Slack, or Google Sheets.

        Possible Limitations and Tips for Improvement

        • 15-Minute Interval: Since Google Apps Script has a daily usage limit, this interval doesn’t excessively impact that limit. You can try decreasing it to 10 or 5 minutes, but you risk exceeding the quota.
        • Gmail API Quota: Gmail has daily usage limits; be careful if processing large quantities of emails.
        • Security: Ensure you protect sensitive data in the webhooks.

        To improve this solution, you can:

        • Add more advanced filters (e.g., process only emails from specific senders).
        • Integrate SMS or push notifications for the most urgent emails.
        • Automatically archive processed emails in a dedicated folder.

        Conclusion

        Automating email management can revolutionize your workflow, saving you hours of time each week. With this solution based on Google Apps Script and IFTTT, you can finally trigger customized actions for every new email.

        I encourage you to try this approach and share your results in the comments. If you need help or want to explore other automations, visit my website