In this article, we are going to understand email sending in the NestJS application.
Step 3:
Let's register the 'From Email Address', so let's complete the 'Create A Sender' form.
Step 5:
Step7:
Copy the 'Server', 'Username', and 'Password' values because we are going to use them in our NestJS application.
SMTP:
Simple Mail Transfer Protocol(SMTP) is used to send, and receive emails in any application. So on an email sent, its transferred over the internet from one server to another using SMTP.
For our demo, we are going to use the 'SendGrid' a third-party provider SMTP.
SendGrid:
To establish email communication in our NestJS application using SendGrid, then we need a few configurations from SendGrid like 'SMTP Host', 'from email address' 'user name', and 'password'.
Step 1:
Go to SendGrid's official website at 'https://sendgrid.com'. Next, do signup.
Go to SendGrid's official website at 'https://sendgrid.com'. Next, do signup.
Step 2:
Now login into the SendGrid website, then under the left-hand side menu go to the 'Settings', then select 'Sender Authentication' menu. At 'Single Sender Verification' click on the 'Get Started' button.
Now login into the SendGrid website, then under the left-hand side menu go to the 'Settings', then select 'Sender Authentication' menu. At 'Single Sender Verification' click on the 'Get Started' button.
Let's register the 'From Email Address', so let's complete the 'Create A Sender' form.
Step4:
Our 'From Email Address' need to be verified by clicking on the email we received from the SendGrid.
Now go to the 'Email API' menu on the left-hand side, then go to the 'Integration Guide' menu. Then click on the 'Choose' button on 'SMTP Relay'.
Step 6:
Specify the name of 'API Key' and create it.
Copy the 'Server', 'Username', and 'Password' values because we are going to use them in our NestJS application.
Create A NestJS Application:
Let's create a NestJS application to accomplish our demo.
Command To Install NestJS CLI
npm i -g @nestjs/cli
npm i -g @nestjs/cli
Command To Create A NestJS App
nest new your_project_name
nest new your_project_name
Install Required Mail Packages:
Let's install the following email nodejs packages into our NestJS application.
npm install --save @nestjs-modules/mailer nodemailer
Register MailerModule:
Now register the 'MailerModule' into our 'AppModule'.
src/app.module.ts:
import { MailerModule } from '@nestjs-modules/mailer'; import { Module } from '@nestjs/common'; import { AppController } from './app.controller'; import { AppService } from './app.service'; @Module({ imports: [ MailerModule.forRoot({ transport: { host: 'smtp.sendgrid.net', auth: { user: 'apikey', pass: 'SG.xxxxxxxxxxxxxxxxxxxxxxxxxx-yyyyyyyyy', }, } }), ], controllers: [AppController], providers: [AppService], }) export class AppModule {}
- (Line: 8) Imported the 'MailerModule' that loads from the '@nestjs-modules/mailer'.
- (Line: 10) SendGrid SMTP host which is fixed value 'smtp.sendgrid.net'.
- (Line: 12) The 'user' value will be 'apikey' which is a fixed value.
- (Line: 13) The 'pass' value will be our generated API key value in the above steps.
Create Email Controller:
Let's create a new controller like 'EmailController'. Run the below command to create the controller.
nest g co name_of_your_controller --flat
src/email.controller.ts:
import { MailerService } from '@nestjs-modules/mailer'; import { Controller } from '@nestjs/common'; @Controller('email') export class EmailController { constructor(private mailService: MailerService) {} }
- Here injected 'MailService' that loads from the '@nestjs-modules/mailer'
Plain Text Email:
Let's try to send a simple plain text email, so let's create an action method to achieve it.
src/email.controller.ts:
@Get('plain-text-email') async plainTextEmail(@Query('toemail') toEmail) { var response = await this.mailService.sendMail({ to:toEmail, from:"nani.bommidi93@gmail.com", subject: 'Plain Text Email ✔', text: 'Welcome NestJS Email Sending Tutorial', }); return response; }
- (Line: 2) The 'recipient email' is the input parameter to the action method.
- (Line: 3-8) Using 'MailerService' calls 'sendMail()' method to send the email.
- (Line: 5) The 'from email' we registered with SendGrid.
- (Line: 6) Email subject.
- (Line: 7) The 'text' property to pass the plain text email body.
Support Me!
Buy Me A Coffee
PayPal Me
Video Session:
Wrapping Up:
Hopefully, I think this article delivered some useful demo on sending simple plain email from the NestJS application. using I love to have your feedback, suggestions, and better techniques in the comment section below.
Refer:
Part - 2 Generate Dynamic Email Template And Send Email In The NestJS Application
Part-3 NestJS Email With File Attachment
Source Code
Part-3 NestJS Email With File Attachment
Source Code
Comments
Post a Comment