| 1 | import { join } from 'node:path'; |
| 2 | import { Global, Module |
| 3 | import { ConfigModule, ConfigService |
| 4 | import { MailerModule } |
| 5 | import { ReactAdapter } |
| 6 | |
| 7 | import { MailService } |
| 8 | import { proxyTransport } |
| 9 | |
| 10 | /** True once either delivery route (HTTP relay or SMTP) is configured. */ |
| 11 | export function mailConfigured(config |
| 12 | return Boolean( |
| 13 | config.get<string |
| 14 | ); |
| 15 | } |
| 16 | |
| 17 | /** Self-hosters without mail configured keep email verification off. */ |
| 18 | export function emailVerificationEnabled(config |
| 19 | return config.get |
| 20 | } |
| 21 | |
| 22 | @Global() |
| 23 | @Module({ |
| 24 | imports: [ |
| 25 | MailerModule.forRootAsync({ |
| 26 | imports: [ConfigModule], |
| 27 | inject: [ConfigService], |
| 28 | useFactory: (config: |
| 29 | const proxyUrl = |
| 30 | const host = |
| 31 | |
| 32 | if (!proxyUrl && |
| 33 | throw new Error( |
| 34 | 'EMAIL_VERIFICATION_ENABLED requires either EMAIL_PROXY or MAIL_HOST/MAIL_PORT/MAIL_USER/MAIL_PASSWORD', |
| 35 | ); |
| 36 | } |
| 37 | |
| 38 | return { |
| 39 | transport: proxyUrl |
| 40 | ? proxyTransport(proxyUrl, |
| 41 | : host |
| 42 | ? { |
| 43 | host, |
| 44 | port: Number(config |
| 45 | secure: config.get |
| 46 | auth: { |
| 47 | user: config.getOrThrow |
| 48 | pass: config.getOrThrow |
| 49 | }, |
| 50 | } |
| 51 | : // No mail configured and nothing sends mail: swallow instead of crashing. |
| 52 | { jsonTransport: true }, |
| 53 | defaults: { |
| 54 | from: |
| 55 | config.get<string |
| 56 | 'Ghost <noreply@ghost.local>', |
| 57 | }, |
| 58 | // Templates are compiled to dist/mail/templates alongside this module. |
| 59 | template: { |
| 60 | dir: join(import. |
| 61 | adapter: new ReactAdapter |
| 62 | }, |
| 63 | }; |
| 64 | }, |
| 65 | }), |
| 66 | ], |
| 67 | providers: [MailService], |
| 68 | exports: [MailService], |
| 69 | }) |
| 70 | export class MailModule |