diff options
| author | Gavin Mogan <git@gavinmogan.com> | 2025-04-22 18:11:27 -0700 | 
|---|---|---|
| committer | Gavin Mogan <git@gavinmogan.com> | 2025-04-22 18:17:31 -0700 | 
| commit | 2520ba69933e17f6ef7f8cc11f803de3e66b179f (patch) | |
| tree | 231fe85c024a33ab735bfce1eb786e9e074c59b9 /src/lib | |
| parent | fc78f8eb7723db727484d83016c7a633a03dbacb (diff) | |
Allow url with config information for nodemailer
Diffstat (limited to 'src/lib')
| -rw-r--r-- | src/lib/config.ts | 1 | ||||
| -rw-r--r-- | src/lib/email.ts | 80 | 
2 files changed, 45 insertions, 36 deletions
diff --git a/src/lib/config.ts b/src/lib/config.ts index e8b774a..003a714 100644 --- a/src/lib/config.ts +++ b/src/lib/config.ts @@ -28,6 +28,7 @@ export interface GathioConfig {          mongodb_url: string;      };      nodemailer?: { +        smtp_url?: string;          smtp_server: string;          smtp_port: string;          smtp_username: string; diff --git a/src/lib/email.ts b/src/lib/email.ts index 82dd48e..7b7a7a1 100644 --- a/src/lib/email.ts +++ b/src/lib/email.ts @@ -1,6 +1,6 @@  import { Request } from "express";  import sgMail from "@sendgrid/mail"; -import nodemailer from "nodemailer"; +import nodemailer, { Transporter } from "nodemailer";  import { getConfig } from "./config.js";  import SMTPTransport from "nodemailer/lib/smtp-transport/index.js";  import { exitWithError } from "./process.js"; @@ -37,32 +37,36 @@ export const initEmailService = async (): Promise<boolean> => {              console.log("Sendgrid is ready to send emails.");              return true;          case "nodemailer": -            if ( -                !config.nodemailer?.smtp_server || -                !config.nodemailer?.smtp_port -            ) { -                return exitWithError( -                    "Nodemailer is configured as the email service, but not all required fields are provided. Please provide all required fields in the config file.", -                ); -            } -            const nodemailerConfig = { -                host: config.nodemailer?.smtp_server, -                port: Number(config.nodemailer?.smtp_port) || 587, -                tls: {  -                    // do not fail on invalid certs -                    rejectUnauthorized: false, -                }, -            } as SMTPTransport.Options; +            let nodemailerTransporter:Transporter|undefined = undefined; +            if (config.nodemailer?.smtp_url) { +                nodemailerTransporter = nodemailer.createTransport(config.nodemailer?.smtp_url); +            } else { +                if ( +                    !config.nodemailer?.smtp_server || +                    !config.nodemailer?.smtp_port +                ) { +                    return exitWithError( +                        "Nodemailer is configured as the email service, but not all required fields are provided. Please provide all required fields in the config file.", +                    ); +                } +                const nodemailerConfig = { +                    host: config.nodemailer?.smtp_server, +                    port: Number(config.nodemailer?.smtp_port) || 587, +                    tls: {  +                        // do not fail on invalid certs +                        rejectUnauthorized: false, +                    }, +                } as SMTPTransport.Options; -            if (config.nodemailer?.smtp_username) { -                nodemailerConfig.auth = { -                    user: config.nodemailer?.smtp_username, -                    pass: config.nodemailer?.smtp_password -                }; +                if (config.nodemailer?.smtp_username) { +                    nodemailerConfig.auth = { +                        user: config.nodemailer?.smtp_username, +                        pass: config.nodemailer?.smtp_password +                    }; +                } +                nodemailerTransporter = nodemailer.createTransport(nodemailerConfig);              } -            const nodemailerTransporter = -                nodemailer.createTransport(nodemailerConfig);              const nodemailerVerified = await nodemailerTransporter.verify();              if (nodemailerVerified) {                  console.log("Nodemailer is ready to send emails."); @@ -110,20 +114,24 @@ export const sendEmail = async (              }          case "nodemailer":              try { -                const nodemailerConfig = { -                    host: config.nodemailer?.smtp_server, -                    port: Number(config.nodemailer?.smtp_port) || 587, -                } as SMTPTransport.Options; +                let nodemailerTransporter:Transporter|undefined = undefined; +                if (config.nodemailer?.smtp_url) { +                    nodemailerTransporter = nodemailer.createTransport(config.nodemailer?.smtp_url); +                } else { +                    const nodemailerConfig = { +                        host: config.nodemailer?.smtp_server, +                        port: Number(config.nodemailer?.smtp_port) || 587, +                    } as SMTPTransport.Options; -                if (config.nodemailer?.smtp_username) { -                    nodemailerConfig.auth = { -                        user: config.nodemailer?.smtp_username, -                        pass: config.nodemailer?.smtp_password -                    }; -                } +                    if (config.nodemailer?.smtp_username) { +                        nodemailerConfig.auth = { +                            user: config.nodemailer?.smtp_username, +                            pass: config.nodemailer?.smtp_password +                        }; +                    } -                const nodemailerTransporter = -                    nodemailer.createTransport(nodemailerConfig); +                    nodemailerTransporter = nodemailer.createTransport(nodemailerConfig); +                }                  await nodemailerTransporter.sendMail({                      envelope: {                          from: config.general.email,  | 
