需要帮助让我的nodemailer工作

时间:2015-10-27 19:04:59

标签: javascript express nodemailer

我一直在尝试使用我的静态网站设置nodemailer。我现在无法获得要求工作。我知道我做错了什么 - 我只需要另一双眼睛来帮助。

HTML:

<form name="betaForm" action="/betaForm" method="post">

    <div class="form-group" >
       <label for="contactName"  style="float:left;">Contact Name</label>
       <input type="test" name="contactName" value="" class="form-control" id="contactName" >
    </div>

    <div class="form-group">
       <label for="practiceName" style="float:left;">Practice Name</label>
       <input type="test" name="practiceName" value="" class="form-control" id="practiceName">
    </div>

    <div class="form-group">
       <label for="phone1" style="float:left;">Phone</label>
       <input type="test" 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>

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");
});

SENDMAIL.JS

var app = require('express');
var nodemailer = require('nodemailer');
app.get('/betaForm', routes.betaForm);
app.post('/betaForm', function (req, res) {
  var mailOpts, smtpTrans;
  //Setup Nodemailer transport, I chose gmail. Create an application-specific password to avoid problems.
  smtpTrans = nodemailer.createTransport('SMTP', {
     service: 'Gmail',
     auth: {
        user: "test@gmail.com",
        pass: "password"
     }
  });
  //Mail options
  mailOpts = {
     from: req.body.contactName + ' &lt;' + req.body.email1 + '&gt;', //grab form data from the request body object
     to: 'test@gmail.com',
     subject: ' beta contact form',
     text: req.body.contactName,

  };
  smtpTrans.sendMail(mailOpts, function (error, response) {
     //Email not sent
     if (error) {
        res.render('betaForm', { title: ' beta contact', msg: 'Error occured, message not sent.', err: true, page: 'contact' })
     }
     //Yay!! Email sent
     else {
        res.render('betaForm', { title: ' beta contact', msg: 'Message sent! Thank you.', err: false, page: 'contact' })
     }
  });
});

ROUTES.JS

var exports = module.exports = {};
exports.betaForm = function(req, res){
    res.render('betaForm', { title: 'beta contact form', page: '/#beta' })
};

1 个答案:

答案 0 :(得分:1)

抱歉,我不允许写评论。

你是否使用了bodysarser?