Integrate with Nodemailer and Mailtrap
In this lesson, we will send our first email all from our server with Nodemailer, Mailtrap and of course NestJS.
Set up a nestJS project
Open your favorite IDE or navigate to the terminal to start a new nestJS project or an existing one (if you have been following prior lessons).
See the lesson on creating a nestjs server, if you will be creating a new project.
Now that you have your project setup, install these Nodemailer packages,
bash$ npm i --save @nestjs-modules/mailer nodemailer
$ npm i --save-dev @types/nodemailer
Create mailtrap account and configure
Go to https://mailtrap.io.
If you never created account with them:
- Visit mailtrap.io/home and sign up.
- Complete Onboarding: Provide your name and intended use. When prompted with "Which product(s) do you plan to use?", ensure you select Email Sandbox.
- Select Node.js as your preferred stack. This helps Mailtrap tailor the setup instructions for you.
- From your dashboard, click "Start Testing" under the Email Sandbox section. This will take you to your "Inboxes."
- In the Integration tab, ensure the SMTP option is selected. You will see your specific host, port, username, and password.
- Secure Your Credentials: Copy these values into your
.envfile using the following format:bash
MAIL_HOST=sandbox.smtp.mailtrap.io MAIL_PORT=2525 MAIL_USER=your_username_here MAIL_PASS=your_password_here
Setup the MailerModule the nestjs way
In line with NestJS way, we have to register the MailerModule somewhere in the trees of existing module. This time we are choosing app.module.ts, in its imports array add the MailerModule.forRoot({...}) function as shown in the example below.
ts...
import { AuthModule } from './auth/auth.module';
import { MailerModule } from '@nestjs-modules/mailer';
@Module({
imports: [
...
MailerModule.forRoot({
transport: {
host: process.env.EMAIL_HOST,
auth: {
user: process.env.EMAIL_USERNAME,
pass: process.env.EMAIL_PASSWORD,
},
},
}),
],
...
})
export class AppModule {}
The object passed to the MailerModule.forRoot({..}) is a "transporter" object. This object tells Nodemailer to use Mailtrap's servers instead of a real provider like Gmail.
Generate a Mail Service to use the transporter
Use the Nest CLI to create a service where your email logic will live:
nest generate service mail.
Inject the MailerService, an out of the box service provided by @nestjs-modules/mailer.
tsimport { Injectable } from '@nestjs/common';
import { MailerService } from '@nestjs-modules/mailer';
@Injectable()
export class MailService {
constructor(private readonly mailerService: MailerService) {}
async sendWelcomeEmail() {
await this.mailerService.sendMail({
to: 'your-email@gmail.com',
subject: 'Welcome to our App',
text: 'Thank you!',
});
}
}
Call your MailService in the controller
Inject your MailService into any controller or other service where you need to trigger an email
ts...
@Get()
async sayHello() {
await this.mailService.sendWelcomeEmail();
return {success: true}
}
Now run the app, npm run start:dev and call the endpoint curl localhost:3000.
If you get a 500 server error and see this on your server terminal you are on track.
bashError: connect ECONNREFUSED 127.0.0.1:587
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1637:16) {
errno: -111,
code: 'ESOCKET',
syscall: 'connect',
address: '127.0.0.1',
port: 587,
command: 'CONN'
This error is to tell us that our process.env variables are not working. Let's correct that in the next section.
How to use .env variables
- Install Nestjs config module
npm i --save @nestjs/config - paste
import { ConfigModule } from '@nestjs/config';top of your module - lastly add
ConfigModule.forRoot()as first item on your module imports. See below
app.module.tsimport { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { MailerModule } from '@nestjs-modules/mailer';
import { MailService } from './mail/mail.service';
...
@Module({
imports: [
ConfigModule.forRoot(),
MailerModule.forRoot({
transport: {
host: process.env.MAIL_HOST,
auth: {
user: process.env.MAIL_USER,
pass: process.env.MAIL_PASS,
},
},
}),
...
})
export class AppModule {}
Run the app, npm run start:dev and call the endpoint curl localhost:3000 again. Everything should work fine now.
If see this Error: Invalid login: 535 5.7.0 Invalid credentials, then you
need to cross check your variables in .env file with that mailtrap provided
you.
References
Footnotes
- How to Send Emails With Nodemailer in NestJS Pasted on Sat, Nov 01, 2025.
- NestJS - How to use .env variables in main app module Pasted on Wed, April 04, 2026.