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
+1 -1
View File
@@ -2337,7 +2337,7 @@ const server = http.createServer(async (req, res) => {
if (p === '/api/admin/updates/send' && req.method === 'POST') {
if (!isAdmin(req)) return json(res, 401, { error: 'auth' });
const b = await readBody(req);
const r = await updates.send({ subject: b.subject, intro: b.intro, closing: b.closing, noteIds: (b.noteIds || []).map(String), audience: b.audience, to: Array.isArray(b.to) ? b.to.slice(0, 5000) : null }, { test: !!b.test });
const r = await updates.send({ subject: b.subject, intro: b.intro, closing: b.closing, image: b.image, imageLink: b.imageLink, noteIds: (b.noteIds || []).map(String), audience: b.audience, to: Array.isArray(b.to) ? b.to.slice(0, 5000) : null }, { test: !!b.test });
return json(res, r.error ? 400 : 200, r);
}
if (p === '/api/admin/releases' && req.method === 'GET') {
+35
View File
@@ -0,0 +1,35 @@
# Member update emails can carry ONE header image (Marty, 2026-10-03: the Spooky Doors announcement with its share image).
# An optional https image (+ link) adds a simple HTML part next to the plain text, never instead of it: the image on
# top (linked), the same paragraphs, the same footer and unsubscribe link. No template, so deliverability stays plain.
ROOT = 'D:/Projects/HighRisk/onchain-ad-membership/site/'
def edit(path, pairs):
b = open(ROOT + path, 'rb').read(); crlf = b.count(b'\r\n') > b.count(b'\n') // 2
s = b.decode('utf-8').replace('\r\n', '\n')
for a, r in pairs:
if s.count(a) != 1: raise SystemExit('%s anchor %d: %s' % (path, s.count(a), a[:70]))
s = s.replace(a, r)
if crlf: s = s.replace('\n', '\r\n')
open(ROOT + path, 'wb').write(s.encode('utf-8')); print('patched', path)
HTML = r"""// 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>';
}
"""
edit('updates.js', [
("function compose({ subject, intro, noteIds, closing }, acct) {", HTML + "function compose({ subject, intro, noteIds, closing, image, imageLink }, acct) {"),
(" 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 };"),
(" await R.mailer.send(to, '[TEST] ' + m.subject, m.text);", " await R.mailer.send(to, '[TEST] ' + m.subject, m.text, m.html);"),
(" await R.mailer.send(acct.email, m.subject, m.text); rec.sent += 1;", " await R.mailer.send(acct.email, m.subject, m.text, m.html); rec.sent += 1;"),
])
edit('server.js', [
("const r = await updates.send({ subject: b.subject, intro: b.intro, closing: b.closing, noteIds: (b.noteIds || []).map(String), audience: b.audience,",
"const r = await updates.send({ subject: b.subject, intro: b.intro, closing: b.closing, image: b.image, imageLink: b.imageLink, noteIds: (b.noteIds || []).map(String), audience: b.audience,"),
])
print('ok')
+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));