Hello There, In this blog i will explain you that how you can send mail in the next js app with help of the email js node module. So, In node js the nodemailer module is most of the time use for sending the mail, but in production nodemailer sometimes not able to send the mail. This is an Error or issue hear. Also this hash solution but i am find the one of the best alternatives to this is email js. Email-js is a node module which use for sending mail and also very easy and fast.
So, In this post we are going to build a next js app which takes user email as input and sends mail to the user. Hope You Link It.
Run Aboue Command in the terminal and then just give the project name. Than run npm run dev for see app is running fine.
Than Install axios and email-js for sending the mail
Know replace index.js app to code given below
We have one useState email which is used for store user email and when the user click on the send button the SendMail Function will call.
Till this point you can see project like below and when you Enter mail and click send you can see output call in console.
Next js is Used the server-side rendering and we can create api and make request to that api in client side. Let’s create api for send the mail.
Add email.js file to api folder
Enter bellow Code to file
Just Open http://localhost:3000/api/email this link in browser and you can see Json Out Put on Screen.
1.First We Import For Send SMTPClient Smtp service mail (like gmail)
3.Than with the help of client.send() method we can send mail
email.js file
So, Our Api, For Sending mail is finish.
In SendMail Function we are gointing to make post request to email api with the help of axios.
Add In SendMail Function
And that's it you are successfully able to create an email send app in next js.
index .js
email.js
Note: Sometimes client.send() methods do not work in production. For Solution of this just use Client.sendAsync(). This will also work in development.
Blog Write By:zeel codder