Janna Theme License is not validated, go to the theme options page to validate the license, you need a single license for each domain name.

Gmail Automation: 8 Useful Google Scripts to Automate Gmail

Gmail, in and of itself, is already a very powerful email client. With the help of filters, you can set up automations to better organize your inbox. However, for power users, the filtering feature isn't enough compared to writing custom scripts. These eight Google scripts can automate your Gmail.

Automate Gmail: 800 Useful Google Scripts to Automate Gmail
Tip: Try another easy Gmail tip: Set up automatic forwarding in Gmail.

1. Auto-delete email after X number of days

Often after reading an email, we keep it in our inbox, no matter how useful it is. Now that Google has set a space limit, you might want to clean up your inbox and get rid of these useless emails. The following script can check for emails labeled "... Delete Me And delete it after a number "X" Of the days.

  1. Make sure you are signed in to your Google Account, and go to Google Scripts Create a new project.
    Automate Gmail: Eight Useful Google Scripts to Automate Gmail
  2. Paste the following script and save it. You can change the number of days (variable delay) that pass before emails are deleted from your inbox.
function auto_delete_mails() { var label = GmailApp.getUserLabelByName("Delete Me"); if(label == null) { GmailApp.createLabel('Delete Me'); } else { var delayDays = 2; // Enter # of days before messages are moved to trash var maxDate = new Date(); maxDate.setDate(maxDate.getDate()-delayDays); var threads = label. getThreads(); for (var i = 0; i < threads.length; i++) { if (threads[i].getLastMessageDate()

3. Create a new trigger to run the script on a schedule. Open “Timeline icon in the left menu -> Triggers -> Add Trigger.”
Automate Gmail: 8 Useful Google Scripts to Automate Gmail 4. Customize the trigger settings to run the script every hour (or however often you like), then scroll down and tap "save".
Automate Gmail: 8 Useful Google Scripts to Automate Gmail

Once you run this script, a new category will be created, Delete for me In your Gmail account, mark unwanted emails with this label, and they will be deleted after the number of days specified in your script.

Tip: If your email seems to be getting out of control, read our SaneBox review and see if it can help you deal with your email.

2. Archive all old emails.

If you want a less risky option than deleting email, you can create a script to archive all old emails by following the steps below.

  1. Create a new Google script with the following code:
function archiveOldEmails() { // Get all the threads in the inbox var threads = GmailApp.getInboxThreads(); // Loop through all the threads for (var i = 0; i < threads.length; i++) { var thread = threads[i]; var messages = thread. getMessages(); // Loop through all the messages in the thread for (var j = 0; j < messages.length; j++) { var message = messages[j]; var age = (new Date() - message. getDate()) / (1000 * 60 * 60 * 24); // If the message is older than 30 days, archive it if (age > 30) { thread.moveToArchive(); } } } }

2. Create a trigger (as shown above) to run the script every day.
At the time you choose, the script will run and move the emails from your inbox to the archive. These archived emails will remain available in the "all mail".

Also read:  How to set up automatic forwarding in Gmail for all or specific emails

3. Get SMS notifications for important emails

This Google text takes advantage of Google Calendar's SMS feature to send you an SMS message when you receive an important email.

  1. Create a new Google script with the following code:
function Gmail_send_sms(){ var label = GmailApp.getUserLabelByName("Send Text"); if(label == null){ GmailApp.createLabel('Send Text'); } else{ var threads = label. getThreads(); var now = new Date(). getTime(); for (var i = 0; i < threads. length; i++) { var message = threads[i]. getMessages()[0]; var from = message. getFrom(); var subject = message. getSubject(); CalendarApp.createEvent(subject, new Date(now+60000), new Date(now+60000), {location: from}). addSmsReminder(0); } label. removeFromThreads(threads); } }

2. Save it and create a trigger to play it every five minutes.
3. Create a new Gmail filter that automatically adds a label. Send a text To the emails you selected. In Gmail, click the icon Search Options in the top search bar.
Automate Gmail: 2 Useful Google Scripts to Automate Gmail 4. Customize the SMS notification criteria that will alert you that you have an important email in your inbox, then tap Create a filter.
Automate Gmail: 2 Useful Google Scripts to Automate Gmail

5. Select “Classification Application” , and select Send a text and click “Create a filter”.
Automate Gmail: 2 Useful Google Scripts to Automate Gmail The script will scan your inbox every five minutes. When it detects an email with the label Send a text It will instantly create an event in Google Calendar, which will trigger the SMS.

4. Create a summary of your emails

Instead of reading all your emails individually, you can write a script to create a single email digest of your unread email.

  1. created Google script New with the following code:
function createEmailSummary() { // Search for unread emails var threads = GmailApp.search("is:unread"); var summary = "Unread Emails:\n"; for (var i = 0; i < threads. length; i++) { var messages = threads[i]. getMessages(); for (var j = 0; j < messages.length; j++) { var message = messages[j]; summary += message.getFrom() + ": " + message.getSubject() + "\n"; } } // Search for emails with specific keywords in the subject var threads = GmailApp.search("subject:(important meeting)"); summary += "\nEmails with keyword 'important meeting' in the subject:\n"; for (var i = 0; i < threads. length; i++) { var messages = threads[i]. getMessages(); for (var j = 0; j < messages.length; j++) { var message = messages[j]; summary += message.getFrom() + ": " + message.getSubject() + "\n"; } } // You can add more search criteria here // ... // Send the summary as an email GmailApp.sendEmail("[email protected]", "Email Summary", summary); }

2. Change "[email protected]" To your email.
3. Create a new trigger to run the script every day.

When you run the script, it will send you an email with the number of each type of email you haven't read yet. The script above is a good starting point—it counts the number of emails with "important meeting" in the subject line. You can adjust the script to calculate your desired search criteria.

5. Save emails as PDF files in Google Drive

If you have an email you want to archive in Google Drive, you can use a Google script to save it as a PDF file to your Google Drive account. The following script will save all messages in an email thread as a single PDF file in Google Drive. If the email contains attachments, a folder will be created and the messages and attachments stored inside it.

  1. Create a new Google script with the following code:
function save_Gmail_as_PDF() { // Get the label "Save As PDF" or create it if it doesn't exist var label = GmailApp.getUserLabelByName("Save As PDF"); if(label == null){ label = GmailApp.createLabel('Save As PDF'); } // Get the threads with the label var threads = label.getThreads(); for (var i = 0; i < threads. length; i++) { var messages = threads[i]. getMessages(); var subject = messages[0]. getSubject(); var bodies = []; // Loop through all messages in thread for(var j = 0; j < messages.length; j++){ var message = messages[j]; bodies.push({ body: message. getPlainBody(), from: message. getFrom(), }); var attachments = message. getAttachments(); var temp_attach = message. getAttachments(); if(temp_attach.length > 0){ for(var k =0; k < temp_attach.length; k++){ attachments.push(temp_attach[k]); } } } // Create a Google Docs document from the message body var bodyDoc = DocumentApp.create(subject); var bodyDocId = bodyDoc. getId(); var bodyDocFile = DriveApp. getFileById(bodyDocId); var bodyDocBody = bodyDoc. getBody(); bodyDocBody.setText(bodys.map(obj => `From: ${obj.from}\n${obj.body}`).join('\n\n')); bodyDoc. saveAndClose(); // Convert the Google Docs document to a PDF var blob = bodyDocFile.getAs('application/pdf'); var pdfFile = DriveApp. createFile(blob); // If there are attachments if(attachments.length > 0){ // Create a folder in Drive with the message subject var folder = DriveApp.createFolder(subject); for (var j = 0; j < attachments. length; j++) { folder. createFile(attachments[j]); } // Add the PDF file of the message body to the folder folder. createFile(pdfFile); } // Remove the Google Docs document from Google Drive DriveApp.getFileById(bodyDocId).setTrashed(true); // Remove the label from the thread label. removeFromThread(threads[i]); } }

2. Save it and set a trigger to run it at regular intervals.
Automate Gmail: 5 Useful Google Scripts to Automate Gmail

Also read:  Top 8 Fixes for Messages Not Sending Photos from iPhone to Android

When you want to save an email and its attachments to Google Drive, simply mark it with a tick. Save to PDFThe text of each email and the sender, in each thread, will be displayed in order, starting with the oldest.

Good to know: How to delete files from GOOGLE DRIVE on phone and computer.

6. Automatically send follow-up emails

You may not always receive a prompt response to your emails. To address this issue, you can send a follow-up email to remind email recipients. Instead of manually writing each follow-up, you can send it automatically using a script!

  1. Create New script From Google with the following code:
function sendFollowupEmails() { const startDate = new Date('2023-01-01'); const daysAgo = 3; var dateInPast = new Date(); dateInPast.setDate(dateInPast.getDate() - daysAgo); var threads = GmailApp.search("is:sent before:" + dateInPast.toLocaleDateString() + "after:" + startDate.toLocaleDateString()); for (var i = 0; i < threads.length; i++) { var conversation = threads[i]; var foundResponse = false; // If a sent thread has more than 1 email, we don't need to followup // because we either followed up already or they responded. if (conversation. getMessages(). length > 1) { foundResponse = true; } if (!foundResponse) { var body = "Hi, I just wanted to follow up on my previous email. If I get no reply I will assume you are not interested. \n\nBest regards,\n[Your Name]"; conversation.replyAll(body); } } }

2. Customize some things in this script:

  • change startDate To a time closer to the present day to not revive very old threads.
  • Change days: Go ahead and customize the age you want the email to be before sending a follow-up.
  • change [YourName] To your name.

3. Save it and set a trigger to run every seven days.

Continue sending emails as normal, and the script will automatically handle follow-ups.

Also read:  How to Install Custom Fonts on Kindle Paperwhite

7. Naming emails according to the subject keyword

If you're inundated with emails but don't want to delete most of them, you can use this script to categorize and organize them based on their content.

  1. Create New script From Google with the following code:
function categorizeEmails() { const emailsToGet = 50; var keywords = { "promo": "Promotions", "offer": "Promotions", "discount": "Promotions", "newsletter": "Newsletters", "action": "Todo", "required": "Todo", "shipped": "Orders", "delivered": "Orders", "receipt": "Orders", "invoice": "Orders" }; var threads = GmailApp. getInboxThreads(0, emailsToGet); for (var i = 0; i < threads.length; i++) { var subject = threads[i].getFirstMessageSubject().toLowerCase(); var labels = GmailApp. getUserLabels(); var foundLabel = false; for (var keyword in keywords) { if (subject.indexOf(keyword) !== -1) { for (var j = 0; j < labels.length; j++) { if (labels[j]. getName() === keywords[keyword]) { threads[i]. addLabel(labels[j]); foundLabel = true; break; } } if (!foundLabel) { GmailApp.createLabel(keywords[keyword]); threads[i]. addLabel(GmailApp. getUserLabelByName(keywords[keyword])); } break; } } } }

2. You can customize the number of emails affected by a variable. mailToGet Or keywords and labels using variable keywords.
3. Save Script and set Operator To run every few minutes or hours.
Automate Gmail: 7 Useful Google Scripts to Automate Gmail This allows you to stay on top of things by displaying only emails with a specific label. This script works well with most of your emails, saving you from simply deleting them.

8. Redirect some emails to multiple other addresses

While Gmail has a built-in way to forward an email to another email address, it can't easily forward an email with certain criteria to multiple email addresses, but this script can.

  1. Create New script With the following code:
function categorizeEmails() { const emailsToGet = 50; var keywords = { "promo": "Promotions", "offer": "Promotions", "discount": "Promotions", "newsletter": "Newsletters", "action": "Todo", "required": "Todo", "shipped": "Orders", "delivered": "Orders", "receipt": "Orders", "invoice": "Orders" }; var threads = GmailApp. getInboxThreads(0, emailsToGet); for (var i = 0; i < threads.length; i++) { var subject = threads[i].getFirstMessageSubject().toLowerCase(); var labels = GmailApp. getUserLabels(); var foundLabel = false; for (var keyword in keywords) { if (subject.indexOf(keyword) !== -1) { for (var j = 0; j < labels.length; j++) { if (labels[j]. getName() === keywords[keyword]) { threads[i]. addLabel(labels[j]); foundLabel = true; break; } } if (!foundLabel) { GmailApp.createLabel(keywords[keyword]); threads[i]. addLabel(GmailApp. getUserLabelByName(keywords[keyword])); } break; } } } }

2. Customize criteria as needed.
3. Create Operator It runs the script every few minutes or hours.

Gmail will automatically forward email that matches your criteria to two or more email addresses.

Frequently Asked Questions

Q1: Is Google Apps Script free to use?
The answer: Yes, Google Apps Script is free to use as long as you have a Gmail account. However, there are dozens of Quotas and restrictions Depending on your usage. Google may change these features at any time, but at the time of writing, they include approximately 40 features, such as document creation, email reading/writing, spreadsheet creation, and total uptime. Free accounts have a very low limit, while Google Workspace has a much higher limit.

Q2: Can I use Google Apps Script to access data from other services?
The answer: Yes, Google Apps Script isn't limited to working with Google services. You can access data from third-party, non-Google services on the Internet (for example, Pokémon API) using built-in classes like UrlFetchApp Like:

var response = UrlFetchApp.fetch("https://pokeapi.co/api/v2/pokemon/ditto");
Logger.log(response.getContentText()); // All details of Ditto would be logged

Q3: What do I do if I encounter errors in my Google Apps programming?
The answer: You can debug your code to identify and fix errors. Fortunately, Google Apps Script has a built-in debugger that you can use to troubleshoot and fix bugs in your code. To use it, simply open the script editor and click Debug. From there, you can set breakpoints, step through your code, and examine variables to help identify any issues.

Go to top button