Skip to main content

Gmail Automation

Gmail Automation: 5 Useful Google Scripts to Automate Your Gmail



Very often, after we read the email, we will just keep it in our inbox, regardless whether it is useful or not. While Google gives you tons of space to store your emails, you might still want to clean up your inbox and get rid of those useless emails. The following script can check emails with the “Delete Me” label and delete them after “x” number of days.
1. Go to Google Scripts and create a blank project (make sure you are logged into your Google account).
google-script-blank-project
Paste the following script and save it.
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()<maxDate){  
        threads[i].moveToTrash();
      } 
    } 
  }
}
You can change the number of days (under delayDays) to pass before it deletes that email from your inbox. Set a trigger (Resources -> Current Project's Triggers -> Add one now) to run it daily.
google-script-set-trigger
Once activated, it will create a label "Delete Me" in your Gmail account. All you have to do is to tag the unwanted emails with this label and they will be deleted after the expiry day (as set in delayDays.
Sometime, after reading an email, you want it to return to your inbox after a few days. With the following Googlescript, you can do so:
1. Create a new Google script with the following code:
var MARK_UNREAD = true;
var ADD_UNSNOOZED_LABEL = false;
 
function getLabelName(i) {
  return "Snooze/Snooze " + i + " days";
}
 
function setup() {
  // Create the labels we’ll need for snoozing
  GmailApp.createLabel("Snooze");
  for (var i = 1; i <= 7; ++i) {
    GmailApp.createLabel(getLabelName(i));
  }
  if (ADD_UNSNOOZED_LABEL) {
    GmailApp.createLabel("Unsnoozed");
  }
}
 
function moveSnoozes() {
  var oldLabel, newLabel, page;
  for (var i = 1; i <= 7; ++i) {
    newLabel = oldLabel;
    oldLabel = GmailApp.getUserLabelByName(getLabelName(i));
    page = null;
    // Get threads in "pages" of 100 at a time
    while(!page || page.length == 100) {
      page = oldLabel.getThreads(0, 100);
      if (page.length > 0) {
        if (newLabel) {
          // Move the threads into "today’s" label
          newLabel.addToThreads(page);
        } else {
          // Unless it’s time to unsnooze it
          GmailApp.moveThreadsToInbox(page);
          if (MARK_UNREAD) {
            GmailApp.markThreadsUnread(page);
          }
          if (ADD_UNSNOOZED_LABEL) {
            GmailApp.getUserLabelByName("Unsnoozed")
              .addToThreads(page);
          }          
        }     
        // Move the threads out of "yesterday’s" label
        oldLabel.removeFromThreads(page);
      }  
    }
  }
}
Next, save it and run the "Setup" function. This will add several new label to your Gmails (such as "Snooze for 2 days", "Snooze for 7 days" etc.) Lastly, just add a trigger for "moveSnoozes" to run everyday. Now, emails marked with the "Snooze" label will return to the inbox with unread status after the number of days have passed. (viaGmail blog)
This Google script make use of the Google Calendar's SMS feature to send you SMS for important emails.
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 set a trigger for it to run every 5 minutes.
3. Lastly, you have to set a filter to add the "Send Text" label to all important incoming emails. The script will scan your inbox every 5 minutes and when it detects an email with the "Send Text" label, it will create an immediate event in Google Calender which will then trigger the SMS.

4. Schedule email to send at a later date

Boomerang is one web service that you can use to schedule emails to send at a later date, but that requires you to install a browser extension. Gmail Delay Send is a Google Script that can do the same task.
1. Go to this link and click the "Install" link. Once you have authorized the script to access your Gmail, it will redirect you to another page where you can configure the script.
google-script-gmail-delay-send-options
2. Once configured, you can then proceed to draft an email and include the future date/time for it to send and save it as draft with the "GmailDelaySend/ToSend" label.
If you have an email that you want to archive in Google Drive, you can use Google script to save it as PDF in your Google Drive account. The following script will save all the messages in an email thread as one PDF file in your Google Drive. If it comes with attachments, it will create a folder and store the messages and attachments within.
1. Create a new Google script with the following code:
function save_Gmail_as_PDF(){
  var label = GmailApp.getUserLabelByName("Save As PDF");  
  if(label == null){
    GmailApp.createLabel('Save As PDF');
  }
  else{
    var threads = label.getThreads();  
    for (var i = 0; i < threads.length; i++) {  
      var messages = threads[i].getMessages();  
      var message = messages[0];
      var body    = message.getBody();
      var subject = message.getSubject();
      var attachments  = message.getAttachments();
      for(var j = 1;j<messages.length;j++){
        body += messages[j].getBody();
        var temp_attach = messages[j].getAttachments();
        if(temp_attach.length>0){
          for(var k =0;k<temp_attach.length;k++){
            attachments.push(temp_attach[k]);
          }
        }
      } 
      // Create an HTML File from the Message Body
      var bodydochtml = DocsList.createFile(subject+'.html', body, "text/html")
      var bodyId=bodydochtml.getId();
 
      // Convert the HTML to PDF
      var bodydocpdf = bodydochtml.getAs('application/pdf');
      if(attachments.length > 0){
        DocsList.createFolder(subject);
        var folder = DocsList.getFolder(subject);
        for (var j = 0; j < attachments.length; j++) {
          folder.createFile(attachments[j]);
          Utilities.sleep(1000);
        }
        folder.createFile(bodydocpdf);
      }
      else{
        DocsList.createFile(bodydocpdf);
      }      
      DocsList.getFileById(bodyId).setTrashed(true);
      label.removeFromThread(threads[i]);
    }
  }  
}

Comments

Popular posts from this blog

The Best Web Hosting Services

Are you looking for the best web  hosting  services for your needs? Whether you need a place to host your small personal blog or a major corporate website, the following list will help you identify the best hosts to use. Finding the best web hosting service isn’t quite as straightforward as searching Google and choosing the one with the lowest price. There are a lot of issues to consider, including the reasons for  why  you need hosting and  how  you intend to use it. Once you have a handle on that, finding the right host becomes much easier. Choose one that’s undersized and you’ll end up with website outages and slow page loads, but choose one that’s oversized and you’ll be throwing money away. Defining Your Web Hosting Needs Before choosing your web host, you’ll need to think about your requirements. Consider the following concerns and decide the importance of each item on a scale of 0 to 10 (with 0 being not at all...

Now You Can Use Reliance Jio 4G Services On 2G And 3G Smartphones

Indians will always be at the top in availing any free internet facility. As, Reliance Jio aims to offer free 4G internet to the 90% of Indians, hence, with one of its services now you can use Jio 4G services on 2G and 3G smartphones. Now You Can Use Reliance Jio 4G Services On 2G And 3G Smartphones Who doesn’t want the free internet? Of course, we all want, Indians will always be at the top in availing any free internet facility. Jio, which is also known as Reliance Jio and officially as Reliance Jio Infocomm Limited has already given its users free unlimited 4G data for 90 days. As the Reliance Jio aims to offer free 4G internet to the 90% of Indians along with the free voice calls and messaging services. So, we all must agree that Indians are always at the peak when it comes about available any internet facility. We all know Jio, which is also known as Reliance Jio Infocomm Limited has previously given its users free unlimited 4G data for 90 days. Not only that but even th...

10 Essential Tips To Keep In Mind While Surfing The Internet

BENGALURU: Internet is an amazing resource which is stapled in many people’s day-to-day lives. It is very much informative but at the same time encircled with many dangers. Listed below are the strategies you can follow to stay safe on the internet as stated by Tech Radar India. Use your discretion on social media Online scams crops up almost everywhere in webpage such as an e-mail, tweet, Facebook post, or many other places. Never click on links that do not look like a real address or pop-ups that claim you have won millions of dollars—all these are scams in which one can easily get trapped. Also don’t fall prey to e-mails which ask you to help someone transfer a large amount of money out of their country delineating their long sad story. Hackers can access data through various innovative ways like sharing links of content that they feel the targeted person is likely to click. Be careful of what you are sharing Limit the content that you share on social media. Facebook...