dockerize smtp-proxy (#118)

This commit is contained in:
KM Koushik
2025-03-20 21:56:12 +11:00
committed by GitHub
parent 5465e2ec74
commit ecd28428d2
5 changed files with 141 additions and 53 deletions

View File

@@ -0,0 +1,45 @@
# Stage 1: Build stage
FROM node:20-alpine AS builder
# Install pnpm (package manager) globally
RUN npm install -g pnpm
# Set working directory for the application
WORKDIR /app
# Copy configuration files first
COPY package.json tsconfig.json tsup.config.ts ./
# Install dependencies (including devDependencies)
RUN pnpm install
# Copy the source code
COPY src/ ./src/
# Build the application
RUN pnpm run build
# Remove development dependencies to reduce size
RUN pnpm prune --prod
# Stage 2: Production stage
FROM node:20-alpine AS production
# Set working directory in the final image
WORKDIR /app
# Copy necessary files from the builder stage: production node_modules, build output, and package definition
COPY --from=builder /app/node_modules ./node_modules
# Copy only the necessary files from builder
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/package.json ./package.json
# Expose SMTP ports (standard SMTP, SMTPS, and alternative ports)
EXPOSE 25 465 587 2465 2587
# Run the SMTP server
CMD ["node", "dist/server.js"]

View File

@@ -0,0 +1,29 @@
name: unsend-smtp-server
services:
smtp-server:
container_name: unsend-smtp-server
image: unsend/smtp-proxy:latest
# Pass necessary environment variables
environment:
SMTP_AUTH_USERNAME: "unsend" # can be anything, just use the same while sending emails
UNSEND_BASE_URL: "https://app.unsend.dev" # your self hosted unsend instance url
# Uncomment this if you have SSL certificates. port 465 and 2465 will be using SSL
# UNSEND_API_KEY_PATH: "/certs/server.key"
# UNSEND_API_CERT_PATH: "/certs/server.crt"
# If you have SSL certificates, mount them here (read-only recommended)
# volumes:
# - ./certs/server.key:/certs/server.key:ro
# - ./certs/server.crt:/certs/server.crt:ro
# Expose the SMTP ports
ports:
- "25:25"
- "587:587"
- "2587:2587"
- "465:465"
- "2465:2465"
# Restart always or on-failure, depending on preference
restart: unless-stopped

View File

@@ -81,10 +81,9 @@ const serverOptions: SMTPServerOptions = {
replyTo: parsed.replyTo?.text,
};
console.log("Parsed email data:", emailObject); // Debug statement
sendEmailToUnsend(emailObject, session.user)
.then(() => callback())
.then(() => console.log("Email sent successfully to: ", emailObject.to))
.catch((error) => {
console.error("Failed to send email:", error.message);
callback(error);
@@ -104,18 +103,22 @@ const serverOptions: SMTPServerOptions = {
};
function startServers() {
// Implicit SSL/TLS for ports 465 and 2465
[465, 2465].forEach((port) => {
const server = new SMTPServer({ ...serverOptions, secure: true });
if (SSL_KEY_PATH && SSL_CERT_PATH) {
// Implicit SSL/TLS for ports 465 and 2465
[465, 2465].forEach((port) => {
const server = new SMTPServer({ ...serverOptions, secure: true });
server.listen(port, () => {
console.log(`Implicit SSL/TLS SMTP server is listening on port ${port}`);
});
server.listen(port, () => {
console.log(
`Implicit SSL/TLS SMTP server is listening on port ${port}`
);
});
server.on("error", (err) => {
console.error(`Error occurred on port ${port}:`, err);
server.on("error", (err) => {
console.error(`Error occurred on port ${port}:`, err);
});
});
});
}
// STARTTLS for ports 25, 587, and 2587
[25, 587, 2587].forEach((port) => {

View File

@@ -1,8 +1,8 @@
const nodemailer = require("nodemailer");
const transporter = nodemailer.createTransport({
host: "smtp.unsend.dev",
port: 2587,
host: "localhost",
port: 25,
secure: false,
auth: {
user: "unsend",