Am trying to create an app that sends mails. It gets the user input(To, Subject, Message) onClick of the form button sends a mail, and store that mail on mongodb
front end
<form>
<span>
to :<input type='text' >
</span>
cc :<input type='text' >
</span>
bcc :<input type='text' >
</span>
<span>
subject :<input type='text' >
</span>
<span>
message :<input type='text' >
</span>
</form>
Back end
to = '[email protected]',
cc = '[email protected]',
bcc = '[email protected]',
subject = 'A project proposal',
message = 'the body of your mail',
etc...
Schema
const mongoose = require('mongoose');
const UserSchema = new mongoose.Schema({
to: { type: String, }, cc: { type: String, }, bcc: { type: String, }, bcc: { type: String, }, subject: { type: String, }, message: { type: String, }, attachment: { type: String, }, date: { type: Date, default: Date.now },
});
const Mail = mongoose.model('Mail', UserSchema);
module.exports = Mail;
A.p.i
const Mail = require('../models/Mail');
// Home Page
router.get('/', forwardAuthenticated, (req, res) => res.render('home'));
// Mail
router.get('/mail', ensureAuthenticated, (req, res) =>
res.render('mail', {
user: req.user,
mail: req.mail
})
);
router.post('/mail', (req, res) => {
const { to, cc, bcc, subject, message, attachment, account } = req.body;
let errors = [];
if (!name || !subject || !message || !account) {
errors.push({ msg: 'Please enter all fields' });
}
if (errors.length > 0) {
res.render('register', {
errors,
name,
subject,
message,
account
});
} else {
const newMail = new Mail({
to,
cc,
bcc,
subject,
message,
attachment,
account
});
newMail
.save()
.then(mail => {
req.flash(
'success_msg',
'mail sent'
);
})
.catch(err => console.log(err));
}
})
module.exports = router;
how do i go about it here?