<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ujanth on Medium]]></title>
        <description><![CDATA[Stories by Ujanth on Medium]]></description>
        <link>https://medium.com/@ujanth98?source=rss-84b44468406d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*q44yShUEQtAH2igF</url>
            <title>Stories by Ujanth on Medium</title>
            <link>https://medium.com/@ujanth98?source=rss-84b44468406d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 03:14:23 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ujanth98/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to Configure NodeMailer in a NestJS Application]]></title>
            <link>https://medium.com/@ujanth98/how-to-configure-nodemailer-in-a-nestjs-application-e32b46823e2f?source=rss-84b44468406d------2</link>
            <guid isPermaLink="false">https://medium.com/p/e32b46823e2f</guid>
            <category><![CDATA[smtp]]></category>
            <category><![CDATA[gmail]]></category>
            <category><![CDATA[nestjs]]></category>
            <category><![CDATA[nodemailer]]></category>
            <category><![CDATA[google-app-password]]></category>
            <dc:creator><![CDATA[Ujanth]]></dc:creator>
            <pubDate>Thu, 25 Jun 2026 08:01:11 GMT</pubDate>
            <atom:updated>2026-06-25T08:01:11.721Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pIaZgJLRIzdOCUv3tmsGFg.png" /></figure><h3><strong>Step 1: Create a NestJS Project</strong></h3><p>The first step is to create a new NestJS application if you do not already have one.</p><pre>nest new nodemailer-be</pre><h3><strong>Step 2: Install Required Dependencies</strong></h3><p>Install the required packages for NodeMailer and email template support.</p><pre>yarn add nodemailer @nestjs-modules/mailer<br>yarn add -D @types/nodemailer <br>yarn add handlebars<br>yarn add -D @types/handlebars</pre><h3><strong>Step 3: Install the Configuration Module</strong></h3><p>Environment variables should be used to securely store sensitive information such as SMTP credentials. The @nestjs/config package allows you to manage environment variables efficiently throughout your application.</p><pre>yarn add @nestjs/config</pre><h3><strong>Step 4: Create Environment Variables</strong></h3><p>Create a .env file in the root directory of your project. This file will contain all email-related configuration values, including SMTP server details and authentication credentials.</p><pre>MAIL_HOST=smtp.gmail.com # SMTP server host address <br>MAIL_PORT=587 # SMTP server port number <br>MAIL_USER=your-email@gmail.com # Email account used to send emails <br>MAIL_PASSWORD=your-app-password # Gmail App Password <br>MAIL_FROM=your-email@gmail.com # Default sender email address</pre><h3>Hint : How to Create an Gmail App Password in a Google Account</h3><p><em>a. Sign in to your Google Account.</em></p><p><em>b. Navigate to </em><strong><em>Security and Sign-In</em></strong><em>.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VPJYegEtea292y5pJSmdug.png" /></figure><p>c. <em>Enable 2-Step Verification if it is not already enabled.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/1*sCHSKGOZyxHFYlYmmhchgw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/972/1*Aar6ucCcI0bYfOMlriBpKQ.png" /></figure><p><em>d. Select App Passwords.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/1*86QLJCtwSVsU07A1lFSeFA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/1*86QLJCtwSVsU07A1lFSeFA.png" /></figure><p><em>e. Enter App Name &amp; Generate App Password.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/1*0oYpUlNXJsUjQk2c30cGJQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/972/1*BCHbWMhZSl6wfe59hF-DUw.png" /></figure><h3><strong>Step 5 : Generate the Mail Service, Controller, and Module Files</strong></h3><p>Use the following command to generate the Mail module, service, and controller files:</p><pre>nest g resource mail</pre><p><strong>Note:</strong> mail is the folder name. You can replace it with your preferred module name if required.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/301/1*oqgd4DaM_mG5ZWhnncqZMA.png" /></figure><h3><strong>Step 6: Configure the Mailer Module</strong></h3><p>The Mailer Module is responsible for establishing a connection with the SMTP server and configuring email templates.</p><pre>import { Module } from &#39;@nestjs/common&#39;;<br>import { MailerModule } from &#39;@nestjs-modules/mailer&#39;;<br>import { ConfigModule, ConfigService } from &#39;@nestjs/config&#39;;<br>import { join } from &#39;path&#39;;<br>import { MailService } from &#39;./mail.service&#39;;<br>import { MailController } from &#39;./mail.controller&#39;;<br>import { HandlebarsAdapter } from &#39;@nestjs-modules/mailer/adapters/handlebars.adapter&#39;;<br><br>@Module({<br>  imports: [<br>    MailerModule.forRootAsync({<br>      imports: [ConfigModule],<br>      useFactory: async (config: ConfigService) =&gt; ({<br>        transport: {<br>          host: config.get(&#39;MAIL_HOST&#39;),<br>          port: config.get&lt;number&gt;(&#39;MAIL_PORT&#39;),<br>          secure: false,<br>          auth: {<br>            user: config.get(&#39;MAIL_USER&#39;),<br>            pass: config.get(&#39;MAIL_PASS&#39;),<br>          },<br>        },<br>        defaults: {<br>          from: `&quot;No Reply&quot; &lt;${config.get(&#39;MAIL_FROM&#39;)}&gt;`,<br>        },<br>        template: {<br>          dir: join(__dirname, &#39;templates&#39;),<br>          adapter: new HandlebarsAdapter(),<br>          options: { strict: true },<br>        },<br>      }),<br>      inject: [ConfigService],<br>    }),<br>  ],<br>  providers: [MailService],<br>  controllers: [MailController],<br>  exports: [MailService],<br>})<br>export class MailModule { }</pre><h3><strong>Step 7: Create an Email Service</strong></h3><p>Create a dedicated service to handle all email-related operations, such as sending welcome emails, OTPs, and password reset emails.</p><pre>import { Injectable } from &#39;@nestjs/common&#39;;<br>import { MailerService } from &#39;@nestjs-modules/mailer&#39;;<br><br>@Injectable()<br>export class MailService {<br>  constructor(private readonly mailerService: MailerService) {}<br><br>  // Welcome Email<br>  async sendWelcomeEmail(to: string, name: string, url: string) {<br>    await this.mailerService.sendMail({<br>      to,<br>      subject: &#39;Welcome to MyApp!&#39;,<br>      template: &#39;welcome&#39;,           <br>      context: { name, url },       <br>    });<br>  }<br><br>  // Reset Password Email<br>  async sendResetPasswordEmail(to: string, name: string, resetUrl: string) {<br>    await this.mailerService.sendMail({<br>      to,<br>      subject: &#39;Reset Your Password&#39;,<br>      template: &#39;reset-password&#39;,    <br>      context: { name, resetUrl, expiry: &#39;15 minutes&#39; },<br>    });<br>  }<br><br>  // OTP Email<br>  async sendOtpEmail(to: string, name: string, otp: string) {<br>    await this.mailerService.sendMail({<br>      to,<br>      subject: &#39;Your OTP Code&#39;,<br>      template: &#39;otp&#39;,            <br>      context: { name, otp, expiry: &#39;5 minutes&#39; },<br>    });<br>  }<br>}</pre><h3>Step 7: Create an Email Controller.</h3><p>Create a Mail Controller with an endpoint that triggers email sending.</p><pre>import { Controller, Post, Body } from &#39;@nestjs/common&#39;;<br>import { MailService } from &#39;./mail.service&#39;;<br><br>@Controller(&#39;mail&#39;)<br>export class MailController {<br>  constructor(private readonly mailService: MailService) {}<br><br>  @Post(&#39;welcome&#39;)<br>  async welcome(@Body() body: { to: string; name: string; url: string }) {<br>    await this.mailService.sendWelcomeEmail(body.to, body.name, body.url);<br>    return { message: &#39;Welcome email sent&#39; };<br>  }<br><br>  @Post(&#39;reset-password&#39;)<br>  async resetPassword(@Body() body: { to: string; name: string; resetUrl: string }) {<br>    await this.mailService.sendResetPasswordEmail(body.to, body.name, body.resetUrl);<br>    return { message: &#39;Reset password email sent&#39; };<br>  }<br><br>  @Post(&#39;otp&#39;)<br>  async otp(@Body() body: { to: string; name: string; otp: string }) {<br>    await this.mailService.sendOtpEmail(body.to, body.name, body.otp);<br>    return { message: &#39;OTP email sent&#39; };<br>  }<br>}</pre><h3><strong>Step 8 :Create Email Templates</strong></h3><p>Create the following Handlebars (.hbs) templates:</p><p><strong>a. Welcome Email Template (</strong>welcome.hbs<strong>)</strong></p><pre>&lt;!DOCTYPE html&gt;<br>&lt;html&gt;<br>&lt;head&gt;<br>  &lt;style&gt;<br>    body { font-family: Arial, sans-serif; background: #f4f4f4; margin: 0; padding: 0; }<br>    .container { max-width: 600px; margin: 40px auto; background: #fff; border-radius: 8px; padding: 40px; }<br>    .header { background: #4F46E5; color: white; padding: 20px; border-radius: 8px 8px 0 0; text-align: center; }<br>    .body { padding: 30px 20px; color: #333; }<br>    .button { display: inline-block; padding: 12px 30px; background: #4F46E5; color: white;<br>              text-decoration: none; border-radius: 5px; margin-top: 20px; }<br>    .footer { text-align: center; color: #999; font-size: 12px; margin-top: 30px; }<br>  &lt;/style&gt;<br>&lt;/head&gt;<br>&lt;body&gt;<br>  &lt;div class=&quot;container&quot;&gt;<br>    &lt;div class=&quot;header&quot;&gt;<br>      &lt;h1&gt;Welcome to MyApp!&lt;/h1&gt;<br>    &lt;/div&gt;<br>    &lt;div class=&quot;body&quot;&gt;<br>      &lt;h2&gt;Hi {{name}},&lt;/h2&gt;<br>      &lt;p&gt;Thanks for signing up! We&#39;re excited to have you on board.&lt;/p&gt;<br>      &lt;p&gt;Click the button below to get started:&lt;/p&gt;<br>      &lt;a href=&quot;{{url}}&quot; class=&quot;button&quot;&gt;Get Started&lt;/a&gt;<br>    &lt;/div&gt;<br>    &lt;div class=&quot;footer&quot;&gt;<br>      &lt;p&gt;© 2025 MyApp. All rights reserved.&lt;/p&gt;<br>    &lt;/div&gt;<br>  &lt;/div&gt;<br>&lt;/body&gt;<br>&lt;/html&gt;</pre><p><strong>b. Reset Password Template (</strong>reset-password.hbs<strong>)</strong></p><pre>&lt;!DOCTYPE html&gt;<br>&lt;html&gt;<br>&lt;head&gt;<br>  &lt;style&gt;<br>    body { font-family: Arial, sans-serif; background: #f4f4f4; margin: 0; padding: 0; }<br>    .container { max-width: 600px; margin: 40px auto; background: #fff; border-radius: 8px; padding: 40px; }<br>    .header { background: #DC2626; color: white; padding: 20px; border-radius: 8px 8px 0 0; text-align: center; }<br>    .body { padding: 30px 20px; color: #333; }<br>    .otp-box { font-size: 32px; font-weight: bold; letter-spacing: 8px; color: #4F46E5;<br>               text-align: center; padding: 20px; background: #f0f0ff; border-radius: 8px; margin: 20px 0; }<br>    .button { display: inline-block; padding: 12px 30px; background: #DC2626; color: white;<br>              text-decoration: none; border-radius: 5px; margin-top: 20px; }<br>    .footer { text-align: center; color: #999; font-size: 12px; margin-top: 30px; }<br>    .warning { color: #DC2626; font-size: 13px; margin-top: 15px; }<br>  &lt;/style&gt;<br>&lt;/head&gt;<br>&lt;body&gt;<br>  &lt;div class=&quot;container&quot;&gt;<br>    &lt;div class=&quot;header&quot;&gt;<br>      &lt;h1&gt;Password Reset Request&lt;/h1&gt;<br>    &lt;/div&gt;<br>    &lt;div class=&quot;body&quot;&gt;<br>      &lt;h2&gt;Hi {{name}},&lt;/h2&gt;<br>      &lt;p&gt;We received a request to reset your password. Click the link below:&lt;/p&gt;<br>      &lt;a href=&quot;{{resetUrl}}&quot; class=&quot;button&quot;&gt;Reset Password&lt;/a&gt;<br>      &lt;p class=&quot;warning&quot;&gt;⚠️ This link expires in &lt;strong&gt;{{expiry}}&lt;/strong&gt;.&lt;/p&gt;<br>      &lt;p&gt;If you didn&#39;t request this, please ignore this email.&lt;/p&gt;<br>    &lt;/div&gt;<br>    &lt;div class=&quot;footer&quot;&gt;<br>      &lt;p&gt;© 2026 Ventrax Solutions. All rights reserved.&lt;/p&gt;<br>    &lt;/div&gt;<br>  &lt;/div&gt;<br>&lt;/body&gt;<br>&lt;/html&gt;</pre><p><strong>c. OTP Template (</strong>otp.hbs<strong>)</strong></p><pre>&lt;!DOCTYPE html&gt;<br>&lt;html&gt;<br>&lt;head&gt;<br>  &lt;style&gt;<br>    body { font-family: Arial, sans-serif; background: #f4f4f4; margin: 0; padding: 0; }<br>    .container { max-width: 600px; margin: 40px auto; background: #fff; border-radius: 8px; padding: 40px; }<br>    .header { background: #059669; color: white; padding: 20px; border-radius: 8px 8px 0 0; text-align: center; }<br>    .body { padding: 30px 20px; color: #333; }<br>    .otp-box { font-size: 40px; font-weight: bold; letter-spacing: 10px; color: #059669;<br>               text-align: center; padding: 20px; background: #f0fff4; border-radius: 8px; margin: 20px 0; }<br>    .footer { text-align: center; color: #999; font-size: 12px; margin-top: 30px; }<br>  &lt;/style&gt;<br>&lt;/head&gt;<br>&lt;body&gt;<br>  &lt;div class=&quot;container&quot;&gt;<br>    &lt;div class=&quot;header&quot;&gt;<br>      &lt;h1&gt;Your OTP Code&lt;/h1&gt;<br>    &lt;/div&gt;<br>    &lt;div class=&quot;body&quot;&gt;<br>      &lt;h2&gt;Hi {{name}},&lt;/h2&gt;<br>      &lt;p&gt;Your one-time password is:&lt;/p&gt;<br>      &lt;div class=&quot;otp-box&quot;&gt;{{otp}}&lt;/div&gt;<br>      &lt;p&gt;This OTP is valid for &lt;strong&gt;{{expiry}}&lt;/strong&gt;. Do not share it with anyone.&lt;/p&gt;<br>    &lt;/div&gt;<br>    &lt;div class=&quot;footer&quot;&gt;<br>      &lt;p&gt;© 2026 Ventrax Solutions. All rights reserved.&lt;/p&gt;<br>    &lt;/div&gt;<br>  &lt;/div&gt;<br>&lt;/body&gt;<br>&lt;/html&gt;</pre><h3><strong>Step 9 : Update the </strong>nest-cli.json<strong> File</strong></h3><p>Update the nest-cli.json file to include the email template assets during the build process.</p><pre>{<br>  &quot;$schema&quot;: &quot;https://json.schemastore.org/nest-cli&quot;,<br>  &quot;collection&quot;: &quot;@nestjs/schematics&quot;,<br>  &quot;sourceRoot&quot;: &quot;src&quot;,<br>  &quot;compilerOptions&quot;: {<br>    &quot;deleteOutDir&quot;: true,<br>    &quot;assets&quot;: [<br>      {<br>        &quot;include&quot;: &quot;mail/templates/**/*&quot;,<br>        &quot;outDir&quot;: &quot;dist&quot;<br>      }<br>    ]<br>  }<br>}</pre><h3>Step 10: Run the Server and Test Email Sending</h3><p>Start the application and test the email functionality.</p><pre>yarn start:dev</pre><h4><strong>Hint : Postman Requests for Each Template</strong></h4><p><strong>a. Welcome Template:</strong></p><pre>POST http://localhost:3000/mail/welcome<br>{<br>  &quot;to&quot;: &quot;user@gmail.com&quot;,<br>  &quot;name&quot;: &quot;Ujanth&quot;,<br>  &quot;url&quot;: &quot;https://myapp.com/dashboard&quot;<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/630/1*azcO67S7yCYWKYh94XvLiw.png" /></figure><p><strong>b. Reset Password:</strong></p><pre>POST http://localhost:3000/mail/reset-password<br>{<br>  &quot;to&quot;: &quot;user@gmail.com&quot;,<br>  &quot;name&quot;: &quot;Ujanth&quot;,<br>  &quot;resetUrl&quot;: &quot;https://myapp.com/reset?token=abc123&quot;<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/630/1*sCuRp9wvxVUI-d3GNbtHsQ.png" /></figure><p><strong>c. OTP:</strong></p><pre>POST http://localhost:3000/mail/otp<br>{<br>  &quot;to&quot;: &quot;user@gmail.com&quot;,<br>  &quot;name&quot;: &quot;Ujanth&quot;,<br>  &quot;otp&quot;: &quot;847291&quot;<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/632/1*VijgBYXuffpy0v7zroJ58A.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e32b46823e2f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>