From 47b4a14c4b1553c1e0b4b900509354bc569abef0 Mon Sep 17 00:00:00 2001 From: Joshua Ryder Date: Wed, 5 Nov 2025 18:11:13 -0500 Subject: [PATCH] fix: Ensure email configuration from environment variables is properly used MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixed an issue where SMTP configuration would fall back to defaults despite environment variables being set in docker-compose.yml. The email utility now properly accesses runtime configuration by accepting the H3 event context. Changes: - Created getEmailConfig() helper with dual-strategy config access - Pass event context from API handlers to email functions - Added fallback to direct process.env access for reliability - Added debug logging to diagnose configuration issues in production This ensures Office365 and other SMTP providers work correctly when configured via environment variables. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- server/api/auth/forgot-password.post.ts | 2 +- server/api/notes/email/[sermonId].post.ts | 3 +- server/utils/email.ts | 49 ++++++++++++++++++++--- 3 files changed, 47 insertions(+), 7 deletions(-) diff --git a/server/api/auth/forgot-password.post.ts b/server/api/auth/forgot-password.post.ts index 9cc48de..aac6de3 100644 --- a/server/api/auth/forgot-password.post.ts +++ b/server/api/auth/forgot-password.post.ts @@ -31,7 +31,7 @@ export default defineEventHandler(async (event) => { // Send email try { - await sendPasswordResetEmail(email, code) + await sendPasswordResetEmail(email, code, event) } catch (error) { console.error('Failed to send reset email:', error) throw createError({ diff --git a/server/api/notes/email/[sermonId].post.ts b/server/api/notes/email/[sermonId].post.ts index 2541adc..46bedb9 100644 --- a/server/api/notes/email/[sermonId].post.ts +++ b/server/api/notes/email/[sermonId].post.ts @@ -89,7 +89,8 @@ export default defineEventHandler(async (event) => { bibleReferencesText, sermon.personal_appliance, sermon.pastors_challenge, - userNotes + userNotes, + event ) return { diff --git a/server/utils/email.ts b/server/utils/email.ts index aa0bf2c..bfdb717 100644 --- a/server/utils/email.ts +++ b/server/utils/email.ts @@ -1,9 +1,47 @@ import nodemailer from 'nodemailer' import crypto from 'crypto' +import type { H3Event } from 'h3' + +/** + * Get email configuration from runtime config or environment variables + * In production, environment variables are accessed directly for reliability + */ +function getEmailConfig(event?: H3Event) { + // Try to use runtime config if event is provided + if (event) { + try { + const config = useRuntimeConfig(event) + return { + emailHost: config.emailHost, + emailPort: config.emailPort, + emailUser: config.emailUser, + emailPassword: config.emailPassword, + emailFrom: config.emailFrom, + } + } catch (e) { + console.warn('[EMAIL] Failed to access runtime config, falling back to env vars') + } + } + + // Fallback to direct environment variable access + return { + emailHost: process.env.EMAIL_HOST || 'smtp.example.com', + emailPort: process.env.EMAIL_PORT || '587', + emailUser: process.env.EMAIL_USER || 'noreply@example.com', + emailPassword: process.env.EMAIL_PASSWORD || '', + emailFrom: process.env.EMAIL_FROM || 'New Life Christian Church ', + } +} + +export async function sendPasswordResetEmail(email: string, code: string, event?: H3Event) { + const config = getEmailConfig(event) + + // Debug logging for email configuration + console.log('[EMAIL CONFIG] Host:', config.emailHost) + console.log('[EMAIL CONFIG] Port:', config.emailPort) + console.log('[EMAIL CONFIG] User:', config.emailUser) + console.log('[EMAIL CONFIG] From:', config.emailFrom) -export async function sendPasswordResetEmail(email: string, code: string) { - const config = useRuntimeConfig() - const transporter = nodemailer.createTransport({ host: config.emailHost, port: parseInt(config.emailPort), @@ -82,9 +120,10 @@ export async function sendSermonNotesEmail( bibleReferences: string, personalAppliance: string, pastorsChallenge: string, - userNotes: string + userNotes: string, + event?: H3Event ) { - const config = useRuntimeConfig() + const config = getEmailConfig(event) const transporter = nodemailer.createTransport({ host: config.emailHost,