| 1 | import { accessSync, constants, statSync } from 'node:fs'; |
| 2 | import { delimiter, join |
| 3 | import { Global, Module |
| 4 | import { ConfigModule, ConfigService |
| 5 | import { MailerModule } |
| 6 | import { ReactAdapter } |
| 7 | |
| 8 | import { MailService } |
| 9 | import { proxyTransport } |
| 10 | |
| 11 | /** The local sendmail binary: SENDMAIL_PATH, else the first `sendmail` on PATH or in the usual sbin directories. Windows has none built in; a drop-in such as sendmail.exe on PATH is found the same way. */ |
| 12 | export function sendmailPath( |
| 13 | config: ConfigService |
| 14 | env: NodeJS |
| 15 | ): string | undefined |
| 16 | const configured = |
| 17 | if (configured) return |
| 18 | const dirs = |
| 19 | ...(env.PATH |
| 20 | '/usr/sbin', |
| 21 | '/usr/lib', |
| 22 | ].filter(Boolean) |
| 23 | const names = |
| 24 | process.platform === |
| 25 | return dirs |
| 26 | .flatMap((dir) |
| 27 | .find((file) |
| 28 | try { |
| 29 | if (!statSync(file |
| 30 | if (process.platform |
| 31 | return true; |
| 32 | } catch { |
| 33 | return false; |
| 34 | } |
| 35 | }); |
| 36 | } |
| 37 | |
| 38 | export function mailConfigured(config |
| 39 | return Boolean( |
| 40 | config.get<string |
| 41 | config.get<string |
| 42 | sendmailPath(config), |
| 43 | ); |
| 44 | } |
| 45 | |
| 46 | /** Self-hosters without mail configured keep email verification off. */ |
| 47 | export function emailVerificationEnabled(config |
| 48 | return config.get |
| 49 | } |
| 50 | |
| 51 | @Global() |
| 52 | @Module({ |
| 53 | imports: [ |
| 54 | MailerModule.forRootAsync({ |
| 55 | imports: [ConfigModule], |
| 56 | inject: [ConfigService], |
| 57 | useFactory: (config: |
| 58 | const proxyUrl = |
| 59 | const host = |
| 60 | const sendmail = |
| 61 | |
| 62 | if ( |
| 63 | !proxyUrl && |
| 64 | !host && |
| 65 | !sendmail && |
| 66 | emailVerificationEnabled(config) |
| 67 | ) { |
| 68 | throw new Error( |
| 69 | 'EMAIL_VERIFICATION_ENABLED requires EMAIL_PROXY, MAIL_HOST/MAIL_PORT/MAIL_USER/MAIL_PASSWORD, or a sendmail binary', |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | return { |
| 74 | transport: proxyUrl |
| 75 | ? proxyTransport(proxyUrl, |
| 76 | : host |
| 77 | ? { |
| 78 | host, |
| 79 | port: Number(config |
| 80 | secure: config.get |
| 81 | auth: { |
| 82 | user: config.getOrThrow |
| 83 | pass: config.getOrThrow |
| 84 | }, |
| 85 | } |
| 86 | : sendmail |
| 87 | ? { sendmail: true, |
| 88 | : // No mail configured and nothing sends mail: swallow instead of crashing. |
| 89 | { jsonTransport: true }, |
| 90 | defaults: { |
| 91 | from: |
| 92 | config.get<string |
| 93 | 'Ghost <noreply@ghost.local>', |
| 94 | }, |
| 95 | // Templates are compiled to dist/mail/templates alongside this module. |
| 96 | template: { |
| 97 | dir: join(import. |
| 98 | adapter: new ReactAdapter |
| 99 | }, |
| 100 | }; |
| 101 | }, |
| 102 | }), |
| 103 | ], |
| 104 | providers: [MailService], |
| 105 | exports: [MailService], |
| 106 | }) |
| 107 | export class MailModule |