February 2019
Beginner
694 pages
18h 4m
English
The code that we have built to send an email, can be refactored into a single class, which encapsulates all of the setup code for us. The MailService.ts file is as follows:
import * as nodemailer from 'nodemailer';
export class MailService {
private _transporter: nodemailer.Transporter;
constructor() {
this._transporter = nodemailer.createTransport(
`smtp://localhost:1025`
);
}
sendMail(to: string, subject: string, content: string) {
let options = {
from: 'from_test@gmail.com',
to: to,
subject: subject,
text: content
}
this._transporter.sendMail(
options, (error, info) => {
if (error) {
return console.log(`error: ${error}`);
}
console.log(`Message Sent ${info.response}`);
});
}
}
Here, we have built a class named MailService ...
Read now
Unlock full access