diff --git a/package-lock.json b/package-lock.json index f2ec33fb..8188397c 100644 Binary files a/package-lock.json and b/package-lock.json differ diff --git a/package.json b/package.json index fc7bb8dd..174bea99 100644 --- a/package.json +++ b/package.json @@ -125,9 +125,9 @@ "@sendgrid/mail": "^8.1.6", "@yukikaze-bot/erlpack": "^1.0.1", "jimp": "^1.6.0", + "mailgun.js": "^12.1.0", "mysql": "^2.18.1", "nodemailer": "^7.0.6", - "nodemailer-mailgun-transport": "^2.1.5", "nodemailer-mailjet-transport": "github:n0script22/nodemailer-mailjet-transport", "pg": "^8.16.3", "sqlite3": "^5.1.7" diff --git a/src/util/config/types/subconfigurations/email/MailGun.ts b/src/util/config/types/subconfigurations/email/MailGun.ts index f743f1ad..a559be29 100644 --- a/src/util/config/types/subconfigurations/email/MailGun.ts +++ b/src/util/config/types/subconfigurations/email/MailGun.ts @@ -17,6 +17,8 @@ */ export class MailGunConfiguration { + username: string | null = null; apiKey: string | null = null; domain: string | null = null; + isEuropean: boolean = true; } diff --git a/src/util/util/email/clients/MailGunEmailClient.ts b/src/util/util/email/clients/MailGunEmailClient.ts new file mode 100644 index 00000000..60493de3 --- /dev/null +++ b/src/util/util/email/clients/MailGunEmailClient.ts @@ -0,0 +1,47 @@ +import { BaseEmailClient, IEmail } from "./IEmailClient"; +import { Config } from "@spacebar/util*"; + +// NOTE: mailgun supports SMTP, is there any point in maintaining this? +export class MailGunEmailClient extends BaseEmailClient { + mailGun?: unknown; + mailGunClient?: unknown; + // mailGun?: import("mailgun.js").default; // for development - doesn't work if package isn't installed + // mailGunClient?: import("mailgun.js/Classes/MailgunClient").default; // for development - doesn't work if package isn't installed + override async init(): Promise { + // 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)); -}