This project sets up an Express server that uses Nodemailer to send emails. It includes the use of environment variables and support for CORS.
- Node.js
- npm (Node Package Manager)
-
Clone this repository:
git clone https://github.com/your-username/nodemailer-express.git cd nodemailer-express -
Install dependencies:
npm install
-
Create a
.envfile in the root of the project and configure the following environment variables:PORT=5000 MAIL_USERNAME=your_email@domain.com MAIL_PASSWORD=your_password MAIL_HOST=smtp.office365.com MAIL_PORT=587 MAIL_USER=recipient@domain.com
To start the server, use the following command:
npm startThe server will listen on the port defined in the .env file or port 5000 by default.
Description:
Sends an email with the provided data.
URL:
/api/v1/contact
Method:
POST
Request Body:
{
"name": "Sender's name",
"email": "Sender's email",
"number": "Sender's phone number",
"text": "Sender's message"
}Success Response:
{
"message": "Message sent successfully!"
}dotenv is used to manage environment variables. express is configured to handle JSON and URL-encoded requests. cors is configured to allow requests from any origin. nodeMail is used to send emails with the contact form details. The /v1/contact endpoint receives the form data and uses mainMail to send the email.