This repository was archived by the owner on Jan 14, 2024. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathsmtp.js
More file actions
94 lines (83 loc) 路 2.54 KB
/
Copy pathsmtp.js
File metadata and controls
94 lines (83 loc) 路 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
const fs = require("fs");
const SMTPServer = require("smtp-server").SMTPServer;
const nodemailer = require('nodemailer');
const smtpPort = 25;
let transporter = nodemailer.createTransport({
service: 'postfix',
host: 'localhost',
secure: false,
port: 2525,
//auth: { user: 'yourlinuxusername@edison.example.com', pass: 'yourlinuxuserpassword' },
tls: { rejectUnauthorized: false }
});
const server = new SMTPServer({
secure: false, // Set to true for secure connections (TLS)
authOptional: false, // Require authentication
banner: "Is-a.dev SMTP Server",
onAuth: (auth, session, callback) => {
try {
const { username, password } = auth;
if (!fs.existsSync(`content/${username}`)) {
return callback(new Error("User does not exist"));
}
let config = fs.readFileSync(`content/${username}/config.json`);
config = JSON.parse(config);
if (!config.smtp) {
return callback(new Error("SMTP is disabled for this user"));
}
if (config.smtp_password && password !== config.smtp_password) {
return callback(new Error("Invalid password"));
}
callback(null, { user: username });
} catch (err) {
console.error("Authentication Error:", err);
callback(new Error("Internal server error"));
}
},
onData: (stream, session, callback) => {
// Handle email data as needed
let data = "";
stream.on("data", (chunk) => {
data += chunk;
});
stream.on("end", () => {
transporter.on("end", () => {
console.log("Email sent successfully!");
callback();
});
transporter.on("error", (err) => {
console.error("Error sending email:", err);
callback();
});
// Send email data
transporter.send(
{
from: mailFrom,
to: rcptTo,
data: data,
},
(err) => {
if (err) {
console.error("Error sending email:", err);
}
transporter.quit();
}
);
});
},
onMailFrom(address, session, callback) {
const allowedDomain = `${session.user}.is-a.dev`;
if (!address.address.endsWith(`@${allowedDomain}`)) {
return callback(
new Error(`Only addresses ending with @${allowedDomain} are allowed to send mail`)
);
}
return callback(); // Accept the address
}
});
server.on("error", (err) => {
console.error("SMTP Server Error:", err);
});
server.listen(smtpPort, () => {
console.log("SMTP Server listening on port " + smtpPort);
});