Refactor email handling to enhance SMTP configuration and error logging
- Updated the email transporter initialization to support secure connections and dynamic port settings. - Added authentication handling for SMTP credentials if provided. - Improved error logging during SMTP verification to include detailed connection information. - Adjusted the sendEmail function signature for consistency with updated parameters.
This commit is contained in:
@@ -24,21 +24,47 @@ let transporter: nodemailer.Transporter;
|
|||||||
// Initialize the transporter based on environment
|
// Initialize the transporter based on environment
|
||||||
function initializeTransporter() {
|
function initializeTransporter() {
|
||||||
if (isProduction && SMTP_HOST) {
|
if (isProduction && SMTP_HOST) {
|
||||||
// Use local Postfix mail relay (no authentication)
|
const port = parseInt(SMTP_PORT, 10) || 587;
|
||||||
transporter = nodemailer.createTransport({
|
// Determine if secure connection (port 465 typically uses SSL)
|
||||||
host: SMTP_HOST,
|
const secure = port === 465;
|
||||||
port: parseInt(SMTP_PORT, 10) || 25, // default to port 25 if not set
|
|
||||||
secure: false, // No SSL for local relay
|
|
||||||
tls: {
|
|
||||||
rejectUnauthorized: false, // Accept self-signed certificates if present
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
transporter.verify((error, success) => {
|
// Build transporter config
|
||||||
|
const transporterConfig: {
|
||||||
|
host: string;
|
||||||
|
port: number;
|
||||||
|
secure: boolean;
|
||||||
|
tls: { rejectUnauthorized: boolean };
|
||||||
|
auth?: { user: string; pass: string };
|
||||||
|
} = {
|
||||||
|
host: SMTP_HOST,
|
||||||
|
port: port,
|
||||||
|
secure: secure,
|
||||||
|
tls: {
|
||||||
|
rejectUnauthorized: false, // Accept self-signed certificates (useful for Mailcow)
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
// Add authentication if credentials are provided
|
||||||
|
if (SMTP_USER && SMTP_PASS) {
|
||||||
|
transporterConfig.auth = {
|
||||||
|
user: SMTP_USER,
|
||||||
|
pass: SMTP_PASS,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
transporter = nodemailer.createTransport(transporterConfig);
|
||||||
|
|
||||||
|
transporter.verify((error, _success) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
console.error('❌ SMTP connection error:', error);
|
console.error('❌ SMTP connection error:', error.message);
|
||||||
|
console.error(' Host:', SMTP_HOST);
|
||||||
|
console.error(' Port:', port);
|
||||||
|
console.error(' Auth:', SMTP_USER ? 'Yes' : 'No');
|
||||||
} else {
|
} else {
|
||||||
console.log('✅ SMTP server is ready to take messages.');
|
console.log('✅ SMTP server is ready to take messages.');
|
||||||
|
console.log(' Host:', SMTP_HOST);
|
||||||
|
console.log(' Port:', port);
|
||||||
|
console.log(' Auth:', SMTP_USER ? 'Yes' : 'No');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
@@ -140,7 +166,7 @@ export function logEmailAttempt(success: boolean, recipient: string, subject: st
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Send an email
|
// Send an email
|
||||||
export async function sendEmail(to: string, subject: string, html: string, text: string, domain?: string): Promise<boolean> {
|
export async function sendEmail(to: string, subject: string, html: string, text: string, _domain?: string): Promise<boolean> {
|
||||||
// Initialize transporter if not already done
|
// Initialize transporter if not already done
|
||||||
if (!transporter) {
|
if (!transporter) {
|
||||||
initializeTransporter();
|
initializeTransporter();
|
||||||
|
|||||||
Reference in New Issue
Block a user