← back to Purelymail Fleet Viewer

run-send-test.js

63 lines

#!/usr/bin/env node
/* Purelymail 206-domain SEND test.
 * Sends one tagged email AS info@<domain> for every fleet domain, via one
 * Purelymail SMTP login (info@glitterwalls.com) using cross-domain send-as.
 * Collector inbox: theagentabrams@gmail.com — arrival there proves info@<domain>
 * can send. ~3s apart, logs every send, updates the viewer status file live.
 *
 *   PM_SMTP_PASS=... node run-send-test.js
 */
'use strict';
const fs = require('fs');
const os = require('os');
const path = require('path');
const { sendAs, closeTransport } = require(path.join(os.homedir(),
  'Projects/email-deliverability-agent/lib/fleet-send.js'));

const HEALTH = path.join(os.homedir(), 'Projects/email-deliverability-agent/output/domain-health.json');
const STATUS = path.join(__dirname, 'data/remediation-status.json');
const SENDS  = path.join(__dirname, 'data/send-test-sends.jsonl');
const RUNID_F= path.join(__dirname, 'data/send-test-runid.txt');
const COLLECTOR = 'theagentabrams@gmail.com';

if (!process.env.PM_SMTP_PASS) { console.error('PM_SMTP_PASS not set'); process.exit(1); }

const runId = 'PMSEND-' + new Date().toISOString().replace(/[-:T.]/g, '').slice(0, 14);
const domains = JSON.parse(fs.readFileSync(HEALTH, 'utf8')).domains.map(d => d.domain);
fs.writeFileSync(SENDS, '');
fs.writeFileSync(RUNID_F, runId);

function patch(fields) {
  try {
    const s = JSON.parse(fs.readFileSync(STATUS, 'utf8'));
    s.sendTest = Object.assign({ label: 'SEND test — info@<domain> via Purelymail send-as', total: domains.length }, s.sendTest, fields);
    s.sendTest.runId = runId;
    s.updatedAt = new Date().toISOString();
    fs.writeFileSync(STATUS, JSON.stringify(s, null, 2));
  } catch (e) { /* non-fatal */ }
}
const sleep = ms => new Promise(r => setTimeout(r, ms));

(async () => {
  patch({ status: 'running', done: 0, note: 'starting send test' });
  let sent = 0, okCount = 0;
  for (const domain of domains) {
    const subject = `PMSEND ${runId} ${domain}`;
    const html = `<p>Purelymail SEND test. run=${runId} domain=${domain}. ` +
      `This was sent AS info@${domain} via Purelymail send-as. Arrival here proves info@${domain} can send.</p>`;
    const r = await sendAs(domain, { to: COLLECTOR, subject, html });
    if (r.ok) okCount++;
    fs.appendFileSync(SENDS, JSON.stringify({ domain, ok: r.ok, messageId: r.messageId || null, error: r.error || null }) + '\n');
    sent++;
    if (sent % 5 === 0 || sent === domains.length) {
      patch({ done: sent, note: `sending — ${sent}/${domains.length} (${okCount} accepted by SMTP)` });
    }
    await sleep(3000);
  }
  closeTransport();
  patch({ done: sent, note: `${sent} sent (${okCount} SMTP-accepted) — waiting 3 min for delivery`, smtpAccepted: okCount });
  await sleep(180000);
  patch({ note: `${sent} sent — delivery window elapsed, ready for collector-inbox tabulation`, sendPhaseComplete: true });
  console.log(`send-test complete: ${sent} sent, ${okCount} SMTP-accepted, runId=${runId}`);
})();