Introducing nest-mailman 🎉🎉
Tags: nestjs, node, nodemailer, javascript
Published: 2020-10-26 13:58:05

---
title: Introducing nest-mailman 🎉🎉
published: true
description: 📮 The mailer package for your NestJS Applications
tags: nestjs, node, nodemailer, javascript
//cover_image: https://direct_url_to_image.jpg
---
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](https://opensource.squareboat.com/nest-mailman/).

## A little background
Over the last couple of months, we at [squareboat](https://squareboat.com/) 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](https://github.com/squareboat) 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.

```javascript
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 (<i>just like how mailmen work in real life, they don't write mails for us, they only take them to the ones intended</i>).
- Mailman accepts an instance of MailMessage and will transport the mails to the specified recipients.

```javascript
// 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.

```javascript
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.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/6gdn1y2qyejr4gabcc2n.png)

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](https://github.com/squareboat) to know more about the available packages. We are looking for feedback, criticism, and of course contributions. See you!