How can you identify the senders who have emailed you the most in Gmail?
I have over 20,000+ Gmail messages from the last few years. When I was trying to free up space in my inbox, I ran into a limitation in the Gmail search filters. There is no way to find the top senders in your Gmail inbox.
Something like this
Sender || Number of emails
Sender1 || 2000
Sender2 || 500
Sender3 || 490
.. etc
Well, I found a solution. So read on
- You will need to create a new project here My Projects — Apps Script (google.com).
- In the code section, copy the below code
function sender_list() {
var max = 499;
var offset = 0; //change the offset value once the execution times out in Gmail
var row_set = 1;
// to avoid execution time overrun. Note the offset or batch number when the execution time ends. For next run change the starting value of offset.
var spreadsheet = SpreadsheetApp.create("Email_Stats_Top_sender_"+offset);
var sheet = spreadsheet.getSheetByName('sheet1');
sheet.appendRow(['Email Address','Count']);
while (true) {
Logger.log("Batch: " + offset);
var filter = "in:anywhere"
var inbox_threads = GmailApp.search(filter,offset,max);
if (inbox_threads.length < max) {
break;
}
var sender_array = [];
var uA=[];
var cObj={};
for (var i = 0; i < inbox_threads.length; i++) {
var message = inbox_threads[i].getMessages();
for (var x = 0; x < message.length; x++) {
var sender = message[x].getFrom();
//check for duplicates
if(uA.indexOf(sender)==-1) {
uA.push(sender);
sender_array.push([sender]);
cObj[sender]=1;
}else{
cObj[sender]+=1;
}
}
Logger.log("Done until Batch No. " + offset + " Thread No. " + i);
}
Logger.log("Operation completed successfully for Batch No. " + offset + " total " + inbox_threads.length);
sender_array.forEach(function(r){
r.splice(1,0,cObj[r[0]]);
});
//var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
//sheet.clear();
// sheet.getRange(2, 1,sender_array.length,2).setValues(sender_array).sort({column:2,ascending:false});
sheet.getRange(1+row_set, 1,sender_array.length,2).setValues(sender_array).sort({column:2,ascending:false});
Logger.log("spreadsheet created for Batch " + offset);
//start next batch
offset += max;
row_set += sender_array.length;
}
}
Then in the code area it looks like this:
3. After pasting, click the “Save” button. Give your project any name you want.
4. Click on "Run." Note: The Run button is greyed out until the project is successfully saved.
5. Monitor the execution log. Finally, a Google Spreadsheet named “Email_Stats_Top_sender_” will be created in the root folder of your Google Drive.
Great!
Notes
- If this is the first time you are running the script, you will need to give your account permissions to the script. Click “Continue” on the screen that pops up when you run the script (do not click “Go Back”) and choose to proceed to grant access.
- The script will go through your emails and counting the different messages from different senders. It then creates a Google Docs spreadsheet and files the message metadata.
- The script runs in batches of 500 email threads at a time as this is a restriction of Google’s pagination API. Therefore, the same sender may appear repeatedly in each batch in the created spreadsheet. You can easily create a filter in Google Sheets and sort in descending order to find the top senders. You can also create a pivot view like in the screenshot below to find your top email senders in each batch.
4. Unfortunately, the biggest limitation is that Google prevents the script from running for more than 6 minutes unless you have the paid Google Workspace tier. If, like me, you have a large number of emails, the execution will eventually time out.
Therefore, you can make the script more efficient by changing the filter variable in the code above. See below for an example. You can play with a variety of search operators to see which ones you like best. This is similar to the search operators used in the Gmail search box.
CHANGE
var filter = "in: anywhere";
TO
var filter = "in: anywhere has: attachment category: promotions"
Another alternative is to simply execute the code multiple times. In the execution log, note the offset value (batch number) at which execution timed out, and rerun your code after updating the offset start value. For example, in the screenshot above, the execution ended at batch number 7984. On my next run, I’ll start my offset variable at 7984.
##change this value to when execution timed out
var offset =7984; //changed from 0 on when execution timed out
I hope you find this useful.