← back to Email Deliverability Agent

lib/fleet-send.js

54 lines

'use strict';
/**
 * fleet-send.js — send email AS info@<any owned domain> via one Purelymail login.
 *
 * Purelymail lets an authenticated mailbox send from any address on any domain
 * verified in the account. So a single SMTP credential (info@glitterwalls.com)
 * can send "as" info@<domain> for all 206 fleet domains — no per-domain mailbox.
 *
 * The sending domain still gets a valid DKIM signature (Purelymail signs on the
 * From: domain) and SPF passes (every fleet domain includes _spf.purelymail.com).
 */
const nodemailer = require('nodemailer');

const SMTP_USER = process.env.PM_SMTP_USER || 'info@glitterwalls.com';
const SMTP_PASS = process.env.PM_SMTP_PASS || '';

let transport;
function getTransport() {
  if (!SMTP_PASS) throw new Error('PM_SMTP_PASS not set');
  if (!transport) {
    transport = nodemailer.createTransport({
      host: 'smtp.purelymail.com',
      port: 465,
      secure: true,
      auth: { user: SMTP_USER, pass: SMTP_PASS },
      pool: true,
      maxConnections: 1,
    });
  }
  return transport;
}

/**
 * Send a message as info@<domain>.
 * @returns {Promise<{ok:boolean, messageId?:string, response?:string, error?:string}>}
 */
async function sendAs(domain, { to, subject, html, fromName = 'Designer Wallcoverings' }) {
  try {
    const info = await getTransport().sendMail({
      from: `"${fromName}" <info@${domain}>`,
      to,
      subject,
      html,
    });
    return { ok: true, messageId: info.messageId, response: info.response };
  } catch (e) {
    return { ok: false, error: String(e && e.message || e) };
  }
}

function closeTransport() { if (transport) transport.close(); }

module.exports = { sendAs, closeTransport, SMTP_USER };