src/emails/emails.service.ts
Properties |
|
constructor(configService: ConfigService)
|
||||||
|
Defined in src/emails/emails.service.ts:75
|
||||||
|
Parameters :
|
| Public Readonly bodies |
Default value : emailBodies()
|
|
Defined in src/emails/emails.service.ts:75
|
| Private Readonly logger |
Default value : new Logger(EmailsService.name)
|
|
Defined in src/emails/emails.service.ts:73
|
| Public Readonly Mailer |
|
Defined in src/emails/emails.service.ts:74
|
| Private Readonly postalBuddy |
Type : Transporter<SentMessageInfo>
|
|
Defined in src/emails/emails.service.ts:72
|
import { Injectable, Logger } from '@nestjs/common';
import { createTransport, Transporter, SentMessageInfo } from 'nodemailer';
import { ConfigService } from '@nestjs/config';
import emailBodies from './comps/emailbuilder';
function MailerFactory(postalBuddy: Transporter<SentMessageInfo>) {
class Mailer {
private readonly subject: string;
private readonly target: string | string[];
private parsable: string;
private attachments: { filename: string; content: Buffer | string }[] = [];
private readonly htmlBody: boolean;
private readonly logger = new Logger('Mailer');
public constructor(
target: string | string[],
subject: string,
html?: boolean,
) {
this.subject = subject;
this.target = target;
this.parsable = '';
this.htmlBody = html || true;
}
/**
* Sets the email's body (should be a html string)
*/
public append(message: string): Mailer {
this.parsable = message;
return this;
}
public attach(data: Buffer, name = 'file'): Mailer {
this.attachments.push({ filename: name, content: data });
return this;
}
/**
* Delivers the email to the address defined in the constructor
*/
public async deliver(): Promise<boolean> {
return new Promise<boolean>(async (resolve) => {
if (typeof this.target == 'string') {
const r = await postalBuddy.sendMail({
from: '"Fileglass" noreply@file.glass',
to: this.target,
subject: this.subject,
html: this.htmlBody ? this.parsable : undefined,
text: !this.htmlBody ? this.parsable : undefined,
attachments: this.attachments,
});
this.logger.log(
`Email sent to: ${this.target} with subject: ${this.subject}`,
);
resolve(true);
} else {
this.logger.error('No target was specified');
resolve(false);
}
});
}
}
return Mailer;
}
declare class Mailer {
constructor(target: string | string[], subject: string, html?: boolean);
public deliver(): Promise<boolean>;
public attach(data: Buffer, name?: string): Mailer;
public append(message: string): Mailer;
}
@Injectable()
export class EmailsService {
private readonly postalBuddy: Transporter<SentMessageInfo>;
private readonly logger = new Logger(EmailsService.name);
public readonly Mailer: typeof Mailer;
public readonly bodies = emailBodies();
constructor(private readonly configService: ConfigService) {
this.postalBuddy = createTransport({
host: 'smtp.sendgrid.net',
auth: {
user: this.configService.get('SG_USER'),
pass: this.configService.get('SG_KEY'),
},
tls: {
servername: 'smtp.sendgrid.net',
},
secure: true,
requireTLS: true,
});
this.logger.log('Email transporter created');
this.Mailer = MailerFactory(this.postalBuddy);
}
}