For some time now, I've been struggling to configure nodemailer on my static website. The main issue I'm facing is getting the 'require' function to work properly at this moment. I am aware that I must be missing something in my setup - hence, I need another perspective to help me out.
Here's a snippet of the HTML:
<form name="betaForm" action="/betaForm" method="post">
<div class="form-group" >
<label for="contactName" style="float:left;">Contact Name</label>
<input type="text" name="contactName" value="" class="form-control" id="contactName">
</div>
<div class="form-group">
<label for="practiceName" style="float:left;">Practice Name</label>
<input type="text" name="practiceName" value="" class="form-control" id="practiceName">
</div>
<div class="form-group">
<label for="phone1" style="float:left;">Phone</label>
<input type="text" name="phone1" value="" class="form-control" id="phone1">
</div>
<div class="form-group">
<label for="email1" style="float:left;">Email</label>
<input type="email" name="email1" value="" class="form-control" id="email1">
</div>
<button type="submit" value="Send" class="btn btn-default">Submit</button>
</form>
Here's a section from SERVER.JS:
var express = require('express');
var nodemailer = require("nodemailer");
var app = express();
app.get('/',function(req,res){
res.sendFile('www/index.html');
});
app.listen(3000,function(){
console.log("Express Started on Port 3000");
});
And here's SENDMAIL.JS:
var app = require('express');
var nodemailer = require('nodemailer');
app.get('/betaForm', routes.betaForm);
app.post('/betaForm', function (req, res) {
var mailOpts, smtpTrans;
//Setting up Nodemailer transport using Gmail
smtpTrans = nodemailer.createTransport('SMTP', {
service: 'Gmail',
auth: {
user: "example@gmail.com",
pass: "password"
}
});
//Mail options
mailOpts = {
from: req.body.contactName + ' <' + req.body.email1 + '>',
to: 'recipient@example.com',
subject: 'Beta Contact Form',
text: req.body.contactName,
};
smtpTrans.sendMail(mailOpts, function (error, response) {
if (error) {
res.render('betaForm', { title: 'Beta Contact', msg: 'Error occurred, message not sent.', err: true, page: 'contact' })
} else {
res.render('betaForm', { title: 'Beta Contact', msg: 'Message sent! Thank you.', err: false, page: 'contact' })
}
});
});
Lastly, ROUTES.JS contains:
var exports = module.exports = {};
exports.betaForm = function(req, res){
res.render('betaForm', { title: 'Beta Contact Form', page: '/#beta' })
};