summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRaphael <mail@raphaelkabo.com>2025-04-23 10:44:47 +0100
committerGitHub <noreply@github.com>2025-04-23 10:44:47 +0100
commitebe7a6182fe84ac0ed49b75e40e60b54d414fe36 (patch)
tree231fe85c024a33ab735bfce1eb786e9e074c59b9
parentfc78f8eb7723db727484d83016c7a633a03dbacb (diff)
parent2520ba69933e17f6ef7f8cc11f803de3e66b179f (diff)
Merge pull request #200 from halkeye/allow-nodemailer-url
Allow url with config information for nodemailer
-rw-r--r--src/lib/config.ts1
-rw-r--r--src/lib/email.ts80
-rwxr-xr-xsrc/routes.js44
3 files changed, 49 insertions, 76 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,
diff --git a/src/routes.js b/src/routes.js
index 1c132ca..e758e6b 100755
--- a/src/routes.js
+++ b/src/routes.js
@@ -24,6 +24,7 @@ import EventGroup from "./models/EventGroup.js";
import path from "path";
import { activityPubContentType } from "./lib/activitypub.js";
import { hashString } from "./util/generator.js";
+import { initEmailService } from "./lib/email.js";
const config = getConfig();
const domain = config.general.domain;
@@ -43,46 +44,9 @@ const nanoid = customAlphabet(
const router = express.Router();
let sendEmails = false;
-let nodemailerTransporter;
-if (config.general.mail_service) {
- switch (config.general.mail_service) {
- case "sendgrid":
- sgMail.setApiKey(config.sendgrid?.api_key);
- console.log("Sendgrid is ready to send emails.");
- sendEmails = true;
- break;
- case "nodemailer":
- const nodemailerConfig = {
- host: config.nodemailer?.smtp_server,
- port: Number(config.nodemailer?.smtp_port) || 587,
- };
-
- if (config.nodemailer?.smtp_username) {
- nodemailerConfig.auth = {
- user: config.nodemailer?.smtp_username,
- pass: config.nodemailer?.smtp_password
- };
- }
-
- nodemailerTransporter = nodemailer.createTransport(nodemailerConfig);
-
- nodemailerTransporter.verify((error, success) => {
- if (error) {
- console.log(error);
- } else {
- console.log(
- "Nodemailer SMTP server is ready to send emails.",
- );
- sendEmails = true;
- }
- });
- break;
- default:
- console.error(
- "You have not configured this Gathio instance to send emails! This means that event creators will not receive emails when their events are created, which means they may end up locked out of editing events. Consider setting up an email service.",
- );
- }
-}
+initEmailService().then((emailService) => {
+ sendEmails = emailService
+});
router.use(fileUpload());