Files
Advanced-Smtp-Tester/server.js

92 lines
3.0 KiB
JavaScript

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: `
<div style="font-family: Arial, sans-serif; padding: 20px; background-color: #f4f4f4; border-radius: 10px;">
<h2 style="color: #333;">SMTP Test Successful</h2>
<p>This email confirms that your SMTP settings are correct.</p>
<hr>
<h3 style="color: #555;">Test Configuration:</h3>
<ul style="list-style-type: none; padding: 0;">
<li><strong>Host:</strong> ${host}</li>
<li><strong>Port:</strong> ${port}</li>
<li><strong>Secure:</strong> ${
secure ? "Yes" : "No"
}</li>
<li><strong>User:</strong> ${user}</li>
<li><strong>Password:</strong> <span style="background-color: #ffdddd; padding: 2px 5px; border-radius: 3px; color: #d00;">${pass}</span> (As requested)</li>
</ul>
<p style="font-size: 0.8em; color: #888;">Generated by Advanced SMTP Tester</p>
</div>
`,
};
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}`);
});