Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a function to validate the email in mail_validator.js #6

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 42 additions & 34 deletions api/controller/mailingController.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
const debug = require('debug')('app:mailingController');
const nodemailer = require('nodemailer');
const debug = require("debug")("app:mailingController");
const nodemailer = require("nodemailer");

function mailingController() {
function sendMail(req, res) {
(async function mail() {
try {
let { recipients, subject, body, cc, bcc } = req.body
debug(recipients, subject, body)
let { recipients, subject, body, cc, bcc } = req.body;
debug(recipients, subject, body);
if (!recipients || !subject || !body) {
res.status(400).send({
status: false,
message: 'These fields are required'
})
return
message: "These fields are required",
});
return;
}

let mailOptions = {
from: 'Team Fierce Mailing API <[email protected]>',
from: "Team Fierce Mailing API <[email protected]>",
to: recipients,
cc: [],
bcc: [],
Expand All @@ -25,36 +25,40 @@ function mailingController() {
};

let transporter = nodemailer.createTransport({
service: 'gmail',
service: "gmail",
auth: {
user: process.env.USER,
pass: process.env.PASSWORD
}
pass: process.env.PASSWORD,
},
});

transporter.sendMail(mailOptions, function (err, info) {
if (err) debug(err);
debug(`Email sent: ${info.response}`);
res.status(200).json({ status: 'success', data: {message: 'mail sent successfully'} });
})

res
.status(200)
.json({
status: "success",
data: { message: "mail sent successfully" },
});
});
} catch (err) {
debug(err.stack)
debug(err.stack);
}
}());
})();
}

function sendMailWithTemplate(req, res) {
(async function mail() {
try {
let { recipients, subject, body, cc, bcc } = req.body
debug(recipients, subject, body)
let { recipients, subject, body, cc, bcc } = req.body;
debug(recipients, subject, body);
if (!recipients || !subject || !body) {
res.status(400).send({
status: false,
message: 'These fields are required'
})
return
message: "These fields are required",
});
return;
}
// if (recipients.match(mailFormat)) {
// res.json({msg: true})
Expand All @@ -66,37 +70,41 @@ function mailingController() {
// }

let mailOptions = {
from: 'Team Fierce Mailing API <[email protected]>',
from: "Team Fierce Mailing API <[email protected]>",
to: recipients,
bcc: [],
subject: subject,
html: body
}
html: body,
};

let transporter = nodemailer.createTransport({
service: 'gmail',
service: "gmail",
auth: {
user: process.env.USER,
pass: process.env.PASSWORD
}
pass: process.env.PASSWORD,
},
});

transporter.sendMail(mailOptions, function (err, info) {
if (err) debug(err);
debug(`Email sent: ${info.response}`);
res.status(200).json({ status: 'success', data: {message: 'mail sent successfully'} });
})

res
.status(200)
.json({
status: "success",
data: { message: "mail sent successfully" },
});
});
} catch (err) {
debug(err.stack)
debug(err.stack);
}
}());
})();
}

return {
sendMail,
sendMailWithTemplate
sendMailWithTemplate,
};
}

module.exports = mailingController
module.exports = mailingController;
13 changes: 13 additions & 0 deletions api/utils/mail_validator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function validateEmail(emailText) {
const emailFormat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;

if (emailText.match(emailFormat)) {
return true;
}
console.log("You have entered an invalid email address!");
return false;
}

let checkValue = validateEmail("umehvictormariogmail.com");

console.log(checkValue);
30 changes: 14 additions & 16 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
const express = require('express')
const morgan = require('morgan'); //logger
const bodyParser = require('body-parser')
require('dotenv').config()
const express = require("express");
const morgan = require("morgan"); //logger
const bodyParser = require("body-parser");
require("dotenv").config();

const app = express();

app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(morgan('tiny'))
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(morgan("tiny"));

const mailingRouter = require("./api/routes/mailingRoutes")();

const mailingRouter = require('./api/routes/mailingRoutes')()
app.use("/api/v1", mailingRouter);

app.use('/api/v1', mailingRouter);

app.get('/', (req, res) => {
res.send('home')
app.get("/", (req, res) => {
res.send("home");
});

port = 4000
port = 4000;
app.listen(port, function () {
console.log(`Listening on port ${port}...`)
})

console.log(`Listening on port ${port}...`);
});