- Problem
- The Solution
- Features
- How It Works
- Setup Instructions
- Example Use Case
- Important Notes
- Contributing
- License
In a fast-paced corporate environment, it's easy to miss critical emails amidst the flood of daily messages. You may have already set up filters to capture important emails, but enabling notifications for every message isn't feasible — the noise would be overwhelming. As a result, important emails often slip through the cracks.
This script provides a seamless way to integrate Gmail and Google Chat to ensure you never miss an important email again. It automatically scans your inbox for new, unread messages in a specific Gmail label and sends notifications for them to a Google Chat room via a webhook.
- Targeted Notifications: The script notifies you only for emails filtered under your specified Gmail label.
- Automated Processing: It automatically checks your inbox, processes unread emails, and sends them to Google Chat.
- Real-Time Alerts: Receive email notifications in Google Chat as soon as they arrive, making sure you're always in the loop for important communications.
- Working Hours Control: Notifications are sent only during weekdays (Monday to Friday) and between 8 AM and 6 PM, ensuring you’re not disturbed outside of work hours.
- Customizable: You can easily adjust the Gmail label, webhook URL, and other parameters to fit your needs.
- Checking Weekdays and Time: The script only runs during weekdays and business hours (8 AM - 6 PM) to prevent unnecessary notifications outside of working hours.
var now = new Date();
var currentHour = now.getHours();
var currentDay = now.getDay(); // Sunday is 0, Monday is 1, ..., Saturday is 6
if (currentDay >= 1 && currentDay <= 5 && currentHour >= 8 && currentHour <= 18) {
// Continue processing emails
} else {
Logger.log("It's either not a weekday or not between 8 AM and 6 PM. Skipping email processing.");
}
- Filtering Emails Using Gmail Labels: The script searches for unread emails under the specified Gmail label and excludes those already processed.
var label = 'generic-label'; // Replace this with your Gmail label
var searchQuery = `label:${label} is:unread -label:sentToChat`;
var threads = GmailApp.search(searchQuery);
- Processing and Sending Notifications to Google Chat: The script sends a message with the subject and snippet of the email to the configured Google Chat room using a webhook.
var emailSubject = message.getSubject();
var emailSnippet = message.getPlainBody().substring(0, 500); // First 500 characters of the email body
var chatMessage = {
text: `📧 **New Email Alert**:
*Subject*: ${emailSubject}
*Snippet*: ${emailSnippet}`
};
var options = {
method: 'post',
contentType: 'application/json',
payload: JSON.stringify(chatMessage)
};
UrlFetchApp.fetch(webhookUrl, options);
- Marking Emails as Processed: Once an email has been sent to Google Chat, it is marked with the
sentToChat
label to avoid reprocessing.
thread.addLabel(sentToChatLabel);
- Go to Google Apps Script.
- Create a new project and copy the script from this repository into the project.
- To follow the official documentation on Apps Script, visit Google Apps Script Documentation.
- Follow the official Gmail support documentation to create a Gmail label: Create a Gmail Label.
- Use this label in the script to filter important emails.
- Follow the official Google Chat documentation to create a webhook in your Google Chat room: Create a Webhook.
- After creating the webhook, copy the URL provided.
- Replace
YOUR_WEBHOOK_URL
in the script with the webhook URL you generated.
- Update the script to use your preferred Gmail label for filtering important emails. Replace
'generic-label'
in the script with the actual label name you're using.
- In Google Apps Script, click on the clock icon (Triggers) in the left sidebar.
- Click on "Add Trigger" and configure:
- Choose which function to run: Select the function (e.g.,
sendFilteredEmailsToGoogleChat
). - Select event source: Choose "Time-driven".
- Type of time-based trigger: Set the interval (e.g., every 10 minutes, hourly) based on your preference.
- Choose which function to run: Select the function (e.g.,
The script will now automatically check for new emails at your defined intervals and send real-time notifications to Google Chat.
- You're a corporate professional receiving tons of emails daily, but only a select few are mission-critical.
- You’ve created a Gmail filter to capture these important emails, but you can't enable notifications for all.
- This script allows you to stay on top of key communications in real-time via Google Chat, without being drowned in email alerts.
- Keep your webhook URL secure and avoid exposing it in public repositories.
- The script processes up to 10 email threads at a time to ensure efficiency.
- Processed emails are labeled with
sentToChat
to avoid duplicates.
Contributions are welcome! If you'd like to contribute to this project, you can do so in the following ways:
-
Fork the Repository: Click on the "Fork" button at the top-right corner of the repository page to create your own copy. You can clone your fork locally, make your changes, and submit a pull request (PR) to propose your improvements.
-
Raise an Issue: If you encounter any bugs, have suggestions, or would like to request a new feature, please raise an issue by navigating to the "Issues" tab and clicking "New Issue." Provide a detailed description so we can help address it quickly.
-
Pull Requests: To submit a PR:
- Fork this repository.
- Create a branch in your fork with a descriptive name for the feature you're adding (e.g.,
add-feature-x
). - Make your changes and commit them with a meaningful message.
- Push the changes to your forked repository and open a PR against this repository's
main
branch.
We appreciate your contributions to improve the functionality of this script!
This project is licensed under the MIT License. See the LICENSE file for more details.