Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions application/backend/src/controllers/MailerController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,33 @@ describe('MailerController', () => {
})
})
})

// Test due to nodemailer moving to more strict sender address parsing
describe('Mailer Configuration', () => {
const originalHostname = process.env.HOSTNAME

beforeEach(() => {
jest.resetModules()
})

afterEach(() => {
process.env.HOSTNAME = originalHostname
})

it('should strip http:// and ports from HOSTNAME in fromAddress', async () => {
// non RFC-compliant environment variable
process.env.HOSTNAME = 'http://localhost:5173'

const mailerModule = await import('../utils/mailer')

expect(mailerModule!.fromAddress).toBe('CTRL <noreply@localhost>')
})

it('should handle plain domains without protocols', async () => {
process.env.HOSTNAME = 'production-domain.com'

const mailerModule = await import('../utils/mailer')

expect(mailerModule!.fromAddress).toBe('CTRL <noreply@production-domain.com>')
})
})
19 changes: 18 additions & 1 deletion application/backend/src/utils/mailer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
import nodemailer from 'nodemailer'
import config from '../config'

export const fromAddress = `CTRL <noreply@${process.env.HOSTNAME}>`
if (!process.env.HOSTNAME) {
throw new Error('process.env.HOSTNAME is required but was not provided.')
}

const hostnameEnvVar = process.env.HOSTNAME
let mailDomain: string

if (/^https?:\/\//i.test(hostnameEnvVar)) {
try {
mailDomain = new URL(hostnameEnvVar).hostname
} catch (err: any) {
throw new Error(`HOSTNAME deployment variable not configured. ${err}`)
}
} else {
mailDomain = hostnameEnvVar
}

export const fromAddress = `CTRL <noreply@${mailDomain}>`

export async function createMailerTransporter() {
if (process.env.STUB_MAILER == 'true') {
Expand Down
Loading