Configuration
The first step we need to do is import the Mailman
module. Mailman supports both static and dynamic imports style of configuration. For more information on modules visit modules section of the official NestJS documentation.
Mailman module is global-scoped and it is recommended to register it in the root module.
Static Import
To import the module statically, you can do
import { Module } from '@nestjs/common';
import { MailmanModule } from '@squareboat/nest-mailman';
@Module({
imports: [
MailmanModule.register({
host: 'smtp.mailtrap.io',
port: 2525,
username: 'xxxxxxx',
password: 'xxxxxxx',
from: 'sender@emailaddress.com',
path: '/your-project-path/resources/mails',
}),
],
controllers: [],
providers: [],
})
export class AppModule {}
Dynamic Import (Recommended)
To import the module dynamically, we first need to make sure that the configuration options are set.
Mailman Options
import { registerAs } from '@nestjs/config';
import { MailmanOptions } from '@squareboat/nest-mailman';
export default registerAs('mailman', () => {
return {
host: process.env.MAIL_HOST,
port: +process.env.MAIL_PORT,
username: process.env.MAIL_USERNAME,
password: process.env.MAIL_PASSWORD,
from: process.env.MAIL_SENDER_ID,
path: '/project-dir/resources/mails'
} as MailmanOptions;
});
from
- The from address in the mail envelope. Note: This can also be overridden on a per-mail basis.path
- The default prefix where mailman looks up for template files.mjml?
- The optional configuration options to be passed for MJML compilation. All the available options are available at MJML options.
MAIL_HOST
, MAIL_PORT
, MAIL_USERNAME
, MAIL_PASSWORD
, MAIL_SENDER_ID
are environment variables and live in .env(.local or .production) file.
Remember to load the configuration in ConfigModule
. You can read more about this here.
Now that the configuration is loaded, you can import your module asynchronously.
import { Module } from '@nestjs/common';
import { AppService } from './app.service';
import { MailmanModule } from '@squareboat/nest-mailman';
import { ConfigService } from '@nestjs/config';
@Module({
imports: [
MailmanModule.registerAsync({
imports: [ConfigService],
useFactory: (config: ConfigService) => config.get('mailman'),
inject: [ConfigService],
}),
],
providers: [AppService],
})
export class AppModule {}