Skip to content

Commit

Permalink
use ethereal email
Browse files Browse the repository at this point in the history
  • Loading branch information
f-w committed Dec 14, 2024
1 parent 3e005fe commit 98103f8
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 76 deletions.
57 changes: 0 additions & 57 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
"mongoose": "^8.8.4",
"morgan": "^1.10.0",
"nodemailer": "^6.9.5",
"nodemailer-direct-transport": "^3.3.2",
"pluralize": "^8.0.0",
"randexp": "^0.5.3",
"redis-memory-server": "^0.10.0",
Expand Down
30 changes: 20 additions & 10 deletions src/common/common.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import dns from 'dns';
import { Request } from 'express';
import { get, merge, union } from 'lodash';
import net from 'net';
import { createTransport } from 'nodemailer';
import pluralize from 'pluralize';
import { BouncesService } from 'src/api/bounces/bounces.service';
import { NotificationsService } from 'src/api/notifications/notifications.service';
Expand Down Expand Up @@ -172,19 +173,28 @@ export class CommonService {
}
}

nodemailer = require('nodemailer');
directTransport = require('nodemailer-direct-transport');
transport: any;
async sendEmail(mailOptions: any, priority = 5) {
const smtpCfg =
this.appConfig.email.smtp || this.appConfig.email.defaultSmtp;
const smtpCfg = this.appConfig.email.smtp;
if (!this.transport) {
if (smtpCfg.direct) {
this.transport = this.nodemailer.createTransport(
this.directTransport(smtpCfg),
);
if (!smtpCfg?.host) {
// create ethereal.email and transport
const data = await this.configurationsService.findOne({
where: {
name: 'etherealAccount',
},
});
this.transport = createTransport({
host: data.value.smtp.host,
port: data.value.smtp.port,
secure: data.value.smtp.secure,
auth: {
user: data.value.user, // generated ethereal user
pass: data.value.pass, // generated ethereal password
},
});
} else {
this.transport = this.nodemailer.createTransport(smtpCfg);
this.transport = createTransport(smtpCfg);
}
}
let info;
Expand Down Expand Up @@ -214,7 +224,7 @@ export class CommonService {
// do client retry if there are multiple addresses
for (const [index, address] of addresses.entries()) {
const newSmtpCfg = { ...smtpCfg, host: address.address };
const transport = this.nodemailer.createTransport(newSmtpCfg);
const transport = createTransport(newSmtpCfg);
let sendMail = transport.sendMail.bind(transport);
if (this.appConfig?.email?.throttle?.enabled) {
sendMail = this.rateLimit(this.emailQueue, sendMail, { priority });
Expand Down
4 changes: 0 additions & 4 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ const config: Record<string, any> = {
adminIps: ['127.0.0.1'],
siteMinderReverseProxyIps: ['127.0.0.1'],
email: {
defaultSmtp: {
direct: true,
name: 'localhost',
},
inboundSmtpServer: {
enabled: true,
},
Expand Down
38 changes: 34 additions & 4 deletions src/observers/smtp.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,22 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import { Injectable, OnApplicationBootstrap } from '@nestjs/common';
import { Injectable, Logger, OnApplicationBootstrap } from '@nestjs/common';
import { createTestAccount } from 'nodemailer';
import { ConfigurationsService } from 'src/api/configurations/configurations.service';
import { AppConfigService } from 'src/config/app-config.service';
import { promisify } from 'util';
import { smtpServer } from './smtp-server';

@Injectable()
export class SmtpService implements OnApplicationBootstrap {
readonly appConfig;
constructor(appConfigService: AppConfigService) {
private readonly logger = new Logger(SmtpService.name);

constructor(
appConfigService: AppConfigService,
private readonly configurationsService: ConfigurationsService,
) {
this.appConfig = appConfigService.get();
}

Expand All @@ -27,9 +36,30 @@ export class SmtpService implements OnApplicationBootstrap {
return;
}
const smtpSvr = this.appConfig.email.inboundSmtpServer;
if (!smtpSvr.enabled) {
if (smtpSvr.enabled) {
await smtpServer(this.appConfig);
}
if (process.env.NOTIFYBC_NODE_ROLE === 'secondary') {
return;
}
if (this.appConfig?.email.smtp?.host) {
return;
}
await smtpServer(this.appConfig);
const name = 'etherealAccount';
const data = await this.configurationsService.findOne({
where: {
name,
},
});
if (data) return;
// create ethereal.email account
const createTestAccountAsync = promisify(createTestAccount);
const etherealAccount = await createTestAccountAsync();
this.logger.log('ethereal email account created:');
this.logger.log(etherealAccount);
await this.configurationsService.create({
name,
value: etherealAccount,
});
}
}

0 comments on commit 98103f8

Please sign in to comment.