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