Skip to content

Mailer Config

MailerConfig is a configuration for the @nestjs-modules/mailer package, which enables you to easily send emails from your NestJS application. This config allows you to define environment variables for your SMTP server, such as host, port, username, password, and default sender information.

In the provided MailerConfig code, the email templates and partials are stored in specific directories within the project structure.

For email templates, they should be placed in the following directory:

bash
src/resources/emails/

These templates are used to generate the main content of your emails. They can include placeholders for dynamic data and can utilize partials for reusable content.

For email partials, they should be placed in the following directory:

bash
src/resources/emails/partials/

Partials are smaller, reusable template pieces that can be included in the main email templates. They are useful for sharing common content across multiple templates, such as headers, footers, or other repeating elements.

Environment variables used in the MailerConfig:

The following environment variables are used for the MailerConfig in the provided code:

  1. SMTP_HOST: The hostname of your SMTP server.
  2. SMTP_PORT: The port number used for connecting to the SMTP server.
  3. SMTP_USER: The username required for authenticating with the SMTP server.
  4. SMTP_PASS: The password required for authenticating with the SMTP server
  5. SMTP_DEFAULT_FROM_NAME: The default sender's name for your emails.
  6. SMTP_DEFAULT_FROM_ADDRESS: The default sender's email address for your emails.

Here's a YAML formatted example of the environment variables used in the MailerConfig:

yaml
SMTP_HOST: 'sandbox.smtp.mailtrap.io'
SMTP_PORT: 2525
SMTP_USER: '329dedddd76d98'
SMTP_PASS: '1q2w3e4r5t'
SMTP_DEFAULT_FROM_NAME: 'Hope Cloud'
SMTP_DEFAULT_FROM_ADDRESS: '[email protected]'

You can adjust the example values according to your application's needs.

How to create MailerConfig

Here's an example of how to use the createMailerConfig to create MailerConfig:

ts
import { createMailerConfig } from '@deeepvision/nest-kit/dist/configs';

export const MailerConfig = createMailerConfig();

How to use MailerConfig for MailerModule initialization:

  1. In your app.module.ts, import the required modules and configurations:
ts
import { MailerModule } from '@deeepvision/nest-kit';
import * as configs from './config';
  1. Inside the @Module decorator, use the MailerModule.forRootAsync() method to initialize the MailerModule with the MailerConfig. Pass a configuration object with the following properties:
  • useFactory: An async factory function that takes injected opts (a ConfigType of MailerConfig) as an argument.
  • inject: An array containing the injection token configs.MailerConfig.KEY to properly inject the DbConfig.

Here's the complete code:

ts
// src/app.module.ts
import { MailerModule } from '@nestjs-modules/mailer';
import { ConfigModule, ConfigType } from '@nestjs/config';
import * as configs from '@/config';

@Module({
  imports:[
    // Other modules

    // Configure the MailerModule with Winston config
    MailerModule.forRootAsync({
      inject: [configs.MailerConfig.KEY],
      useFactory: async (opts: ConfigType<typeof configs.MailerConfig>) => opts,
    }),
  ],
  ...
})
export class AppModule {}

By following these steps, you'll be able to utilize the MailerConfig for initializing the MailerModule with the appropriate logger settings.

Created by DeepVision Software.