fix: Ensure email configuration from environment variables is properly used
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 <noreply@anthropic.com>
This commit is contained in:
@@ -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({
|
||||
|
||||
@@ -89,7 +89,8 @@ export default defineEventHandler(async (event) => {
|
||||
bibleReferencesText,
|
||||
sermon.personal_appliance,
|
||||
sermon.pastors_challenge,
|
||||
userNotes
|
||||
userNotes,
|
||||
event
|
||||
)
|
||||
|
||||
return {
|
||||
|
||||
@@ -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 <noreply@example.com>',
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
|
||||
Reference in New Issue
Block a user