// Outbound mail via SendGrid v3 (domain-authenticated instantadpay.com; links in bodies go out on the share domain, see mailLinks). // Key sources: SENDGRID_KEY env, else DATA_DIR/sendgrid.key in the volume. // No key = email sign-in stays feature-flagged off in production. const fs = require('fs'); const path = require('path'); const https = require('https'); let DATA_DIR = null; const FROM = { email: 'no-reply@instantadpay.com', name: 'InstantAdPay' }; function init(opts) { DATA_DIR = opts.dataDir; } function key() { if (process.env.SENDGRID_KEY) return process.env.SENDGRID_KEY.trim(); try { return fs.readFileSync(path.join(DATA_DIR, 'sendgrid.key'), 'utf8').trim(); } catch (e) { return ''; } } function hasKey() { return !!key(); } // 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. // Links in mail go on the share domain (2026-11-17, Spamhaus DBL): instantadpay.com is on the DBL, so every URL to it in a body // (and the List-Unsubscribe header) counts against each email. One rewrite here covers every sender; the share host passes // /my, /ledger, /join/, ... through to the same page and answers /unsubscribe and /banners/* itself. The From address // and any name@instantadpay.com in the text stay as they are. MAIL_BASE='' turns it off. const MAIL_BASE = String(process.env.MAIL_BASE == null ? 'https://instantadpay.link' : process.env.MAIL_BASE).trim().replace(/\/+$/, ''); const IAP_LINK_RE = /(? /^https?:/i.test(m) ? MAIL_BASE : bare); } 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); } } unsub = mailLinks(unsub); text = mailLinks(text); if (html) html = mailLinks(html); subject = mailLinks(subject); if (unsub && !String(text).includes('/unsubscribe?')) text = text + '\n\n--\nStop these emails: ' + unsub; if (unsub && html && !String(html).includes('/unsubscribe?')) { const foot = '

Stop these emails: unsubscribe

'; html = /<\/body>/i.test(html) ? html.replace(/<\/body>/i, foot + '') : html + foot; } return new Promise((resolve, reject) => { const content = [{ type: 'text/plain', value: text }]; if (html) content.push({ type: 'text/html', value: html }); const msg = { personalizations: [{ to: [{ email: to }] }], from: opts && opts.fromName ? { email: FROM.email, name: String(opts.fromName).slice(0, 60) } : FROM, // same authenticated address; another network site's name on mail sent for it (siteinvite.js code mail, 2026-11-21) subject, content }; if (unsub) msg.headers = { 'List-Unsubscribe': '<' + unsub + '>', 'List-Unsubscribe-Post': 'List-Unsubscribe=One-Click' }; if (opts && opts.replyTo) msg.reply_to = { email: String(opts.replyTo) }; // help desk: replies thread back into the ticket if (opts && opts.headers) msg.headers = Object.assign(msg.headers || {}, opts.headers); 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 }, res => { let d = ''; res.on('data', c => d += c); res.on('end', () => res.statusCode < 300 ? resolve(true) : reject(new Error('sendgrid ' + res.statusCode + ': ' + d.slice(0, 200)))); }); req.on('error', reject); req.on('timeout', () => req.destroy(new Error('sendgrid timeout'))); req.end(body); }); } 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', null, { transactional: true }); } module.exports = { init, hasKey, send, sendCode, mailLinks };