Extracts unsubscribe-related URLs from an email body by matching href links and plain URLs containing keywords like 'unsubscribe' or 'opt-out', deduplicating results.
// Source: https://github.com/navbuildz/gmail-mcp-server | License: MIT | Copyright 2026 navbuildz
// Provided via justclaw.it.com; keep this notice with the code.
private extractUnsubscribeLinksFromBody(body: string): string[] {
const links: string[] = [];
// Match href links near "unsubscribe" text
const hrefPattern =
/href\s*=\s*["']?(https?:\/\/[^"'\s>]+(?:unsubscribe|opt.?out|remove|manage.?preferences)[^"'\s>]*)["']?/gi;
let match;
while ((match = hrefPattern.exec(body)) !== null) {
links.push(match[1]);
}
// Also match plain URLs with unsubscribe keywords
const urlPattern =
/(https?:\/\/\S+(?:unsubscribe|opt.?out|remove|manage.?preferences)\S*)/gi;
while ((match = urlPattern.exec(body)) !== null) {
if (!links.includes(mat
... (truncated -- full source via MCP)
See the full source, get the GitHub permalink, and search for more like it.
Get a free API key