()
| 35 | } |
| 36 | |
| 37 | export async function init(): Promise<void> { |
| 38 | if (isInitialized()) { |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | const { EMAIL_HOST, EMAIL_USER, EMAIL_PASS, EMAIL_PORT, EMAIL_FROM } = |
| 43 | process.env; |
| 44 | |
| 45 | if (EMAIL_FROM !== undefined) { |
| 46 | emailFrom = EMAIL_FROM; |
| 47 | } |
| 48 | |
| 49 | if (!(EMAIL_HOST ?? "") || !(EMAIL_USER ?? "") || !(EMAIL_PASS ?? "")) { |
| 50 | if (isDevEnvironment()) { |
| 51 | Logger.warning( |
| 52 | "No email client configuration provided. Running without email.", |
| 53 | ); |
| 54 | } else if (process.env["BYPASS_EMAILCLIENT"] === "true") { |
| 55 | Logger.warning("BYPASS_EMAILCLIENT is enabled! Running without email."); |
| 56 | } else { |
| 57 | throw new Error("No email client configuration provided"); |
| 58 | } |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | try { |
| 63 | transporter = nodemailer.createTransport({ |
| 64 | host: EMAIL_HOST, |
| 65 | secure: EMAIL_PORT === "465", |
| 66 | port: parseInt(EMAIL_PORT ?? "578", 10), |
| 67 | auth: { |
| 68 | user: EMAIL_USER, |
| 69 | pass: EMAIL_PASS, |
| 70 | }, |
| 71 | }); |
| 72 | transportInitialized = true; |
| 73 | |
| 74 | Logger.info("Verifying email client configuration..."); |
| 75 | const result = await transporter.verify(); |
| 76 | |
| 77 | if (!result) { |
| 78 | throw new Error( |
| 79 | `Could not verify email client configuration: ${JSON.stringify( |
| 80 | result, |
| 81 | )}`, |
| 82 | ); |
| 83 | } |
| 84 | |
| 85 | Logger.success("Email client configuration verified"); |
| 86 | } catch (error) { |
| 87 | transportInitialized = false; |
| 88 | Logger.error(getErrorMessage(error) ?? "Unknown error"); |
| 89 | Logger.error("Failed to verify email client configuration."); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | type MailResult = { |
| 94 | success: boolean; |
nothing calls this directly
no test coverage detected