Appearance
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:
- SMTP_HOST: The hostname of your SMTP server.
- SMTP_PORT: The port number used for connecting to the SMTP server.
- SMTP_USER: The username required for authenticating with the SMTP server.
- SMTP_PASS: The password required for authenticating with the SMTP server
- SMTP_DEFAULT_FROM_NAME: The default sender's name for your emails.
- 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:
- In your
app.module.ts
, import the required modules and configurations:
ts
import { MailerModule } from '@deeepvision/nest-kit';
import * as configs from './config';
- Inside the
@Module
decorator, use theMailerModule.forRootAsync()
method to initialize theMailerModule
with theMailerConfig
. Pass a configuration object with the following properties:
useFactory
: An async factory function that takes injectedopts
(aConfigType
ofMailerConfig
) as an argument.inject
: An array containing the injection tokenconfigs.MailerConfig.KEY
to properly inject theDbConfig
.
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.