diff options
| author | INOUE Daisuke <1407574+dinoue@users.noreply.github.com> | 2025-04-28 21:01:02 +0900 | 
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-04-28 21:01:02 +0900 | 
| commit | c5a527e63c16c17a18d4452952fa17fe045abc67 (patch) | |
| tree | 8bd6c35cce70d1af280ae48f0d7bcf85b1d91201 /src/lib | |
| parent | 9f0c750c05a6ae53a00e7f238b9b1610f3dc05c6 (diff) | |
| parent | b04e6394c5c5d882800f5215feae89d76a18af23 (diff) | |
Merge branch 'main' into feature/localization-again
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 406775d..4016c92 100644 --- a/src/lib/config.ts +++ b/src/lib/config.ts @@ -29,6 +29,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,  | 
