Skip to content

Instantly share code, notes, and snippets.

@Bucephalus-lgtm
Last active March 8, 2022 06:28
Show Gist options
  • Save Bucephalus-lgtm/74e12bdc934b5db46e9ee5ac03e1f13e to your computer and use it in GitHub Desktop.
Save Bucephalus-lgtm/74e12bdc934b5db46e9ee5ac03e1f13e to your computer and use it in GitHub Desktop.
Send Email After Verifying User Credentials
const express = require('express');
const bodyParser = require('body-parser');
const nodemailer = require('nodemailer');
const app = express();
const path = require('path');
const dotenv = require('dotenv');
dotenv.config();
app.use(express.static('public'));
app.set(bodyParser.json());
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.get('/', function(req, res) {
res.render('home');
});
app.get('/verification', function(req, res) {
res.render('index');
});
app.post('/send-mail', (req, res) => {
const smtpTrans = nodemailer.createTransport({
service: 'gmail',
type: 'SMTP',
host: 'smtp.gmail.com',
secure: true,
auth: {
user: process.env.EMAIL,
pass: process.env.PASSWORD
}
});
const mailOpts = {
from: `${process.env.EMAIL}`,
to: `${req.body.email}`,
subject: "It's an urgent message",
text: `${req.body.name} (${req.body.email}) says: ${req.body.message}`
}
smtpTrans.sendMail(mailOpts, (error, response) => {
if (error) {
console.log(error);
// res.send('failed') // Show a page indicating failure
res.json({
msg: 'failed!',
error
});
} else {
// res.send('success') // Show a page indicating success
res.json({
msg: 'success!'
});
}
})
})
const port = process.env.PORT || 5000;
app.listen(port, function() {
co
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment