Mailer Module
Project-Setup
Changes in files
/api/src/config/environment-variables.ts
@IsString()
MAILER_HOST: string;
@Type(() => Number)
@IsInt()
MAILER_PORT: number;
@IsUndefinable()
@IsArray()
@Transform(({ value }) => value.split(","))
@IsEmail({}, { each: true })
MAILER_SEND_ALL_MAILS_TO?: string[];
@IsUndefinable()
@IsArray()
@Transform(({ value }) => value.split(","))
@IsEmail({}, { each: true })
MAILER_SEND_ALL_MAILS_BCC?: string;
/api/src/config/config.ts
mailer: {
// Mailer configuration
defaultFrom: '"Comet Demo" <comet-demo@comet-dxp.com>',
sendAllMailsTo: envVars.MAILER_SEND_ALL_MAILS_TO,
sendAllMailsBcc: envVars.MAILER_SEND_ALL_MAILS_BCC,
transport: { // nodemailer configuration
host: envVars.MAILER_HOST,
port: envVars.MAILER_PORT,
}
},
/docker-compose.yml
mailhog:
image: mailhog/mailhog
ports:
- 1025:1025
- 8025:8025
/.env
# mailer
MAILER_HOST=localhost
MAILER_PORT=1025
MAILER_SEND_ALL_MAILS_TO=demo-leaddev@comet-dxp.com,demo-pm@comet-dxp.com
/api/src/app.module.ts
imports: [
...
MailerModule.register(config.mailer),
]
/deployment/helm/values.tpl.yaml
api:
env:
...
MAILER_HOST: "localhost"
MAILER_PORT: 25
MAILER_SEND_ALL_MAILS_TO: "demo-leaddev@comet-dxp.com,demo-pm@comet-dxp.com"
Usage
Sending a Mail
Inject the MailerService
into your service or controller: (MailerModule is global, so no module import is needed)
@Resolver(() => Product)
@RequiredPermission(["products"], { skipScopeCheck: true })
export class CustomProductResolver {
constructor(
private readonly mailerService: MailerService,
...,
) {}
...
}
send mail
@Mutation(() => Boolean)
async publishAllProducts(): Promise<boolean> {
...
await this.mailerService.sendMail({
type: "products-published",
to: "product-manager@comet-dxp.com",
cc: "vice-product-manager@comet-dxp.com",
subject: "All products have been published",
});
...
}