Hello folks! We are super excited to release the first version of nest-mailman. Mailman allows you to send mails from your NestJS applications with breeze. This blog post provides a simple overview of the package, complete documentation is available at nest-mailman.
A little background
Over the last couple of months, we at squareboat have gotten into the realms of NestJS.
Let's accept this that Node.js projects with its level of flexibility can easily go off hand. After months of hustling to get the right Node.js project structure, we stumbled upon NestJS and haven't looked back since.
Our motivation with nest-mailman and other nest packages that we have been working on is to provide utility packages that will solve most of your application use cases.
Enough talk show me the code!
Installation
To get up and running with mailman run:
npm i @squareboat/nest-mailman --save
Usage
We start by importing MailmanModule
and registering it with configuration options. For the sake of this article, we will be using the register
construct to register our module statically, although the recommended approach is to provide the config options via registerAsync
. For a detailed description visit https://opensource.squareboat.com/nest-mailman/docs/send-mail.
import { MailmanModule } from '@squareboat/nest-mailman';
@Module({
imports: [
MailmanModule.register({
host: 'smtp.mailtrap.io',
port: 2525,
username: 'c88XXXXXXXXX',
password: 'sdsXXXXXXXXX',
from: 'hello@mailman.com',
path: 'path/to/mail/templates'
}),
],
})
export class TestModule {}
Going along the lines of SOLID principles that NestJS highly emphasizes on, Mailman comes with 2 important APIs - MailMessage
and Mailman
- MailMessage is responsible for building mails whereas Mailman does the work of a transporter (just like how mailmen work in real life, they don't write mails for us, they only take them to the ones intended).
- Mailman accepts an instance of MailMessage and will transport the mails to the specified recipients.
// test.service.ts
import { MailMessage } from '@squareboat/nest-mailman';
@Injectable()
export class TestmailService {
triggerMail() {
// Building mail message
const mailMessage = MailMessage.init();
mailMessage
.subject('Order Received')
.view('orderReceived', { productName: 'XYZ', productId: 123 }); // will look for orderReceived.hbs
// Sending the constructed mail
Mailman
.init()
.to('receiver@gmail.com')
.send(mailMessage);
return 'Done';
}
}
Mailman is configured by default for handlebar templates and will append .hbs
to all the template file names.
Bonus
We understand that writing a template for every mail that your application rolls out is an absolute chore that developers despise, and that's why mailman ships with a very simple generic mail builder API that makes building emails as simple as invoking a function.
triggerMail(){
const mailMessage = MailMessage.init();
mailMessage
.subject('Testing 2')
.greeting('Hello Sarah,')
.line("You've received a new enquiry")
.line('Summary: The product looks promising! would love to setup a call to discuss more')
.action('View Enquiry', 'https://app.test.in/admin/queries/123');
// ... and you can call line(), action() methods any number of times to build your mail
}
And bam๐ our mail is ready.
For a complete overview about nest-mailman visit (https://opensource.squareboat.com/nest-mailman/)
And this is our shot at giving back to the community. Head over to our site to know more about the available packages. We are looking for feedback, criticism, and of course contributions. See you!
Top comments (1)
Hey thank you, I was looking for something like this.