Instructor: 00:00 First of all, you need to create a new account at ethereal.email. Ethereal is a fake SMTP service. It is used for only testing emails.
00:10 First, you need to create a new account. Account is created.
00:14 Now, you need to copy these credentials. I'm going to copy my user name. I'm going to paste into my development.js file. I'm going to create a new object Ethereal.
00:24 First, we need to create the user name. Let's say, I'm going to place my user name. I also need password. I'm going to put my password here. I also need to copy the host. I'm going to copy the host here. I also need the port.
00:45 Next step, you need to install couple of dependencies. I need to install a NodeMailer, HTML to text. Both dependencies have installed successfully.
01:03 Now, we need to create a new file inside the modules. I'm going to create a new folder "Modules."
01:11 First of all, we need to import the NodeMailer, NodeMailer from the NodeMailer package. I also need HTML to text plug-in, HTML to text.
01:29 Let's create a new method, SendMail or SendEmail. It will take the options. I need to use the error function. It will return a new promise.
01:50 I'm going to use the promise. It'll take resolve and reject. Now, we need to create the transporter from the NodeMailer package, transporter...I can use the create transport method.
02:06 Here, we need to specify all the configuration. Let me specify the host. I can get the host for my dev config object, devconfig.ethereal.host.
02:18 I also need the port, devconfig.ethereal.port. Let me specify my auth credentials.
02:29 I need a user name. I can get that user name from ethereal.username.
02:34 I also need password, devconfig.ethereal.password. Let's convert the HTML to text. I can use this package HTML to text .fromString. We need to specify the HTML, I will get the HTML from the option's object.
03:01 You can also provide the word wrap. I'm going to use 130.
03:09 Let's create the mail options. The first property is "From," From should be inaudible or you can specify any email. I'm going to use the text is the text. I also need to provide the HTML. You can get the HTML from the options.html.
03:33 We also need to provide the "To." I can get the "To" email from the options. I can also specify the subject. Let me get the subject from the options. Finally, we need to call the sendEmail method from the transporter.
03:51 I'm going to provide my mail options. The second argument is callback method. It'll give me the error and info.
03:59 If error comes, we need to reject the promise. I'm going to call the reject method with error. If everything is good, you can log the messageID info.messageID.
04:14 You can also log the preview URL. Let say, I'm going to save preview URL. I can get the preview URL by calling this method getText Message URL. Let me provide the info object.
04:31 Finally, we need to return the resolve function with simple message, email has sent to your inbox.
04:46 We have created this sendEmail method. Now, we need to use this method inside the controller. I'm going to create a new method inside the user controller. Let's say sendEmail.
04:58 I'm going to use async await. You can also use try and catch. If something goes wrong, I can log the error. You can also send the 500 status code with error.
05:16 I'm going to call this method. It'll give me the results.
05:27 Let me get the SendEmail method from my modules. Go to the modules, I have a mail, yet we have sent email.
05:38 Now, I can use this method here, SendEmail, that can provide the options. You can use any HTML.
05:53 Let's say, reset password link or whatever you want to send. Let's say, subject is reset password, or I can also specify the "To."
06:09 I'm going to use any fake email. It will give me the info or success message.
06:16 I can return this message in the response results. Let me start the server. I did not create the route for the sendEmail. Let me do it, sendEmail to user, let's say that.
06:34 I'm going to create a new route inside the user router. Let's do it here. I'm going to create the text route, path should be sendMail and action should be sendEmail to user.
06:58 Let's try to test it. Path should be user's sendMail. It will take some time. Incredible, email has sent to your inbox. Here is a preview URL. You can click on this preview URL, to preview the email, otherwise you can go to your inbox.
07:21 I'm going to login to my account. Let me find my credentials. This is my user name. I'm going to paste my user name. I also need to specify my password.
07:41 Let me check my messages. Cool. If we have our reset password message, I got this message. This is a reset password, this is a plain text, and this is the HTML.