Mail: every non-transactional email carries an unsubscribe link and honours opt-outs
instantadpay.com landed on the Spamhaus DBL. Only the drip and the admin update tool had an unsubscribe link; the Wednesday/Friday broadcasts, coach nudges and digests, milestone, refill, reach, wallet nudges and sponsor notices had none, and several did not check opt-outs. The rule now lives in mailer.send, so new senders inherit it: skip anyone who unsubscribed, append the signed link (unless the body already has one) and add RFC 8058 one-click List-Unsubscribe headers. Exempt (transactional): sign-in codes, mail to ADMIN_EMAIL, purchase receipts and payout notices. /unsubscribe now accepts the one-click POST too. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -15,16 +15,36 @@ function key() {
|
||||
}
|
||||
function hasKey() { return !!key(); }
|
||||
|
||||
function send(to, subject, text, html) { // html optional (2026-10-01): sent alongside the plain text, never instead of it
|
||||
// Opt-out rule (2026-10-15, Spamhaus DBL listing): every email except sign-in
|
||||
// codes and mail to the admin is skipped for anyone who unsubscribed, carries
|
||||
// the signed unsubscribe link (appended unless the body already has one) and
|
||||
// RFC 8058 one-click List-Unsubscribe headers. New senders inherit this.
|
||||
function isAdmin(to) { const a = String(process.env.ADMIN_EMAIL || '').trim().toLowerCase(); return !!a && String(to).trim().toLowerCase() === a; }
|
||||
|
||||
async function send(to, subject, text, html, opts) { // html optional (2026-10-01): sent alongside the plain text, never instead of it
|
||||
const transactional = !!(opts && opts.transactional) || isAdmin(to);
|
||||
let unsub = '';
|
||||
if (!transactional) {
|
||||
const drip = require('./drip');
|
||||
try { if (await drip.isUnsubscribed(to)) return false; } catch (e) { console.error('mailer opt-out check', e.message); }
|
||||
try { unsub = drip.unsubUrl(to); } catch (e) { console.error('mailer unsub link', e.message); }
|
||||
}
|
||||
if (unsub && !String(text).includes('/unsubscribe?')) text = text + '\n\n--\nStop these emails: ' + unsub;
|
||||
if (unsub && html && !String(html).includes('/unsubscribe?')) {
|
||||
const foot = '<p style="font-size:12px;color:#888;margin-top:24px">Stop these emails: <a href="' + unsub + '" style="color:#888">unsubscribe</a></p>';
|
||||
html = /<\/body>/i.test(html) ? html.replace(/<\/body>/i, foot + '</body>') : html + foot;
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
const content = [{ type: 'text/plain', value: text }];
|
||||
if (html) content.push({ type: 'text/html', value: html });
|
||||
const body = JSON.stringify({
|
||||
const msg = {
|
||||
personalizations: [{ to: [{ email: to }] }],
|
||||
from: FROM,
|
||||
subject,
|
||||
content
|
||||
});
|
||||
};
|
||||
if (unsub) msg.headers = { 'List-Unsubscribe': '<' + unsub + '>', 'List-Unsubscribe-Post': 'List-Unsubscribe=One-Click' };
|
||||
const body = JSON.stringify(msg);
|
||||
const req = https.request({ hostname: 'api.sendgrid.com', path: '/v3/mail/send', method: 'POST',
|
||||
headers: { Authorization: 'Bearer ' + key(), 'Content-Type': 'application/json',
|
||||
'Content-Length': Buffer.byteLength(body) }, timeout: 15000 },
|
||||
@@ -43,7 +63,7 @@ function sendCode(to, code) {
|
||||
return send(to, code + ' is your InstantAdPay sign-in code',
|
||||
'Your sign-in code is: ' + code + '\n\n'
|
||||
+ 'It works for 15 minutes. If you did not request it, ignore this email.\n\n'
|
||||
+ 'InstantAdPay\nAdvertise and earn instantly. Locked in code, not promises.\nhttps://instantadpay.com');
|
||||
+ 'InstantAdPay\nAdvertise and earn instantly. Locked in code, not promises.\nhttps://instantadpay.com', null, { transactional: true });
|
||||
}
|
||||
|
||||
module.exports = { init, hasKey, send, sendCode };
|
||||
|
||||
@@ -1065,7 +1065,7 @@ async function emailOnEvent(ev) {
|
||||
const notify = async (memberId, subject, body) => {
|
||||
if (!memberId || !mailer.hasKey()) return;
|
||||
const a = await accounts.byMemberId(memberId);
|
||||
if (a && a.email) mailer.send(a.email, subject, body + '\n\nSee it on the live ledger: https://instantadpay.com/ledger\n\nInstantAdPay').catch(() => {});
|
||||
if (a && a.email) mailer.send(a.email, subject, body + '\n\nSee it on the live ledger: https://instantadpay.com/ledger\n\nInstantAdPay', null, { transactional: true }).catch(() => {}); // receipts + payouts: sent even after an unsubscribe
|
||||
};
|
||||
// Marty (2026-09-15): payment and missed-payment notices also land in the on-site inbox, so
|
||||
// they are waiting (login modal + Messages card) whether or not the email was read. Sent as
|
||||
@@ -1831,9 +1831,9 @@ const server = http.createServer(async (req, res) => {
|
||||
const set = ['iap.sponsor=; Path=/; SameSite=Lax; Max-Age=0' + (IS_PROD ? '; Secure' : ''), 'iap.angle=' + key + cookieTail, 'iap.ref=' + encodeURIComponent('legacy:' + brand + (src ? ':' + src : '')) + cookieTail];
|
||||
return serveJoinPage(res, '', key, JOIN_ANGLES[key], set);
|
||||
}
|
||||
if (p === '/unsubscribe' && req.method === 'GET') {
|
||||
if (p === '/unsubscribe' && (req.method === 'GET' || req.method === 'POST')) { // POST = RFC 8058 one-click from the mail client
|
||||
const r = await drip.unsubscribe(u.searchParams.get('e'), u.searchParams.get('t'));
|
||||
const msg = r.error ? r.error : 'Done. You will not get any more follow-up emails from InstantAdPay. Your account is unchanged.';
|
||||
const msg = r.error ? r.error : 'Done. You will not get any more emails from InstantAdPay, apart from sign-in codes you ask for and receipts for payments to or from your account. Your account is unchanged.';
|
||||
res.writeHead(r.error ? 400 : 200, baseHeaders({ 'Content-Type': 'text/html; charset=utf-8', 'Cache-Control': 'no-store' }));
|
||||
return res.end('<!doctype html><html><head><meta charset="utf-8"><title>InstantAdPay</title><link rel="stylesheet" href="/assets/site.css?v=20260909a"></head><body><div class="wrap" style="max-width:560px;padding:80px 22px"><a href="/"><img src="/logo.png" alt="InstantAdPay" style="height:34px"></a><h1 style="font-size:30px;margin:26px 0 12px">' + (r.error ? 'Hmm.' : 'Unsubscribed.') + '</h1><p>' + msg + '</p><p><a class="btn small sec" href="/my">Member area</a></p></div></body></html>');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user