Skip to content

Commit 79fac06

Browse files
author
HunteRoi
committed
feat: use SMTP instead of 3rd party service
1 parent 1b0315f commit 79fac06

10 files changed

Lines changed: 56 additions & 20 deletions

File tree

#.env

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ POSTGRES_PASSWORD=
55
DATABASE_PORT=5432
66
DATABASE_HOST=localhost
77
NODE_ENV=development
8-
SENDGRID_API_KEY=
8+
SMTP_USER=
9+
SMTP_PASS=

config.development.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,8 @@
146146
"ok_hand": "👌",
147147
"warning": "⚠️",
148148
"communicationServiceOptions": {
149-
"mailData": {
150-
"from": "mdpdevti@henallux.be",
151-
"templateId": "d-c8892944b4f2417bafece2d7a5b73d1f"
152-
}
149+
"from": "noreply-discord@henallux.be",
150+
"port": 587,
151+
"host": "smtp.office365.com"
153152
}
154153
}

config.production.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,8 @@
154154
"ok_hand": "👌",
155155
"warning": "⚠️",
156156
"communicationServiceOptions": {
157-
"mailData": {
158-
"from": "mdpdevti@henallux.be",
159-
"templateId": "d-ae8dfdd7cc9c49f1af6e016ecbb4d856"
160-
}
157+
"from": "noreply-discord@henallux.be",
158+
"port": 587,
159+
"host": "smtp.office365.com"
161160
}
162161
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@
2525
"@hunteroi/discord-selfrole": "^4.0.4",
2626
"@hunteroi/discord-temp-channels": "^3.3.0",
2727
"@hunteroi/discord-verification": "^1.5.0",
28-
"@sendgrid/mail": "8.1.3",
2928
"discord.js": "^14.16.2",
3029
"dotenv": "^16.4.5",
3130
"ts-postgres": "1.3.0"
31+
"nodemailer": "^6.9.1",
3232
},
3333
"devDependencies": {
3434
"@types/node": "^20.12.7",
35+
"@types/nodemailer": "^6.4.7",
3536
"@typescript-eslint/eslint-plugin": "^7.6.0",
3637
"@typescript-eslint/parser": "^7.6.0",
3738
"eslint": "^9.0.0",

src/config.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ const defaultConfig: Configuration = {
3131
ok_hand: '',
3232
warning: '',
3333
communicationServiceOptions: {
34-
apiKey: '',
35-
mailData: { from: '', templateId: '' }
34+
auth: { user: '', pass: '' },
35+
from: '',
36+
port: 587,
37+
host: ''
3638
}
3739
};
3840

@@ -48,7 +50,8 @@ export async function readConfig(): Promise<Configuration> {
4850
}
4951

5052
const config = { ...json, version: `${environment}-v${packageInfo.version}` };
51-
config.communicationServiceOptions.apiKey = process.env.SENDGRID_API_KEY;
53+
config.communicationServiceOptions.auth.user = process.env.SMTP_USER;
54+
config.communicationServiceOptions.auth.pass = process.env.SMTP_PASS;
5255

5356
return config;
5457
}

src/datadrop.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@ import { LogEventLevel, DefaultLogger, ConsoleLogger } from '@hunteroi/advanced-
55
import { InteractionsSelfRoleManager, RoleToEmojiData, SelfRoleManagerEvents } from '@hunteroi/discord-selfrole';
66
import { ChildChannelData, ParentChannelData, TempChannelsManager, TempChannelsManagerEvents } from '@hunteroi/discord-temp-channels';
77
import { VerificationManager, VerificationManagerEvents } from '@hunteroi/discord-verification';
8-
import { SendGridService } from '@hunteroi/discord-verification/lib/services/SendGridService.js';
8+
import { SendGridService } from '@hunteroi/discord-verification/lib/services/SendGridService';
9+
import * as path from 'path';
910

10-
import PostgresDatabaseService from './services/PostgresDatabaseService.js';
11+
import { PostgresDatabaseService, SMTPService } from './services/index.js';
1112
import { getErrorMessage, readFilesFrom } from './helpers.js';
12-
import { Configuration } from './models/Configuration.js';
13+
import { Configuration, User, IDatabaseService } from './models/index.js';
1314
import { readConfig } from './config.js';
14-
import { User } from './models/User.js';
15-
import { IDatabaseService } from './models/IDatabaseService.js';
1615

1716
export class DatadropClient extends Client {
1817
#config: Configuration;
@@ -44,7 +43,7 @@ export class DatadropClient extends Client {
4443
this.tempChannelsManager = new TempChannelsManager(this);
4544

4645
this.database = new PostgresDatabaseService(this.logger);
47-
const communicationService = new SendGridService(config.communicationServiceOptions);
46+
const communicationService = new SMTPService(config.communicationServiceOptions);
4847
this.verificationManager = new VerificationManager(this, this.database, communicationService, {
4948
codeGenerationOptions: { length: 6 },
5049
maxNbCodeCalledBeforeResend: 3,

src/models/Configuration.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { Snowflake } from 'discord.js';
22

33
import { SendGridOptions } from '@hunteroi/discord-verification/lib/services/SendGridService.js';
44

5+
import { SMTPServiceOptions } from '../services/SMTPService.js';
6+
57
export interface SpecialRoleConfiguration {
68
roleid: Snowflake;
79
emote: string;
@@ -62,5 +64,5 @@ export interface Configuration {
6264
ok_hand: string;
6365
warning: string;
6466

65-
communicationServiceOptions: SendGridOptions;
67+
communicationServiceOptions: SMTPServiceOptions;
6668
}

src/models/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from './Configuration';
2+
export * from './IDatabaseService';
3+
export * from './User';

src/services/SMTPService.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { createTransport } from 'nodemailer';
2+
import SMTPTransport from 'nodemailer/lib/smtp-transport';
3+
import { ISenderAPI, SenderAPIData } from '@hunteroi/discord-verification';
4+
5+
export type SMTPServiceOptions = SMTPTransport.Options;
6+
7+
export default class SMTPService implements ISenderAPI {
8+
#options: SMTPServiceOptions;
9+
10+
constructor(options: SMTPServiceOptions) {
11+
this.#options = options;
12+
}
13+
14+
async send({ name, code, ...data }: SenderAPIData): Promise<void> {
15+
const transporter = createTransport(this.#options);
16+
await transporter.verify();
17+
await transporter.sendMail({
18+
from: this.#options.from,
19+
to: data.to,
20+
subject: 'Discord Authentication Code',
21+
text: `Hello ${name}! Your code is ${code}. See you soon o/`,
22+
html: `<p>Hello ${name}!</p><p>Your code is ${code}.</p><p>See you soon o/</p>`
23+
});
24+
}
25+
}

src/services/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import PostgresDatabaseService from './PostgresDatabaseService';
2+
import SMTPService from './SMTPService';
3+
4+
export { PostgresDatabaseService, SMTPService };

0 commit comments

Comments
 (0)