#!/usr/bin/env node /** * Email Configuration Test Script * Run this to verify your SMTP settings are working correctly * * Usage: node test-email.js your-test-email@example.com */ require("dotenv").config(); const nodemailer = require("nodemailer"); const testEmail = process.argv[2]; if (!testEmail) { console.log("\nāŒ Please provide a test email address:"); console.log(" node test-email.js your-email@example.com\n"); process.exit(1); } console.log("\nšŸ“§ Sky Art Shop - Email Configuration Test\n"); console.log("─".repeat(50)); // Check if SMTP is configured if ( !process.env.SMTP_HOST || !process.env.SMTP_USER || !process.env.SMTP_PASS ) { console.log("\nāŒ SMTP not configured!\n"); console.log("Please edit your .env file and add:"); console.log("─".repeat(50)); console.log("SMTP_HOST=smtp.gmail.com"); console.log("SMTP_PORT=587"); console.log("SMTP_SECURE=false"); console.log("SMTP_USER=your-gmail@gmail.com"); console.log("SMTP_PASS=your-16-char-app-password"); console.log('SMTP_FROM="Sky Art Shop" '); console.log("─".repeat(50)); console.log("\nSee: https://myaccount.google.com/apppasswords\n"); process.exit(1); } console.log("āœ“ SMTP Host:", process.env.SMTP_HOST); console.log("āœ“ SMTP Port:", process.env.SMTP_PORT); console.log("āœ“ SMTP User:", process.env.SMTP_USER); console.log("āœ“ SMTP Pass:", "*".repeat(process.env.SMTP_PASS.length)); console.log("āœ“ From:", process.env.SMTP_FROM || '"Sky Art Shop"'); console.log("─".repeat(50)); async function testEmailSend() { console.log("\nā³ Creating email transporter..."); const transporter = nodemailer.createTransport({ host: process.env.SMTP_HOST, port: parseInt(process.env.SMTP_PORT) || 587, secure: process.env.SMTP_SECURE === "true", auth: { user: process.env.SMTP_USER, pass: process.env.SMTP_PASS, }, }); console.log("ā³ Verifying connection..."); try { await transporter.verify(); console.log("āœ“ SMTP connection verified!\n"); } catch (error) { console.log("\nāŒ Connection failed:", error.message); console.log("\nCommon issues:"); console.log(" • Wrong email or password"); console.log(" • App Password not set up correctly"); console.log(" • 2-Factor Auth not enabled on Gmail"); console.log(" • Less secure apps blocked (use App Password instead)\n"); process.exit(1); } console.log("ā³ Sending test email to:", testEmail); const testCode = Math.floor(100000 + Math.random() * 900000); try { const info = await transporter.sendMail({ from: process.env.SMTP_FROM || `"Sky Art Shop" <${process.env.SMTP_USER}>`, to: testEmail, subject: "šŸŽØ Sky Art Shop - Email Test Successful!", html: `

šŸŽ‰ Email Works!

Your Sky Art Shop email configuration is working correctly!

Here's a sample verification code:

${testCode}

This is a test email from Sky Art Shop backend.

Ā© ${new Date().getFullYear()} Sky Art Shop

`, }); console.log("\n" + "═".repeat(50)); console.log(" āœ… EMAIL SENT SUCCESSFULLY!"); console.log("═".repeat(50)); console.log("\nšŸ“¬ Check your inbox at:", testEmail); console.log(" (Also check spam/junk folder)\n"); console.log("Message ID:", info.messageId); console.log("\nšŸŽ‰ Your email configuration is working!\n"); } catch (error) { console.log("\nāŒ Failed to send email:", error.message); console.log("\nFull error:", error); process.exit(1); } } testEmailSend();