From 4383fcd4497c67e34d27fc0806824650df34466a Mon Sep 17 00:00:00 2001 From: Puyodead1 Date: Sat, 21 Jan 2023 13:03:17 -0500 Subject: [PATCH] Add Mailgun transport --- package-lock.json | Bin 273528 -> 276467 bytes package.json | 3 +- src/util/config/types/EmailConfiguration.ts | 13 +++-- .../types/subconfigurations/email/MailGun.ts | 22 ++++++++ .../types/subconfigurations/email/SMTP.ts | 25 +++++++++ .../types/subconfigurations/email/index.ts | 20 +++++++ src/util/util/Email.ts | 51 ++++++++++++++++-- 7 files changed, 123 insertions(+), 11 deletions(-) create mode 100644 src/util/config/types/subconfigurations/email/MailGun.ts create mode 100644 src/util/config/types/subconfigurations/email/SMTP.ts create mode 100644 src/util/config/types/subconfigurations/email/index.ts diff --git a/package-lock.json b/package-lock.json index 8d7b1db285116ba26abd76a989ef88a953c227d5..a07a7ec7553991efe4baebfe83eb6df91b947ec7 100644 GIT binary patch delta 1535 zcmaJ>OQ;)V6lRiJ?Jc&{w)bjVTknl1YD_ZkNpvB3zmu8V%;W(IGnvU`CX<<)d1R7+ z(2L-vQYpdyh%S0@A-Ir=NEW(i5d^ysi<|mf3A&LMx=`HcB=?p+7S87X|8ow^`Mz_$ z@9&#O@7y|i#aMk#H$-W`E1G167KiNUA=~72YuhmGz{?wf*U>N%Cf<}+=khA_w7bHO z6$9*i39b9W7}(tL!H+K@RwNWzo*~2wU;N&;$=XJ15KLGTjpUMqyVdXZ1)-84Yt<1| zY{f7-ok*0(@g&um^7$NZ#g!56(nBWF!E36)OqK0%Erm7+8AUqPA>(Lm%FMZdf7^F* zcKM&DPInDWGu)6ViHa$8Y-`yo5=MwH3~ooD_1Uk!!{DaoUxSbt)I2+X>m2kL!u zQOv_Zj?i%ycDnT>Tkh!$jEf3i%SY4A2-BgvLyekhy*8!dQ(2zHe-F_A_wKB762 zpqOCz2ejtL!W8)QeFy^Xrc{Wr^d=wT%_t{Or7?} z*=8bJw;M%gt2si(EEnXbN@=CzY)E{6|4V=_Xt6Y#LvHmbF&^BQf z`J_oX8GGBn2V3l@l*_Y1A*~MJkrCuu1XFSo>=4Toal57$%t#Zatqz>T)o!F0%-PMY zGT1y1EhlYu+Ow?&9sLfRb(QIrZOk-TBNPJKq1t6X-NJ7xk?I4AE5~yZUQ#1S%>tVr`A!1(1^<~>7y90p7Z%0> zFD=mo_Jy;bfWz?Jgkx$U)ll?h1`oqC#yAT$e)AoekACviIB*&EA3XA)*~~V6eiB^1 z0wvM. */ +import { + MailGunConfiguration, + SMTPConfiguration, +} from "./subconfigurations/email"; + export class EmailConfiguration { - host: string | null = null; - port: number | null = null; - secure: boolean | null = null; - username: string | null = null; - password: string | null = null; + provider: string | null = null; + smtp: SMTPConfiguration = new SMTPConfiguration(); + mailgun: MailGunConfiguration = new MailGunConfiguration(); } diff --git a/src/util/config/types/subconfigurations/email/MailGun.ts b/src/util/config/types/subconfigurations/email/MailGun.ts new file mode 100644 index 00000000..52cd9069 --- /dev/null +++ b/src/util/config/types/subconfigurations/email/MailGun.ts @@ -0,0 +1,22 @@ +/* + Fosscord: A FOSS re-implementation and extension of the Discord.com backend. + Copyright (C) 2023 Fosscord and Fosscord 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 . +*/ + +export class MailGunConfiguration { + apiKey: string | null = null; + domain: string | null = null; +} diff --git a/src/util/config/types/subconfigurations/email/SMTP.ts b/src/util/config/types/subconfigurations/email/SMTP.ts new file mode 100644 index 00000000..11eb9e14 --- /dev/null +++ b/src/util/config/types/subconfigurations/email/SMTP.ts @@ -0,0 +1,25 @@ +/* + Fosscord: A FOSS re-implementation and extension of the Discord.com backend. + Copyright (C) 2023 Fosscord and Fosscord 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 . +*/ + +export class SMTPConfiguration { + host: string | null = null; + port: number | null = null; + secure: boolean | null = null; + username: string | null = null; + password: string | null = null; +} diff --git a/src/util/config/types/subconfigurations/email/index.ts b/src/util/config/types/subconfigurations/email/index.ts new file mode 100644 index 00000000..92fe9184 --- /dev/null +++ b/src/util/config/types/subconfigurations/email/index.ts @@ -0,0 +1,20 @@ +/* + Fosscord: A FOSS re-implementation and extension of the Discord.com backend. + Copyright (C) 2023 Fosscord and Fosscord 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 . +*/ + +export * from "./MailGun"; +export * from "./SMTP"; diff --git a/src/util/util/Email.ts b/src/util/util/Email.ts index 9688c3c5..b8019cbd 100644 --- a/src/util/util/Email.ts +++ b/src/util/util/Email.ts @@ -55,6 +55,8 @@ export function adjustEmail(email?: string): string | undefined { export const Email: { transporter: Transporter | null; init: () => Promise; + initSMTP: () => Promise; + initMailgun: () => Promise; generateVerificationLink: (id: string, email: string) => Promise; sendVerificationEmail: (user: User, email: string) => Promise; doReplacements: ( @@ -72,9 +74,22 @@ export const Email: { } = { transporter: null, init: async function () { - const { host, port, secure, username, password } = Config.get().smtp; - if (!host || !port || !secure || !username || !password) return; - console.log(`[SMTP] connect: ${host}`); + const { provider } = Config.get().email; + if (!provider) return; + + if (provider === "smtp") await this.initSMTP(); + else if (provider === "mailgun") await this.initMailgun(); + else throw new Error(`Unknown email provider: ${provider}`); + }, + initSMTP: async function () { + const { host, port, secure, username, password } = + Config.get().email.smtp; + if (!host || !port || !secure || !username || !password) + return console.error( + "[Email] SMTP has not been configured correctly.", + ); + + console.log(`[Email] Initializing SMTP transport: ${host}`); this.transporter = nodemailer.createTransport({ host, port, @@ -87,14 +102,40 @@ export const Email: { await this.transporter.verify((error, _) => { if (error) { - console.error(`[SMTP] error: ${error}`); + console.error(`[Email] SMTP error: ${error}`); this.transporter?.close(); this.transporter = null; return; } - console.log(`[SMTP] Ready`); + console.log(`[Email] Ready`); }); }, + initMailgun: async function () { + const { apiKey, domain } = Config.get().email.mailgun; + if (!apiKey || !domain) + return console.error( + "[Email] Mailgun has not been configured correctly.", + ); + + try { + const mg = require("nodemailer-mailgun-transport"); + const auth = { + auth: { + api_key: apiKey, + domain: domain, + }, + }; + + console.log(`[Email] Initializing Mailgun transport...`); + this.transporter = nodemailer.createTransport(mg(auth)); + console.log(`[Email] Ready`); + } catch { + console.error( + "[Email] Mailgun transport is not installed. Please run `npm install nodemailer-mailgun-transport --save` to install it.", + ); + return; + } + }, /** * Replaces all placeholders in an email template with the correct values */