Automatically Unsubscribe from Newsletters in Gmail

The bulk email messages in Gmail have an “unsubscribe” link that you can click to remove your email address from the mailing list. However, if you wish to unsubscribe from multiple email newsletters in one go, you can use the Gmail Unsubscriber script.

Apply the label “Unsubscribe” to all the emails from which you wish to unsubscribe and call this apps script. It extracts the unsubscribe link from the raw message header and fetches the link to unsubscribe you. Press Cmd+Enter to see the list of all mailing lists from which you have been unsubscribed.

Joshua Peak has done the groundwork but it only works if the email message contains the unsubscribe link in the List Unsubscribe header.

function main() {

  var label = GmailApp.getUserLabelByName("Unsubscribe");
  var threads = label.getThreads();

  threads.forEach(function(thread) {

    var message = thread.getMessages()[0];
    var value = message.getRawContent()
                       .match(/^List-Unsubscribe: ((.|\r\n\s)+)\r\n/m)[1];

    if (value) {
      var url = value.match(/<(https?:\/\/[^>]+)>/)[1];
      if (url) {
        var status = UrlFetchApp.fetch(url).getResponseCode();
        Logger.log("Unsubscribe " + status + " " + url);
      }
    }

    thread.removeLabel(label);
  });
}

I extended this to unsubscribe from mailing lists where the link may be in the message body or messages that may require your to unsubscribe by sending an email to a specific email address.


function Gmail_Unsubscribe() {
  
  var threads = GmailApp.search("label:Unsubscribe");
  
  for (var t in threads)  {
    
    var message = threads[t].getMessages()[0];
    
    var raw = message.getRawContent();
    
    // Search for the List Unsubscribe header in the Email Header
    var urls = raw.match(/^list\-unsubscribe:(.|\r\n\s)+<(https?:\/\/[^>]+)>/im);

    // thanks josh/list-unsubscribe @github

    if (urls) {
      
      // Click the unsubscribe link
      UrlFetchApp.fetch(urls[2], {muteHttpExceptions: true});
      
    } else {
      
      // Find the unsubscribe email
      urls = raw.match(/^list\-unsubscribe:(.|\r\n\s)+<mailto:([^>]+)>/im);
      
      if (urls) {
        
        // Send blank email to unsubscribe
        GmailApp.sendEmail(urls[2], "Unsubscribe", "Unsubscribe");
        
      } else {
        
        // Get the HTML of the email 
        var body = message.getBody().replace(/\s/g, "");
        
        // Regex to find all hyperlinks
        var hrefs = new RegExp(/<a[^>]*href=["'](https?:\/\/[^"']+)["'][^>]*>(.*?)<\/a>/gi);
        
        // Iterate through all hyperlinks inside the message
        while ( urls = hrefs.exec(body) ) {
          
          // Does the anchor text or hyperlink contain words like unusbcribe or optout
          if (urls[1].match(/unsubscribe|optout|opt\-out|remove/i)
              || urls[2].match(/unsubscribe|optout|opt\-out|remove/i)) {
            
            // Click the unsubscribe link
            UrlFetchApp.fetch(urls[1], {muteHttpExceptions: true});
            break;
            
          }
        }
      } 
    }
  }
}