Port MailGun transport

This commit is contained in:
Rory& 2025-09-30 04:50:01 +02:00
parent 088e7d89ab
commit 7053c16344
7 changed files with 55 additions and 60 deletions

BIN
package-lock.json generated

Binary file not shown.

View File

@ -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"

View File

@ -17,6 +17,8 @@
*/
export class MailGunConfiguration {
username: string | null = null;
apiKey: string | null = null;
domain: string | null = null;
isEuropean: boolean = true;
}

View File

@ -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<void> {
// 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<void> {
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,
});
}
}

View File

@ -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;

View File

@ -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();

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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));
}