1 const express = require('express')
2 const app = express()
3 const dotenv = require('dotenv').config()
4
5 app.use(express.json())
6
7 // Server
8 const port = process.env.PORT || 6060
9
10 app.listen(port, () => {
11 console.log(`Server is listen on port ${port}`)
12 })
13
14 // test
15 app.get('/', (req, res) => {
16 res.json({ name: "Your Name" })
17 })
1 const mongoose = require('mongoose')
2
3 // Database
4 const connectDb = async () => {
5 try {
6 await mongoose.connect(process.env.MONGO_URI)
7 console.log("Connected to database")
8
9 } catch (error) {
10 console.log(error)
11 }
12 }
1 // Database
2 const connectDb = require('./connection/connect')
3 connectDb()
Sender is like Info about sender like, which service use for send mail ?, sender's email address and password.
mail is info about email which was send by sender. mail have from which is sender email, to which is receiver email, subject is subject of email and text is text write in email.
1 const nodemailer = require("nodemailer")
2
3 exports.sendMail = async (req, res) => {
4
5 let sender = nodemailer.createTransport({
6 service: 'Gmail',
7 auth: {
8 user: 'iamshiv20032003@gmail.com',
9 pass: process.env.PASS
10 }
11 })
12
13 let mail = {
14 from: 'iamshiv20032003@gmail.com',
15 to: "shivrajg20032003@gmail.com",
16 subject: "test",
17 text: "Send a email using node.js"
18 }
19
20 sender.sendMail(mail, function (error, info) {
21 if (error) {
22 res.status(500).json(error.message)
23 } else {
24 res.status(200).json("Email sent successfully" + info.response)
25 }
26 })
27 }
1. Open chrome and go to settings menu.
2. Click on Manage your Google Account.
3. Search for app password and click on it.
4. Next Input your email password to verify.
5. select app = mail.
6. select window (in my case i select = Windows computer).
7. Click on GENERATE.
8. Next screen will Show your Password ๐.
1 const router = require('express').Router()
2 const { sendMail } = require('../controllers/mail')
3
4 router.route('/mail/new').post(sendMail)
5
6 module.exports = router
1 const router = require('express').Router()
2
3 // mail routes
4 const mailRoute = require('./mail')
5 router.use('/', mailRoute)
6
7 module.exports = mailRoute
1 // routes
2 const router = require('./routes/router')
3 app.use('/api', router)
1 const nodemailer = require("nodemailer")
2
3 exports.sendMail = (req, res) => {
4
5 const { email, subject, text } = req.body
6 const { originalname, buffer } = req.file
7
8 let sender = nodemailer.createTransport({
9 service: 'Gmail',
10 auth: {
11 user: 'iamshiv20032003@gmail.com',
12 pass: process.env.PASS
13 }
14 })
15
16 let mail = {
17 from: 'iamshiv20032003@gmail.com',
18 to: email,
19 subject: subject,
20 text: text,
21 attachments: {
22 filename: originalname,
23 content: buffer
24 }
25 }
26
27 sender.sendMail(mail, function (error, info) {
28 if (error) {
29 res.status(500).json(error.message)
30 } else {
31 res.status(200).json("Email sent successfully:" + info.response)
32 }
33 })
34
35 }
1 const router = require('express').Router()
2 const { sendMail } = require('../controllers/mail')
3
4 const multer = require('multer')
5 const upload = multer()
6
7 router.route('/mail/new').post(upload.single("attachment"), sendMail)
8
9 module.exports = router