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: `

SMTP Test Successful

This email confirms that your SMTP settings are correct.


Test Configuration:

Generated by Advanced SMTP Tester

`, }; const info = await transporter.sendMail(mailOptions); console.log("Message sent: %s", info.messageId); res.json({ success: true, message: "Connection verified and email sent successfully!", details: { messageId: info.messageId, response: info.response, }, }); } catch (error) { console.error("SMTP Error:", error); res.status(500).json({ success: false, message: "SMTP Test Failed", error: error.message, }); } }); app.listen(port, () => { console.log(`Server running at http://localhost:${port}`); });