This project is a Node.js application that uses the Express module to create a server. The server is set to run on port 3000 and can be accessed using environment variables. The server receives a URL request at the /send-email
endpoint using the POST method. The request body should contain JSON with the format { "subject": "free", "message": "free" }
. The server then uses the nodemailer
module to send an email using your Gmail account. The email address should be the same as the one used in your Gmail account.
- Node.js version 6 or newer.
- Express module.
- Nodemailer module.
- dotenv module.
- Gmail account.
To install the required modules, run the following command:
npm install express nodemailer dotenv
To run the server, use the following command:
npm start
or you can use the following command:
node index.js
Important Note: I'm using
node index.js
because I have a script in mypackage.json
file that runs theindex.js
file. If you don't have a script in yourpackage.json
file, you can use thenpm start
command.
The server is created using the Express module. The server listens on port 3000 and can be accessed using environment variables. The server receives a URL request at the /send-email
endpoint using the POST method. The request body should contain JSON with the format { "subject": "free", "message": "free" }
.
The server then uses the nodemailer
module to send an email using your Gmail account. The email address should be the same as the one used in your Gmail account. The server returns a response with the text "Email has been sent" and a status code of 201.
Here's a summary explanation of the code:
- The code starts by importing the necessary modules: express, nodemailer, and dotenv.
- Use the
dotenv
module to load environment variables from a.env
file intoprocess.env
. - An Express application is created using the
express()
function and stored in theapp
variable. - The
express.json()
middleware is added to parse JSON data in the request body. - A transporter object is created using the Nodemailer module. It is configured to use the Gmail service and requires the sender's email address and password.
- A HTML content is defined, which will be used as the body of the email.
- An Express route is defined for the
/send-email
endpoint using theapp.post()
method. This route handles the logic for sending emails. - Inside the route handler, the
mailOptions
object is created, which contains the necessary information for sending the email, such as the sender, recipient, subject, and message. - The
transporter.sendMail()
method is called to send the email. If there is an error, it is logged and a 500 status code is sent in the response. If the email is sent successfully, the response is logged and a 201 status code is sent. - The server is configured to listen on a specified port, which is either obtained from the environment variable
process.env.PORT
or defaults to port 3000. - When the server starts listening, a message is logged to indicate the server is running.
This code sets up a server that can receive requests to send emails. It uses the Express framework for handling routes, the Nodemailer module for sending emails, and dotenv, which shows how to use environment variables to store sensitive information like email credentials and server ports. The server listens to a specified port and responds with appropriate status codes to indicate the success or failure of the email-sending process.
"Programming isn’t about what you know, it’s about what you can figure out." – Chris Pine