Refactor email transporter initialization for improved local relay support

- Simplify SMTP transporter setup for production by removing unnecessary authentication for local Postfix relay.
- Enhance error and success logging for SMTP connection verification.
- Update development mode to log email output to console with a warning message.
This commit is contained in:
2025-06-07 02:11:49 +02:00
parent 8066098242
commit 532138f988

View File

@@ -32,47 +32,33 @@ let transporter: nodemailer.Transporter;
// Initialize the transporter based on environment // Initialize the transporter based on environment
function initializeTransporter() { function initializeTransporter() {
if (isProduction && SMTP_HOST && SMTP_USER && SMTP_PASS) { if (isProduction && SMTP_HOST) {
// Production: Use SMTP server // Use local Postfix mail relay (no authentication)
// ProtonMail specific configuration
// ProtonMail often requires using their Bridge application for SMTP
const isProtonMail = SMTP_HOST.includes('protonmail');
transporter = nodemailer.createTransport({ transporter = nodemailer.createTransport({
host: SMTP_HOST, host: SMTP_HOST,
port: parseInt(SMTP_PORT, 10), port: parseInt(SMTP_PORT, 10) || 25, // default to port 25 if not set
secure: parseInt(SMTP_PORT, 10) === 465, // true for 465, false for other ports secure: false, // No SSL for local relay
auth: { tls: {
user: SMTP_USER, rejectUnauthorized: false, // Accept self-signed certificates if present
pass: SMTP_PASS,
}, },
// ProtonMail specific settings
...(isProtonMail && {
tls: {
// Do not fail on invalid certs
rejectUnauthorized: false,
// Specific ciphers for ProtonMail
ciphers: 'SSLv3',
},
}),
}); });
// Verify SMTP connection configuration transporter.verify((error, success) => {
transporter.verify(function (error, _success) {
if (error) { if (error) {
console.error('SMTP connection error:', error); console.error('SMTP connection error:', error);
} else { } else {
console.log('SMTP server is ready to take our messages'); console.log('SMTP server is ready to take messages.');
} }
}); });
} else { } else {
// Development: Log emails to console // Fallback for development: log email output to console
transporter = nodemailer.createTransport({ transporter = nodemailer.createTransport({
streamTransport: true, streamTransport: true,
newline: 'unix', newline: 'unix',
buffer: true, buffer: true,
}); });
console.log('⚠️ Email transporter using streamTransport (development mode)');
} }
} }