← back to Cypress Awards

backend/send_status_email.js

96 lines

// Send server status email using Node.js
// This avoids needing sendmail/postfix installed

const nodemailer = require('nodemailer');
const fs = require('fs');
const path = require('path');

async function sendStatusEmail(status, details) {
  const emailContent = `
CyPresAwards Server Status Report
==================================
Date: ${new Date().toLocaleString('en-US', { timeZone: 'UTC' })} UTC
Server: 45.61.58.125 (cypresawards.com)

Status: ${status}

${details}

---
This is an automated message from CyPresAwards server monitoring.
`;

  // Always log to file
  const logFile = '/var/log/cypresawards_status_email.log';
  const logEntry = `
${'='.repeat(70)}
TO: steve@designerwallcoverings.com
DATE: ${new Date().toISOString()}
${emailContent}
`;

  fs.appendFileSync(logFile, logEntry);
  console.log('✅ Status email logged to:', logFile);

  // Try to load SMTP configuration
  let smtpConfig;
  const configPath = path.join(__dirname, 'smtp_config.js');

  try {
    smtpConfig = require(configPath);
  } catch (err) {
    console.log('ℹ️  No smtp_config.js found - email will only be logged to file');
    console.log('   To enable email delivery:');
    console.log('   1. Copy smtp_config.example.js to smtp_config.js');
    console.log('   2. Configure your SMTP credentials');
    console.log('   3. Set enabled: true');
    console.log('\n' + emailContent);
    return;
  }

  // Check if SMTP is enabled
  if (!smtpConfig.enabled) {
    console.log('ℹ️  SMTP is disabled in smtp_config.js - email only logged to file');
    console.log('\n' + emailContent);
    return;
  }

  // Send actual email via SMTP
  try {
    const transporter = nodemailer.createTransporter({
      host: smtpConfig.host,
      port: smtpConfig.port || 587,
      secure: smtpConfig.secure || false,
      service: smtpConfig.service,
      auth: smtpConfig.auth
    });

    const subject = `CyPresAwards Server Status - ${new Date().toISOString().split('T')[0]}`;

    await transporter.sendMail({
      from: smtpConfig.from || 'noreply@cypresawards.com',
      to: smtpConfig.to || 'steve@designerwallcoverings.com',
      subject: subject,
      text: emailContent
    });

    console.log('✅ Email sent successfully to:', smtpConfig.to);
    console.log('\n' + emailContent);
  } catch (error) {
    console.error('❌ Failed to send email via SMTP:', error.message);
    console.log('   Email has been logged to file instead');
    throw error;
  }
}

// Get status from command line args
const status = process.argv[2] || 'UNKNOWN';
const details = process.argv.slice(3).join(' ') || 'No details provided';

sendStatusEmail(status, details)
  .then(() => process.exit(0))
  .catch(err => {
    console.error('Error sending email:', err);
    process.exit(1);
  });