Gmail Auto Unsubscriber Script

JavaScript

 
function autoUnsubscribe() {
  // Ye script 'Unsubscribe' label wale emails dhundhegi
  var labelName = 'Unsubscribe';
  var label = GmailApp.getUserLabelByName(labelName);
  
  if (!label) return; // Agar label nahi hai toh ruk jao
  
  var threads = label.getThreads();
  
  for (var i = 0; i < threads.length; i++) {
    var message = threads[i].getMessages()[0];
    var rawContent = message.getRawContent();
    
    // Background code se "List-Unsubscribe" URL extract karna
    var match = rawContent.match(/List-Unsubscribe:.*?(https?:\/\/[^>,\s]+)/i);
    
    if (match && match[1]) {
      var unsubscribeUrl = match[1];
      try {
        // HTTP GET request bhejna (Bina browser open kiye)
        UrlFetchApp.fetch(unsubscribeUrl, { muteHttpExceptions: true });
      } catch (e) {
        Logger.log("Failed to hit URL: " + unsubscribeUrl);
      }
    }
    
    // Unsubscribe hone ke baad email ko Trash mein daal dena
    threads[i].removeLabel(label);
    threads[i].moveToTrash();
  }
}

Avatar

Abid Akay

CEO / Co-Founder

The smallest daily habits create the biggest changes in the long run.

Cookie
We care about your data and would love to use cookies to improve your experience.