deploy success - first version successed

This commit is contained in:
2025-07-28 13:17:54 +08:00
parent 2999e562c7
commit eeb3b4b0df
9 changed files with 172 additions and 358 deletions

View File

@@ -7,6 +7,14 @@ RUN npm install
COPY . .
# Expose API port and SMTP port
EXPOSE 5182
EXPOSE 25
# Environment variables for database connection will be passed at runtime
# ENV DB_HOST=...
# ENV DB_USER=...
# ENV DB_PASSWORD=...
# ENV DB_NAME=...
CMD [ "node", "app.js" ]

View File

@@ -1,9 +1,12 @@
const express = require('express');
const cors = require('cors');
const db = require('./db');
const { SMTPServer } = require('smtp-server');
const { saveEmail } = require('./saveEmail');
const app = express();
const port = 5182;
const apiPort = 5182;
const smtpPort = 25;
app.use(cors());
app.use(express.json());
@@ -27,6 +30,33 @@ app.get('/api/messages', async (req, res) => {
}
});
app.listen(port, () => {
console.log(`Backend server listening at http://localhost:${port}`);
// Start API server
app.listen(apiPort, () => {
console.log(`Backend API server listening at http://localhost:${apiPort}`);
});
// Configure and start SMTP server
const smtpServer = new SMTPServer({
authOptional: true,
disabledCommands: ['AUTH'],
onData(stream, session, callback) {
console.log('Receiving email...');
saveEmail(stream)
.then(() => {
console.log('Email processed and saved successfully.');
callback(); // Accept the message
})
.catch(err => {
console.error('Error processing email:', err);
callback(new Error('Failed to process email.'));
});
},
});
smtpServer.on('error', err => {
console.error('SMTP Server Error:', err.message);
});
smtpServer.listen(smtpPort, () => {
console.log(`SMTP server listening on port ${smtpPort}`);
});

View File

@@ -11,6 +11,6 @@
"mysql2": "^3.14.2",
"mailparser": "^3.7.4",
"cors": "^2.8.5",
"get-stream": "^9.0.1"
"smtp-server": "^3.13.4"
}
}

View File

@@ -1,16 +1,11 @@
#!/usr/bin/env node
const { simpleParser } = require('mailparser');
const db = require('./db');
// Using require for get-stream v5 as it's CommonJS
const getStream = require('get-stream');
async function saveEmail(rawEmail) {
async function saveEmail(stream) {
try {
const parsed = await simpleParser(rawEmail);
const parsed = await simpleParser(stream);
const rawEmail = stream.toString(); // Or handle stream to buffer conversion appropriately
// Ensure 'to' and 'from' objects exist before accessing 'text'
const recipient = parsed.to ? parsed.to.text : 'undisclosed-recipients';
const sender = parsed.from ? parsed.from.text : 'unknown-sender';
const subject = parsed.subject;
@@ -21,7 +16,7 @@ async function saveEmail(rawEmail) {
[recipient, sender, subject, body, rawEmail]
);
console.log(`Email saved with ID: ${result.insertId}`);
console.log(`Email from <${sender}> to <${recipient}> saved with ID: ${result.insertId}`);
if (parsed.attachments && parsed.attachments.length > 0) {
for (const attachment of parsed.attachments) {
@@ -34,21 +29,11 @@ async function saveEmail(rawEmail) {
}
} catch (error) {
console.error('Failed to save email:', error);
// Exit with an error code to signal failure to the calling process (e.g., Mailu)
process.exit(1);
// We should not exit the process here, but maybe throw the error
// so the caller (SMTPServer) can handle it.
throw error;
}
}
(async () => {
try {
const rawEmail = await getStream(process.stdin);
if (rawEmail && rawEmail.length > 0) {
await saveEmail(rawEmail);
} else {
console.log('Received empty input, no email to save.');
}
} catch (error) {
console.error('Error reading from stdin:', error);
process.exit(1);
}
})();
module.exports = { saveEmail };