Home > Web Front-end > JS Tutorial > Apps Script - List all the newsletters filling up your Gmail. Selective unsubscribing got easy

Apps Script - List all the newsletters filling up your Gmail. Selective unsubscribing got easy

Linda Hamilton
Release: 2025-01-27 00:33:09
Original
194 people have browsed it

Apps Script - List all the newsletters filling up your Gmail. Selective unsubscribing got easy


Drowning in overflowing Gmail newsletters? While filtering for unsubscribe links is a known solution, this article tackles a more complex problem: efficiently managing a large number of existing newsletter subscriptions.

The Challenge

The sheer volume of subscribed newsletters can become overwhelming. Manually reviewing and unsubscribing from each one is tedious. The goal here wasn't to delete old emails, but to create a streamlined list of current, unique newsletter senders for targeted unsubscribing. Gmail itself lacks a simple solution.

The Solution: Google Apps Script

Google Apps Script provided the answer. This powerful tool lets you write code to interact with various Google services, automating repetitive tasks. With approximately 100 newsletter subscriptions across different platforms, this script proved invaluable in reducing the list to only essential subscriptions.

Here's a step-by-step guide to replicate this solution:

  1. Create a Google Sheet: This will store the generated list of newsletter senders.
  2. Access Apps Script: In your Google Sheet, navigate to "Extensions" > "Apps Script."
  3. Write the Script: The script below retrieves unique email addresses of newsletter senders from Gmail, along with their names, and neatly organizes them in the spreadsheet.
  4. Save and Run: Save the script, give it a descriptive name, and run the main function. Authorize the script to access your Gmail and Google Sheets.
  5. Review and Unsubscribe: The spreadsheet will populate with unique email addresses. Use this list to unsubscribe from unwanted newsletters within Gmail.

Script Breakdown:

The script comprises two main functions:

  • main(): This function calls _getUniqueMailingListSenders().
  • _getUniqueMailingListSenders(): This function takes three parameters:
    • searchOption: The Gmail search query (e.g., list:() for general newsletters).
    • title: The title to be added to the spreadsheet columns.
    • clearSheet: A boolean to determine whether to clear the existing sheet.

The function then:

  1. Retrieves all email threads matching the search criteria.
  2. Uses a JavaScript Set to collect unique sender email addresses.
  3. Extracts sender names using regular expressions.
  4. Stores email addresses and names in a Map.
  5. Dynamically adds columns to the Google Sheet, including a timestamp.
  6. Writes the unique sender data to the spreadsheet.
  7. Logs the process to the Apps Script execution log for debugging.

Complete Code:

<code class="language-javascript">function _getUniqueMailingListSenders(
  searchOption,
  title,
  clearSheet = true,
) {
  const threads = GmailApp.search(searchOption);
  const senders = new Set();
  threads.forEach(thread => {
    const messages = thread.getMessages();
    messages.forEach(message => {
      senders.add(message.getFrom());
    });
  });

  const emailData = new Map();
  senders.forEach((sender) => {
    const emailMatch = sender.match(/<([^>]+)>/);
    const email = emailMatch ? emailMatch[1] : sender;
    const name = sender.replace(emailMatch ? emailMatch[0] : '', '').trim();
    if (!emailData.has(email)) {
      emailData.set(email, name);
    }
  });

  const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  if (clearSheet) {
    sheet.clear();
  }
  const lastColumn = sheet.getLastColumn();
  const emailColumn = lastColumn + 1;
  const nameColumn = lastColumn + 2;
  const currentDate = new Date().toLocaleDateString();

  sheet.getRange(1, emailColumn).setValue(`${title} - Email - ${currentDate}`);
  sheet.getRange(1, nameColumn).setValue(`${title} - Name - ${currentDate}`);

  let row = 2;
  emailData.forEach((name, email) => {
    console.log(`Adding data on column (${emailColumn}, ${nameColumn}) and row ${row}: `, {email, name});
    sheet.getRange(row, emailColumn).setValue(email);
    sheet.getRange(row, nameColumn).setValue(name);
    row++;
  });
}

function main() {
  _getUniqueMailingListSenders(
    'list:()',
    'Unique Email Senders',
    false,
  );
}</code>
Copy after login

Conclusion

Google Apps Script offers a powerful solution for managing overwhelming email subscriptions. This script simplifies the process, allowing for efficient review and unsubscribing from unwanted newsletters. Explore Apps Script for other potential automation opportunities.

Connect with me on:

  • Website
  • LinkedIn
  • GitHub
  • X (Twitter)

The above is the detailed content of Apps Script - List all the newsletters filling up your Gmail. Selective unsubscribing got easy. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template