From 7053c16344b55756550924a31a2103f845e4d4f4 Mon Sep 17 00:00:00 2001 From: Rory& Date: Tue, 30 Sep 2025 04:50:01 +0200 Subject: [PATCH] Port MailGun transport --- package-lock.json | Bin 398511 -> 397416 bytes package.json | 2 +- .../types/subconfigurations/email/MailGun.ts | 2 + .../util/email/clients/MailGunEmailClient.ts | 47 +++++++++++++++ .../{SMTPTransport.ts => SMTPEmailClient.ts} | 2 +- src/util/util/email/index.ts | 8 +-- src/util/util/email/transports/MailGun.ts | 54 ------------------ 7 files changed, 55 insertions(+), 60 deletions(-) create mode 100644 src/util/util/email/clients/MailGunEmailClient.ts rename src/util/util/email/clients/{SMTPTransport.ts => SMTPEmailClient.ts} (97%) delete mode 100644 src/util/util/email/transports/MailGun.ts diff --git a/package-lock.json b/package-lock.json index f2ec33fbe354bef0be3e40de31dc18e411cb5cb0..8188397ce90c597c4977fdc7a9cd54a4e7ae295f 100644 GIT binary patch delta 257 zcmV+c0sj84sTk;>7_cP)vp@lC1GC5k1qPRyfCLT%F)}VOlfnutm!N_K1e3Z14ucX4 zhY||`w-O5i5~PRDu>!Zwu>-SV0x>d|L1Y6mm$VZE7y&VtQDy@qRYPPpazrm-S#NVe zbxUJNNjYgqWM@V;ZZxrm zGB<8CYE4>baB^5vOKxU#V^%UkZ8?{rx&khjkZ}bamyi?$3j{GTE-{y$6a+b!fLa40 zldzj1mk=NX1-J4P1iEIISULd}m*B|)8<)UC0U?*#eFO`aR)Pc*w+4I!8v}>efCRVK HfCbo-U8+_F delta 873 zcmZXSJ!}(66vwseSq_=65FiPkH$h4PmhH8jwJ|j017mw_Y$vfVi4fA*JG)+IcGlTl zdx`lJS47tV*4qxatL|A%9L zJ{|jOf;({D##GTPT?rdVCnwLslczV1bB?e3#(!+%9uRhPH~iz>&wgQ4Ytf)%A!;{G z%h?WIiH0H}>DNvF`lg@jZTk5mPXyZ}y27^3a-%>DMI=^w%twx!v@atEhlAnT_bS+r zJle~RS7v{ij8n_bXSKED%IZ|L=t!1~W~@Xj6%w$6i*8^#1aX#;|M}}{djjF=%<$-TuI}#E@VgMo=>JF@X zPZeVd4O77u)jL>$uI|*Kjoa8l1QL^KLF6Lc@f=mZioG_YNw9&br-Wdh{hWFHXm>OHX zFJyMKa84jbPm(1LG)R>QYoaSF1a;zHdVezHO-}K%UNT&lgMh*q!OncIcY{yD}jO> zo!6(0?1CJX=Bg`G4JnaU0&I0ZsQ6-`7<;=PB-z?Oz7e+PAlTU}41=D^{s#O}w(lz# zXSXi{!1C9?Pwe#?80tEUBdqY!7hw;tfn7aMddKe`go%CljXT6{{L97q32__5LErJH zq26t_b!-KHuvt*%CMxw*qF7i#S5sQPy>vm5P&$W7*-TW5M2HeVB~sE8*>t>mu~Bc3 zi@HHFR<0#2&cr&2xUAqfj!T7%n@f^{vM|NY-w{r*;s`f#aDbpb{e5`v hYQjYKcmF { + // get configuration + const { apiKey, username, domain, isEuropean } = Config.get().email.mailgun; + + // ensure all required configuration values are set + if (!apiKey || !domain || !username) return console.error("[Email] Mailgun has not been configured correctly."); + + try { + // try to import the transporter package + this.mailGun = new (await import("mailgun.js")).default(FormData); + } catch { + // if the package is not installed, log an error and return void so we don't set the transporter + console.error("[Email] MailGun transport is not installed. Please run `npm install mailgun.js --save-optional` to install it."); + return; + } + + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-expect-error + this.mailGun.client({ username: username, key: apiKey, url: isEuropean ? "https://api.eu.mailgun.net" : undefined }); + } + + override async sendMail(email: IEmail): Promise { + if (!this.mailGun) throw new Error("MailGun not initialized"); + if (!this.mailGunClient) throw new Error("MailGun not initialized"); + const { domain } = Config.get().email.mailgun; + if (!domain) throw new Error("MailGun domain not configured"); + + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-expect-error + await this.mailGunClient.messages.create(domain, { + to: email.to, + from: email.from, + subject: email.subject, + text: email.text, + html: email.html, + }); + } +} diff --git a/src/util/util/email/clients/SMTPTransport.ts b/src/util/util/email/clients/SMTPEmailClient.ts similarity index 97% rename from src/util/util/email/clients/SMTPTransport.ts rename to src/util/util/email/clients/SMTPEmailClient.ts index 828c4a3e..6ef0e3f8 100644 --- a/src/util/util/email/clients/SMTPTransport.ts +++ b/src/util/util/email/clients/SMTPEmailClient.ts @@ -1,7 +1,7 @@ import { BaseEmailClient, IEmail } from "./IEmailClient"; import { Config } from "@spacebar/util*"; -export class SMTPTransport extends BaseEmailClient { +export class SMTPEmailClient extends BaseEmailClient { // sendGrid?: unknown; nodemailer?: typeof import("nodemailer"); transporter: import("nodemailer").Transporter; diff --git a/src/util/util/email/index.ts b/src/util/util/email/index.ts index 8223fa55..3bef048e 100644 --- a/src/util/util/email/index.ts +++ b/src/util/util/email/index.ts @@ -18,13 +18,13 @@ import fs from "fs/promises"; import path from "node:path"; -import { SentMessageInfo, Transporter } from "nodemailer"; import { User } from "../../entities"; import { Config } from "../Config"; import { generateToken } from "../Token"; import { BaseEmailClient, IEmail, IEmailClient } from "./clients/IEmailClient"; import { SendGridEmailClient } from "./clients/SendGridEmailClient"; -import { SMTPTransport } from "./clients/SMTPTransport"; +import { SMTPEmailClient } from "./clients/SMTPEmailClient"; +import { MailGunEmailClient } from "./clients/MailGunEmailClient"; const ASSET_FOLDER_PATH = path.join( __dirname, @@ -79,13 +79,13 @@ export const Email: { switch (provider) { case "smtp": - this.transporter = new SMTPTransport(); + this.transporter = new SMTPEmailClient(); break; case "sendgrid": this.transporter = new SendGridEmailClient(); break; case "mailgun": - this.transporter = new BaseEmailClient(); + this.transporter = new MailGunEmailClient(); break; case "mailjet": this.transporter = new BaseEmailClient(); diff --git a/src/util/util/email/transports/MailGun.ts b/src/util/util/email/transports/MailGun.ts deleted file mode 100644 index badc9880..00000000 --- a/src/util/util/email/transports/MailGun.ts +++ /dev/null @@ -1,54 +0,0 @@ -/* - Spacebar: A FOSS re-implementation and extension of the Discord.com backend. - Copyright (C) 2023 Spacebar and Spacebar Contributors - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published - by the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . -*/ - -import { Config } from "@spacebar/util"; -import nodemailer from "nodemailer"; - -export default async function () { - // get configuration - const { apiKey, domain } = Config.get().email.mailgun; - - // ensure all required configuration values are set - if (!apiKey || !domain) - return console.error( - "[Email] Mailgun has not been configured correctly.", - ); - - let mg; - try { - // try to import the transporter package - mg = require("nodemailer-mailgun-transport"); - } catch { - // if the package is not installed, log an error and return void so we don't set the transporter - console.error( - "[Email] Mailgun transport is not installed. Please run `npm install nodemailer-mailgun-transport --save-optional` to install it.", - ); - return; - } - - // create the transporter configuration object - const auth = { - auth: { - api_key: apiKey, - domain: domain, - }, - }; - - // create the transporter and return it - return nodemailer.createTransport(mg(auth)); -}