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:
main
function. Authorize the script to access your Gmail and Google Sheets.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:
Set
to collect unique sender email addresses.Map
.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>
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:
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!