Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
♻️ Allow email configuration of SES
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Nov 9, 2020
1 parent 8d30a01 commit fb081a2
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 29 deletions.
15 changes: 9 additions & 6 deletions src/config/configuration.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@ export interface Configuration {
email: {
name: string;
from: string;
host: string;
port: number;
secure: boolean;
auth: {
user: string;
pass: string;
ses?: boolean;
transport?: {
host: string;
port: number;
secure: boolean;
auth: {
user: string;
pass: string;
};
};
};

Expand Down
15 changes: 9 additions & 6 deletions src/config/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,15 @@ const configuration: Configuration = {
email: {
name: process.env.EMAIL_NAME ?? 'Staart',
from: process.env.EMAIL_FROM ?? '',
host: process.env.EMAIL_HOST ?? '',
port: int(process.env.EMAIL_PORT, 587),
secure: !!process.env.EMAIL_SECURE,
auth: {
user: process.env.EMAIL_USER ?? process.env.EMAIL_FROM ?? '',
pass: process.env.EMAIL_PASSWORD ?? '',
ses: !!process.env.EMAIL_SES,
transport: {
host: process.env.EMAIL_HOST ?? '',
port: int(process.env.EMAIL_PORT, 587),
secure: !!process.env.EMAIL_SECURE,
auth: {
user: process.env.EMAIL_USER ?? process.env.EMAIL_FROM ?? '',
pass: process.env.EMAIL_PASSWORD ?? '',
},
},
},
sms: {
Expand Down
12 changes: 0 additions & 12 deletions src/providers/mail/mail.interface.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
export interface MailConfig {
name: string;
from: string;
host: string;
port: number;
secure: boolean;
auth: {
user: string;
pass: string;
};
}

export interface MailOptions {
template?: string;
data?: Record<string, any>;
Expand Down
17 changes: 12 additions & 5 deletions src/providers/mail/mail.service.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
import { Injectable, Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { render } from '@staart/mustache-markdown';
import { SES } from 'aws-sdk';
import { promises as fs } from 'fs';
import mem from 'mem';
import nodemailer from 'nodemailer';
import Mail from 'nodemailer/lib/mailer';
import SESTransport from 'nodemailer/lib/ses-transport';
import PQueue from 'p-queue';
import pRetry from 'p-retry';
import { join } from 'path';
import { MailConfig, MailOptions } from './mail.interface';
import { Configuration } from '../../config/configuration.interface';
import { MailOptions } from './mail.interface';

@Injectable()
export class MailService {
private readonly logger = new Logger(MailService.name);
private transport: Mail;
private config: MailConfig;
private config: Configuration['email'];
private queue = new PQueue({ concurrency: 1 });
private readTemplate = mem(this.readTemplateUnmemoized);

constructor(private configService: ConfigService) {
const emailConfig = this.configService.get<MailConfig>('email');
if (emailConfig) this.config = emailConfig;
this.transport = nodemailer.createTransport(this.config);
this.config = this.configService.get<Configuration['email']>('email');
console.log(this.config);
if (this.config.ses)
this.transport = nodemailer.createTransport({
SES: new SES({ apiVersion: '2010-12-01' }),
} as SESTransport.Options);
else this.transport = nodemailer.createTransport(this.config.transport);
}

send(options: Mail.Options & MailOptions) {
Expand Down

0 comments on commit fb081a2

Please sign in to comment.