Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Gmail categories

I used to have a working apps script that deleted old promotional emails in my Gmail account.

It used the code

var label = GmailApp.getUserLabelByName("Promotions");

to get the label and then it iterated through label.getThreads() to decide whether each thread was old enough to delete.

However, Google have now changed Gmail so the auto categorisations are now under the Categories section in the UI, rather than within the list of labels so the above now returns null.

How can I fix my code to retrieve the Promotions category? I have tried Categories/Promotions too but it also comes up null.

like image 916
SilverlightFox Avatar asked Feb 02 '14 10:02

SilverlightFox


People also ask

What are the 5 categories of Gmail?

Gmail allows you to separate your Inbox into several categories that appear as tabs. These categories are Primary, Social, Updates, Forums, and Promotions.

Can I Categorise emails in Gmail?

In Gmail, you use labels to categorize your email. Labels are like folders, but with a twist—you can apply several labels to an email, then later find the email by clicking any of its labels from the left panel. You can also search by label.


3 Answers

Gmail categories can easily be searched.

Here is a small code that looks for every promotion mail. The result is an array of threads, you can add Label to each of them so that your old script will be happy again ;-)

  var threads = GmailApp.search('category:promotions');// check the category Gmail added to the thread

documentation here

like image 119
Serge insas Avatar answered Sep 28 '22 19:09

Serge insas


Here is a script I use to delete old promotional emails as well as some other categories and custom labels.

function auto_delete_email(){
  delete_Label ("Cameras",30);
  delete_Label ("Travel",365);
  delete_Category ("Social",90);
  delete_Category ("Finance",365*3);
  delete_Category ("Forums",90);
  delete_Category ("Promos",365*3);
}

function delete_Label(mailLabel,delayDays) {  
  var label = GmailApp.getUserLabelByName(mailLabel);   
  if (!label) {return false;}
  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();
    } 
  } 

function delete_Category(mailCategory,delayDays) {  
  var maxDate = new Date(); 
  maxDate.setDate(maxDate.getDate()-delayDays);    
  var threads = GmailApp.search('category:' + mailCategory);
  for (var i = 0; i < threads.length; i++) {  
    if (threads[i].getLastMessageDate()<maxDate){  
      threads[i].moveToTrash();
    } 
  } 
} 
like image 30
Matt Folsom Avatar answered Sep 28 '22 18:09

Matt Folsom


You can also expand the search criteria to use (d, m, y) like "older_than:1y" which mimics the search box functionality in the Gmail app. This may be helpful as I've had significant issues getting the date comparison to work smoothly.

For example:

function delete_old_Category() {  
  var maxDate = new Date(); 
  maxDate.setDate(maxDate.getDate()- 180);    
  var threads = GmailApp.search("category:promos older_than:6m",0, 100);
  for (var i = 0; i < threads.length; i++) {  
     threads[i].moveToTrash(); {  
  
    } 
  } 
} 
like image 37
dsugasa Avatar answered Sep 28 '22 19:09

dsugasa