const express = require("express"); const nodemailer = require("nodemailer"); const bodyParser = require("body-parser"); const cors = require("cors"); const path = require("path"); const app = express(); const port = process.env.PORT || 3000; app.use(cors()); app.use(bodyParser.json()); app.use(express.static(path.join(__dirname, "public"))); app.post("/api/test-smtp", async (req, res) => { const { host, port, secure, user, pass, from, to } = req.body; // Log intent console.log( `Attempting SMTP connection to ${host}:${port} (${ secure ? "Secure" : "Insecure" }) for ${user}` ); // Create Transporter const transporter = nodemailer.createTransport({ host: host, port: parseInt(port), secure: secure === true || secure === "true", // true for 465, false for other ports auth: { user: user, pass: pass, }, tls: { rejectUnauthorized: false, // Allow self-signed certs for testing flexibility }, }); try { // 1. Verify Connection await transporter.verify(); console.log("SMTP Connection Verified Successfully"); // 2. Send Test Email const mailOptions = { from: from || user, // Default to user if from not specified to: to, subject: "SMTP Test - Advanced SMTP Tester", html: `
This email confirms that your SMTP settings are correct.
Generated by Advanced SMTP Tester