Member update emails can carry one linked header image: a simple HTML part alongside the plain text (same paragraphs, footer, unsubscribe)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-10-03 16:19:49 -05:00
parent 067460c0e6
commit 7775ed87ef
3 changed files with 49 additions and 5 deletions
+13 -4
View File
@@ -43,7 +43,16 @@ let countsSnap = null, counting = false;
async function refreshCounts() { if (counting) return; counting = true; try { countsSnap = await counts(); } catch (e) {} finally { counting = false; } }
function countsSnapshot() { if (!countsSnap && !counting) refreshCounts().catch(() => {}); return countsSnap; }
function compose({ subject, intro, noteIds, closing }, acct) {
// the optional HTML twin of the plain text: one linked header image, then the same paragraphs (URLs clickable)
const escH = s => String(s == null ? '' : s).replace(/[&<>"]/g, c => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;' }[c]));
const okUrl = u => /^https:\/\/[^\s"'<>]+$/.test(String(u || ''));
function toHtml(parts, image, link) {
const para = t => '<p style="margin:0 0 16px">' + escH(t).replace(/https:\/\/[^\s<]+/g, u => '<a href="' + u + '" style="color:#6d28d9">' + u + '</a>').replace(/\n/g, '<br>') + '</p>';
const img = '<img src="' + escH(image) + '" alt="" width="560" style="display:block;width:100%;max-width:560px;height:auto;border:0;border-radius:10px;margin:0 0 20px">';
return '<!doctype html><html><body style="margin:0;padding:0;background:#ffffff"><div style="max-width:560px;margin:0 auto;padding:20px 16px;font:15px/1.55 Arial,Helvetica,sans-serif;color:#1f2430">'
+ (okUrl(link) ? '<a href="' + escH(link) + '">' + img + '</a>' : img) + parts.map(para).join('') + '</div></body></html>';
}
function compose({ subject, intro, noteIds, closing, image, imageLink }, acct) {
const notes = R.releases.notes().filter(n => noteIds.includes(n.id));
const name = acct && acct.username ? '@' + acct.username : 'there';
const parts = ['Hi ' + name + ','];
@@ -54,7 +63,7 @@ function compose({ subject, intro, noteIds, closing }, acct) {
parts.push('Sign in: ' + SITE + '/my');
parts.push('InstantAdPay\nAdvertise and earn instantly. Locked in code, not promises.\nNo income is guaranteed; results depend on your effort. Cryptocurrency carries risk.');
if (acct && acct.email) parts.push('Stop these update emails: ' + R.drip.unsubUrl(acct.email));
return { subject: String(subject || '').trim().slice(0, 150) || 'What is new on InstantAdPay', text: parts.join('\n\n') };
return { subject: String(subject || '').trim().slice(0, 150) || 'What is new on InstantAdPay', text: parts.join('\n\n'), html: okUrl(image) ? toHtml(parts, image, imageLink) : null };
}
let running = null;
@@ -67,7 +76,7 @@ async function send(input, opts) {
const to = R.adminEmail; if (!to) return { error: 'No admin email configured.' };
const acct = (await R.accounts.byEmail(to)) || { email: to, username: null };
const m = compose(input, acct);
await R.mailer.send(to, '[TEST] ' + m.subject, m.text);
await R.mailer.send(to, '[TEST] ' + m.subject, m.text, m.html);
return { ok: true, test: true, to };
}
if (running) return { error: 'A send is already running (' + running.sent + ' of ' + running.total + '). Wait for it to finish.' };
@@ -84,7 +93,7 @@ async function send(input, opts) {
try {
if (R.drip.isUnsubscribed && await R.drip.isUnsubscribed(acct.email)) { rec.skipped += 1; continue; }
const m = compose(input, acct);
await R.mailer.send(acct.email, m.subject, m.text); rec.sent += 1; (rec.delivered = rec.delivered || []).push(acct.email);
await R.mailer.send(acct.email, m.subject, m.text, m.html); rec.sent += 1; (rec.delivered = rec.delivered || []).push(acct.email);
} catch (e) { rec.failed += 1; console.error('updates send', acct.email, e.message); }
if ((rec.sent + rec.failed + rec.skipped) % 10 === 0) { const l2 = log(); const k = l2.sends.find(x => x.id === rec.id); if (k) Object.assign(k, rec); saveLog(l2); }
await new Promise(r => setTimeout(r, 150));