← back to Ventura Claw Leads

lib/email.js

54 lines

// VCL email — direct Purelymail SMTP via nodemailer (lifted from NPH).
// Mailbox: info@venturaclaw.com (Purelymail). Password lives in /secrets as
// INFO_VENTURACLAW_COM_PASSWORD; fans out to VCL .env on sync.
// In dev, falls back to console logging when SMTP creds are absent.

const nodemailer = require('nodemailer');

const SMTP_HOST = process.env.VCL_SMTP_HOST || process.env.NPH_SMTP_HOST || 'smtp.purelymail.com';
const SMTP_PORT = parseInt(process.env.VCL_SMTP_PORT || process.env.NPH_SMTP_PORT || '587', 10);
const SMTP_USER = process.env.VCL_SMTP_USER || process.env.EMAIL_FROM || 'info@venturaclaw.com';
const SMTP_PASS = process.env.VCL_SMTP_PASS || process.env.INFO_VENTURACLAW_COM_PASSWORD || '';
const FROM = process.env.EMAIL_FROM || 'info@venturaclaw.com';
const FROM_NAME = process.env.EMAIL_FROM_NAME || 'Ventura Claw';

let _transport = null;
function getTransport() {
  if (_transport) return _transport;
  if (!SMTP_PASS) return null;
  _transport = nodemailer.createTransport({
    host: SMTP_HOST,
    port: SMTP_PORT,
    secure: SMTP_PORT === 465,
    requireTLS: SMTP_PORT === 587,
    auth: { user: SMTP_USER, pass: SMTP_PASS }
  });
  return _transport;
}

async function sendEmail({ to, subject, html, text, replyTo, extraHeaders }) {
  const transport = getTransport();
  if (!transport) {
    console.warn('[email] no SMTP creds (VCL_SMTP_PASS / INFO_VENTURACLAW_COM_PASSWORD unset); logging instead');
    console.log('--- EMAIL ---\nto:', to, '\nfrom:', FROM, '\nreply-to:', replyTo || '(none)', '\nsubject:', subject, '\nbody:\n', (html || text || '').slice(0, 500), '\n-------------');
    return { ok: false, mocked: true };
  }
  try {
    const info = await transport.sendMail({
      from: { name: FROM_NAME, address: FROM },
      to,
      replyTo: replyTo || undefined,   // crucial for lead-delivery: business hits Reply → consumer gets it
      subject,
      html: html || undefined,
      text: text || undefined,
      headers: (extraHeaders && typeof extraHeaders === 'object') ? extraHeaders : undefined
    });
    return { ok: true, messageId: info.messageId, accepted: info.accepted, rejected: info.rejected };
  } catch (err) {
    console.warn('[email] smtp error:', err.code || err.message);
    return { ok: false, error: err.message, code: err.code };
  }
}

module.exports = { sendEmail };