├── .env ├── public └── index.html ├── vercel.json ├── package.json ├── .gitignore ├── README.md └── index.js /.env: -------------------------------------------------------------------------------- 1 | // E-MAILER 2 | EMAIL_HOST = 'smtp.elasticemail.com' 3 | EMAIL_USERNAME = 'adx-marketing@adxfactor.com' 4 | EMAIL_PASSWORD = 'FE6F4B89A3A33B0CAD868D1566F2BD308414' 5 | EMAIL_PORT = '2525' -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | Its the end point for API "sendmail" 2 | Localhost:3000/sendmail 3 | Its the "POST API" in body you can add multiple keys and its optional 4 | Name , Email , Phone , Subject , Message -------------------------------------------------------------------------------- /vercel.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": 2, 3 | "builds": [ 4 | { 5 | "src": "./index.js", 6 | "use": "@vercel/node" 7 | } 8 | ], 9 | "routes": [ 10 | { 11 | "src":"/(.*)", 12 | "dest": "/" 13 | } 14 | ] 15 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "adx-marketing", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "app.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1", 8 | "start": "nodemon index.js" 9 | }, 10 | "author": "", 11 | "license": "ISC", 12 | "dependencies": { 13 | "cors": "^2.8.5", 14 | "dotenv": "^16.0.3", 15 | "express": "^4.18.2", 16 | "nodemailer": "^6.9.1", 17 | "nodemon": "^2.0.22" 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # These are some examples of commonly ignored file patterns. 2 | # You should customize this list as applicable to your project. 3 | # Learn more about .gitignore: 4 | # https://www.atlassian.com/git/tutorials/saving-changes/gitignore 5 | 6 | # Node artifact files 7 | node_modules/ 8 | dist/ 9 | 10 | # Compiled Java class files 11 | *.class 12 | 13 | # Compiled Python bytecode 14 | *.py[cod] 15 | 16 | # Log files 17 | *.log 18 | 19 | # Package files 20 | *.jar 21 | 22 | # Maven 23 | target/ 24 | dist/ 25 | 26 | # JetBrains IDE 27 | .idea/ 28 | 29 | # Unit test reports 30 | TEST*.xml 31 | 32 | # Generated by MacOS 33 | .DS_Store 34 | 35 | # Generated by Windows 36 | Thumbs.db 37 | 38 | # Applications 39 | *.app 40 | *.exe 41 | *.war 42 | 43 | # Large media files 44 | *.mp4 45 | *.tiff 46 | *.avi 47 | *.flv 48 | *.mov 49 | *.wmv 50 | 51 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Node.js send email tutorial. 2 | 3 | ## Requirements 4 | 5 | * Nodejs 16/18 6 | 7 | Clone project and run test. 8 | 9 | ``` 10 | $ git clone https://git_url_clone 11 | $ cd 12 | $ npm install 13 | $ node index.js 14 | ``` 15 | 16 | "Update your administrator email account by modifying the '.env' file. If the '.env' file doesn't exist, create it and set the required variables."
17 | 18 | EMAIL_HOST
19 | EMAIL_USERNAME
20 | EMAIL_PASSWORD
21 | EMAIL_PORT
22 | 23 | "In this repository, I have also included a Vercel configuration. If you want to deploy your project on Vercel and run this API without any hosting costs, simply fork this repository and use your Vercel account. By selecting this repository in Vercel, your Node.js project will be live on Vercel, and you can manage the API from there. For example, if you forget to access the API at https://nodejs-send-email.vercel.co, your API endpoint will be https://nodejs-send-email.vercel.co/sendmail." 24 | 25 | Author: Waqar Ahmad 26 | 27 | Linkedin: https://www.linkedin.com/in/waqar134/ 28 | 29 | "A little bit of fragrance always clings to the hands that gives you roses!" 30 | 31 | Thanks for watching! 32 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | require('dotenv').config(); 2 | const express = require('express'); 3 | const nodemailer = require('nodemailer'); 4 | const cors = require('cors'); 5 | const app = express(); 6 | const PORT = 6786; 7 | const path = require('path'); 8 | app.use(express.static(path.join(__dirname, 'public'))); 9 | 10 | 11 | app.use(cors()); 12 | app.use(express.json()); 13 | 14 | const transporter = nodemailer.createTransport({ 15 | host: process.env.EMAIL_HOST || 'smtp.elasticemail.com', 16 | port: process.env.EMAIL_PORT || '2525', 17 | secure: false, 18 | auth: { 19 | user: process.env.EMAIL_USERNAME || 'adx-marketing@adxfactor.com', 20 | pass: process.env.EMAIL_PASSWORD || 'FE6F4B89A3A33B0CAD868D1566F2BD308414', 21 | }, 22 | }); 23 | 24 | 25 | app.get('/', (req, res) => { 26 | res.sendFile(path.join(__dirname, 'public', 'index.html')); 27 | }); 28 | 29 | app.post('/sendmail', (req, res) => { 30 | const { name ,email , phone , subject ,message } = req.body; 31 | const mailOptions = { 32 | from: 'adx-marketing@adxfactor.com', 33 | to: 'waqardevops134@gmail.com', 34 | subject: `Query from ${name}`, 35 | text: `Name: ${name} Email:${email} Phone:${phone} Subject:${subject} Message:${message}` 36 | }; 37 | transporter.sendMail(mailOptions, (error, info) => { 38 | if (error) { 39 | console.log(error); 40 | return res.json({ status: '0', message: 'Internal server error. Please try again' }); 41 | } else { 42 | return res.json({ status: '1', message: 'Message sent successfully' }); 43 | } 44 | }); 45 | }); 46 | 47 | app.listen(PORT, () => { 48 | console.log(`Server running on port ${PORT}`); 49 | }); 50 | --------------------------------------------------------------------------------