📺 Watch the complete Node.JS Playlist here :https://youtube.com/playlist?list=PLwGdqUZWnOp3KELplHtc-RnJ5xTUPqdgH&si=_ob8F88HYxCdspup ------------------------------------------------------------------------ In this video, we’ll learn how to send emails in a Node.js app using Nodemailer and Ethereal. We’ll go step by step, from installing Nodemailer to writing the sendEmail function and handling errors. By the end, you'll be able to send emails easily in your project. 🚀 🚀 Boost Your Skills with these Pre-Requisite Videos:
⌛TIMELINE⏳ ⏳ 0:00 – Intro to Email Verification 📢 0:12 – About Our Series Update 📩 1:20 – What is Nodemailer? 🛠️ 5:30 – What is Ethereal & How to Create an Account 📦 5:55 – Installing Nodemailer ⚙️ 6:30 – Creating sendEmail Function with All Arguments 📤 9:20 – Using Nodemailer to Send Emails with Ethereal 🔍 15:00 – Important Object Returned by sendMail Method 🔁 17:40 – Quick Recap: How to Send Emails Using Nodemailer 🐞 18:50 – Let’s Tackle Errors
Hello what's up guys, today in this video
we will see how to send email using nad
mailer, yes guys, today's video is very
important because we were
waiting for it impatiently, finally today that moment has come
where whenever someone clicks on the recent verification
link, then guys a mail comes on
our console, when I click on that, then
guys you see an interface of this type,
today we are going to
see all this in this video, you don't worry,
but finally till now if you are watching me,
then guys congratulations because
you have a lot of patience because I feel that for
almost 3 months I am continuing this series
and these 3 months if
you follow me then it is a big thing and
finally now day by day one better than the other things are going to
come which till today I must have
uploaded 000 on my youtube, very less means
hardly any 10 creators must have uploaded that video
which I am going to do now,
okay so now how can we do this
our fully Flagged is a project
in which finally now we are adding token
basically we are looking at the email verification part
okay so we have done till here
we have done this while verifying our email if you remember
what kind of
link should be there we have done all that
now we have to show this dashboard so
first of all guys how
can we send mail in our node
application by saying so we have a node mail
and I guess you will get it here also
if I show you okay so
this is you can see node mailer is a node.js
library used to send emails node mailer is a
library with the help of which you
can send email we use therian email a fake
email service for testing we will
use theril only in this particular video
but in upcoming videos we will
use the recent and other things also through which
your official
the node mail and then in production you can
use services like
amazon2 and then we will use further Do
n't worry, at least you got to know
what is node mailer, it is a node's
library with the help of which you can share mail
and in that we use guys SMTP
server, it can be any server but
right now we will use Therian which is
a fake email service provider, okay
and mostly we do it for testing,
so I'm going to go ahead, okay, here is
your node mail, alright,
you can see here, what is it, how is it,
you will find everything here,
what are the features of node mailer, everything is here, so
guys, if you want to understand node mailer in very simple
language, then you can understand that node
mailer is equal to a post office where
you go and pass your letter,
but when you are passing the letter, as it is, you will
not give it, guys in that letter there will be
your content, you will
write the full address of the recipient, to
whom you have to send the last address and then whatever post office it is,
guys, it's their job That
delivery is basically to deliver it to the recipient's address. So,
node mailer
also does the same thing here, where there are
just one or two methods which are important which
you have to keep in mind. First is
we need to create a transporter here and then second is
inside that transporter, we get a method
named send mail. So
look carefully, once you understand this, your
life will become very easy. Look,
we required node mailer, it was not a big deal,
we are creating a transport here. So,
here the simple meaning of transporter is that
guys, which SMTP server
we are using, whatever data is there,
we have to pass it here. It means
its job will be to deliver our letter or our
email to the place where we want to deliver it.
Now, here this transporter, you
can see which is
smtp2go. Here, which SMTP server are we
using or which service.
We are using the real service
let's say if I go to
google2 goole.com the
first thing we need to do is to create a
transporter and its job will be to deliver our mail to the
place where we
want it to be delivered simple now once there is
another method inside the transporter which I
told you that is your send mail this is very
easy whenever you go to email you know
open gmail if you want to send mail to someone then
what do you do first of all you mention to
whom you want to send it then you
write the subject and then you write your content
whether it is in text format
or atml format if you write any one of the two then
we will write one and pass it
and yes by default we do not need to write a form there
because we are already
logged in there we are logging in with our own
id right if we
click on send then our mail will go to them
but here we do not have anything like that
so first of all we write the form also
see how much It is easy, just which
service are you using to send mail, there is
a method of send mail in which
we have to enter the full address of the recipient
and we have an address of who is the person
who has sent it to you and that's it, that's all,
create transport and that's whatever
your object will be, dot send mail and that's it,
so you do one thing, let's
go to the
real email, we go here and
there is nothing to do here, you just have to
click on it, the moment you click,
guys a simple thing will come in front of you,
you will get your user name and then
password, okay, don't delete it,
keep it as it is, now do one thing,
first of all come here, ok, guys, you
can see that we have come to our BS code editor, so
first of all what we need to do, first of all
we need to
install node mailer, okay, so first of all we
install it, ok, mine has been
installed, now I am running it,
now what do we have to pass to node mailer, guys This
is node mailer, you can see we need to
pass this particular data,
which is inside send mail, who do I mean to
whom do I have to send it, what should be the subject,
what should be its content, we have to pass that,
so I will create a function for this
now and
this function which I am talking about, so
basically I have to create this code now
because I am going to forward them in this format,
right, so let me do
one thing, send
email will be the
name of our function in which I have to pass the data,
basically now I am passing the argument here, to
whom do I have to send it,
I have to send it to whichever user has logged in,
okay, you created your application with lets
and you hosted it live, now if
any user from Madhya Pradesh is logging in, then
this will be mailed to him,
what will be the request url subject, let me do one thing, I
already have it, this subject
will be verify your email ATML, see the
content will be either in text format or
in your ATML format, but we are
doing it in ATML format because Because
we are going to need some links, some
extra JavaScript part is going to be needed,
so that is the reason why I am
using template literals here and
click the link below to verify your
email. Click the link below to verify your
email. Oh my God, finally, since then you must be thinking,
how did this come here? How did this
come here? From where is this token coming to us? So
it is coming from here. You can use this
token. You can use this token. And then the
token that I am getting here is a random token
because I have already generated a random token in yesterday's video
and I have
also added it to the table of my database.
I have also created a link of my own. So
this is just that message, which is the
mail, which I have to forward. So this is just that. And
even after clicking on verify email, it was getting verified automatically. The
reason behind that is that I am
redirecting it. Verify email
link. Verify email link. Calling this
means that I am going back. I am calling the create verify email
link where this
URL of mine is very
important inside this verify email
token route I have the complete
functionality that this is the same email whose token
we are checking or not simple okay so
I go here
my work is done and by the way
many of you may have noticed or not but
we have also used dot catch here now you are
thinking where did the catch
come from without try so I would like to tell you that this
catch method is not only try catch
if you have ever played promise then
see in your promise also dot then dot catch dot then dot then
dot catch is there
right so here there was
no neat dot then I wrote dot catch so it's your
promise simple you can understand like this and if
everything is correct then the response will
redirect them
to the verify email page from where it came
back okay so This is done for you,
now this send mail is obviously I have to
pass it to node mailer, so
now look, I need node mailer
here, it is very important,
look carefully, so let's do one thing,
node mailer is a library with the help of which
you can send mail, inside it we can
use different services, whose
job is basically to forward our mail, so
it is a library,
so I am creating a folder called LIB,
inside it I will add
node mail, okay and I would like to tell you one thing that
this part is already
given by them, so
I will copy it, okay and
I have already explained it, it is not a big deal, so it is
very easy, now
I don't need this common js module, I will
import a
node
mail, okay,
from node mail, okay,
till here my work is done and yes one thing is for
sure that now
what do we do, here I am creating a custom account, do
not take tension, create a test account inside it.
We get it, okay, let's create a test
account because we are testing and that's
it, one of your tasks is done, then we
keep one thing in mind here,
you have to write await
here, okay, now look, I did
n't know that I have to write await here, when I was
practicing this, I got an error, then I came to know that I have to
write await here because the
promise is returning, so there are some things
guys, which you have to keep in mind
that yes, it can happen here, it is
not necessary that you always know everything, now
this await happens here, we
cannot do anything and there is no await here,
then for some reason, but whatever it is, so this is how
we are creating a transport, so
create a transport, which service are you
using, I mean SMTP, I mean
real, I am using, its port is
this, I have kept secure false, yes obviously
I have to make it false because if it is true, then
guys, we will not be able to work, the
mail will not go properly, then this in auth
Basically you change it and
paste it here, this is
Rosario, what is this name brother, Rosario
or Rose Riya, is it like this and this is your
password and for some and god sake and guys I
request you guys to make your own and don't just
sit down to copy paste it, okay,
right, my transport photo is
ready with me, it's like your
postman is ready with you, everything is ready,
you just have to take your letter and
put it there, so now it is here, okay,
I will do one thing, I am copying this,
all that you can see here, let me
delete it from my end
and by the way guys I have
deleted everything from there, okay now I will copy my own from here
because I have already done
everything, look, so this is my
send email, we are talking about it here in
which there is two subject and then HTML,
two subject and then ATML now the
most important thing here is what is its format,
look carefully Now guys,
finally the time has come where we have to
send the mail. Now to send the mail,
we know very easily that
is
transporter, so
transporter dot we have send mail
method and within this, you can
share whatever method you want like what will you
write under to, we have a second under to, so
you already have to have the to,
right, that means to whom do you want to send the subject,
you already have the subject, I already have the subject,
ATML is already with you.
Now there is
still something missing here, like if
let's imagine, okay let's
imagine, you have caught it online here, there was a
verification button, you
clicked on this, okay and you are there, where are you
in MP, Madhya Pradesh, okay and here is
your system, okay this is your guys
system, now here let's say you got the mail,
okay you clicked on this and you got the
mail, but tell me one thing, how will you know
who sent the mail. So let's say this is your email
id, we know the subject, we
also know the format of ATML,
but who sent it from, where is it, when
anything comes to your inbox here, in this
way, first, second, third, then brother, we get mail, then
how will we know who sent it, so
where is we need one more, feed here that is
your from, okay, do you understand, I told you this much
because just this will not work, the from field is
very
important here and finally, from
where will I get the data of from, I will get it from
here, test account do user, fine, now
you must have come to know that we have written
this user and password here in authenticate
because this is a particular method, in
this test account, with the help of that,
I can access all the fields inside auth, so I
pass the value of from here in this way,
so by
setting the URL, whatever will be the value of this test account
dot user, will come there, you will get to
know from, okay, he has sent it, do you understand
and I wrote this so that
whatever email I have They come in the middle of this particular angle bracket
and this URL is
talking about a short project, so
from to subject and now you let's say the
transporter has shared the mail,
how will you know whether the mail was shared or not, so
guys, it must have given some response
like every time, so whatever response it would have
given, we need to wait, okay, the promise is
returning by doing cost info, there is
a stent variable on it, I would like to store it
or this is it, okay,
my work is done, now guys,
what is happening here, is that we are
not sending this to anyone's personal inbox and we
need that link, so to get that link,
we have another method,
here inside the node mailer, a get test
message URL, this is the beauty of it is
providing you everything, it is saying, okay,
you have to do testing, create a test account,
you create a transport, you can use whatever you want to
use, then
what mail do you want to send, set your format The
transporter will send the mail and
after that copy this info and
pass it here, meaning whatever you would have written and passed by
whichever transporter would have passed it,
whatever response would have come from that
info object, you have to pass that here.
Okay, so whenever you pass this info object,
it gives you a URL, do you
understand that, that means yes brother this is the URL
in which I have passed it and that is the
same URL which we are getting on it, so I have
kept it as const test email URL, all
right, let me do one thing, now I will
copy and paste it and I just realize
that you have to keep these two parts inside the send mail
only, otherwise this info is
a constant variable and its
scope remains only within the braces,
if I take it out, then
I would never have been able to access this info, there are a few
things which you have to understand, okay, and you do
not remember everything the first time you see it, I hope
you must have understood the funda,
this is it my work is completed, no
tension No, I will press control and save. The
time for the exam has come. Let's check
it out. So first of all, let's
see if we have got this part or not. Okay, now I am going here.
Okay guys, so if
I refresh it once,
this page will appear.
I would also like to start my drizzle here. So
guys, you can see that
we have
logged in. I would like to go to the profile.
See, there is nothing till now. Today, let's pray
that it is okay. So as soon as we click on Verify Now,
we are going to a page. Till here, it is
fine. This was explained in yesterday's video.
Whenever we click on this, check it again. Let
me explain it to you once again.
What happens is that we generate a random token.
Then we are storing it
in our database using I mean including
user ID. Then we create a link.
And then what do we do?
We are passing the send mail. Okay,
what are we doing to the send mail? We are passing it
and then we have shown the send mail in node miller in
this way in today's video,
we have
set it up completely in this way, let's save the control and
okay, I would like to open the console here
now the moment, I click on it,
lt is not defined, okay, no
problem, this is the error of yesterday's video, I
know where less than if you remember
I said when you were working here,
this less than part is jil
oram, okay, okay I will go back, this is the
refresh Ian's watch, guys, this is okay, I will
go here, click, okay, oh
my god, it is tormenting me in this way,
right, I have done this as well, let's go back,
I will click here, let's do
one thing here just once, let's
check it out, our easymail is valid, has
any data come here or not, no
data has come here, this is what it means
So guys, something or the other has happened at this place, so
we can do one thing,
this is my part, whatever is my
part, let's go with try and then catch,
okay, if anything remains then we can catch it, let's
paste the error here, let's check it
out, okay, according to me,
something is happening right here because this
error that we are getting, God knows what kind of error it is,
although we can check it out,
it was not a big deal,
oh my
God guys, we got a mail, look here, look,
I have received a mail, if I click on this, okay, there
is something wrong, look, object
promise verify email, if I am
going to verify email, okay, guys, one
interesting thing we are getting this here and that is that there is
something wrong with the object promise token, but yes the email is equal, there is no
problem in the email, so guys, let's do
one thing, see at least this much
we got to know, just writing a try catch
helped a lot, right one Let's work, let's
go here and see, it is happening till here,
why not directly console
dialog or what are we getting in the token, let's
see once, okay, this is
how I will make the token, okay,
look guys, trial and error, I told you, this is how
work happens in real, I go back
here, refresh it, okay, I
click on it, okay, look, I get the mail, the
mail is coming to me regularly, there is no
tension, the real email comes, I open it,
look, here I am getting something like this, so
somewhere on the token, oh my God, look
guys, look, look, there is no problem, that
means I am getting the token but
we did not handle the promise properly, that's the only
problem, there is no other
problem, only that one
problem is happening here, so guys, how are
you feeling, is this how much oh sum and
this is the reality, guys, it is not like that
whenever I am writing the code, there is never any error
and we do it, it is not like that while we are
making the video Yes, I have done a
lot of practice behind it. I have
my team and we practice together and only
then I will put that code in front of you guys.
I am not coding it on the spot. If I
do it on the spot then who knows how many
errors will come up and what will happen. But still look,
I got an error and where did the mistake happen? I will
tell you, first of all, the mistake happened and we did
not get anything at all. At least a try and then
catch. When we used it, somehow it
figured it out and at least
gave us that link. And when we got the link,
we got to know that everything is correct. There is just
one problem where this was not there. There was
something like this where it is clearly visible
that this is also there and we
also have this. It was just some problem with the promise as well.
So we got to know that the mail is
correct but there is something wrong with the token. So I got the
token consoled. When we
consoled the token, I was getting that eight digit
token but there was a promise attached to it. So
it means that there is a
problem here. It is happening okay because when we are
passing it, it is equal to random
token only, so we passed it, something is going wrong in the generated random token
and one problem is that
when I am not writing anything here, so
why am I making it async here,
I should not have made this function an async
asynchronous function, this was the only
problem and there is no other problem here,
so we should not have made it an async function,
now I will save control and let's
check it out, yes, I am just figuring out
that this should be a reason, now
I click here and bam guys,
see, the token has also come, the link has also come, I will click
and boom guys, see, my token has come
and my link has come, this is it, so
guys, now I will tell you in tomorrow's video
some best practices which we
could follow so that this error which came today does
not come in tomorrow's video, are you understanding, so
see, by the way, in today's video, I did
not make the error intentionally, to be very frank, it was
just a sink by mistake there Added
and the kind of error it gave, god
knows what the error was, although we could have tackled it
but we were not able to figure it out, at least by
writing try catch we came to know what was
wrong, right? So in tomorrow's video,
I will tell you how we can make it
better, in which we will see
transaction, yes guys, in tomorrow's video,
finally in the database, if you have ever
heard of transaction while doing a sequence, then
now we are going to implement it live in tomorrow's video, it will be
a lot of
fun, but finally you saw the node mailer,
you saw the real, how do you get the link,
you got to know all this, where is it coming from,
why is it coming, it is good that the error is coming
so that you get more idea that how we were getting it,
these small things, it is
necessary to get errors, otherwise I will tell you that if you write funa fun, then there will be no
benefit, you will forget it
in a week, then anyway, no, the
source code is absolutely free, I will share the link with you
in the description and comment section below,
if you genuinely If you
want to support then guys please like, share
and subscribe,
but at the same time guys you
can get my entry on youtube1, there is a sale going on,
in a day or two I am going to do 249, okay,
but you have a chance for a day or two, you
can try it yourself, okay, you will see, it will increase in two-three
days, so this is a good opportunity, you should
buy it, I don't know
how many of you are still watching this video, but
if you are watching it, then congratulations, you have a
lot of patience, you will definitely become something in the
future, but please subscribe for now
and tomorrow's video is very
important, don't miss it, I will meet you
in tomorrow's video till then guys,
thank you so much, take care
[Music]