MEAN.JS联系表格

时间:2015-04-19 16:02:46

标签: angularjs forms contact meanjs

尝试为我的网站创建联系表单和反馈表单。这是我正在使用的路由和控制器但是我需要了解我的路由发生了什么以及如何从表单中捕获输入字段在MEAN.JS中实现:

route.js

app.route('/mail').get(mail.createmail);

app/controller.js

    exports.createmail = function(req, res) {

    var mailOpts, smtpTrans;
    // create reusable transporter object using SMTP transport
    var transporter = nodemailer.createTransport({
        service: 'Gmail',
        auth: {
            user: 'administrator@radleaf.com',
            pass: '34Girls34*goo'
        }
    });

    // NB! No need to recreate the transporter object. You can use
    // the same transporter object for all e-mails

    // setup e-mail data with unicode symbols
    var mailOptions = {
        from: 'Fred Foo ✔ <foo@blurdybloop.com>', // sender address
        to: 'ty@radleaf.com', // list of receivers
        subject: 'Hello ✔', // Subject line
        text: 'Hello world ✔', // plaintext body
        html: '<b>Hello world ✔</b>' // html body
    };

    // send mail with defined transport object
    transporter.sendMail(mailOptions, function (error, info) {
        if (error) {
            console.log(error);
        } else {
            console.log('Message sent: ' + info.response);
        }
    });

 };

不确定如何使用带有视图的HTML:

<form action="mail">...</form>

1 个答案:

答案 0 :(得分:4)

如果我正确理解了这个问题,您就会问如何收集输入到表单中的数据,将该数据发送到expressJS,然后使用该数据发送出站电子邮件。

如果是这样,那么这是你的流程:

第1步:在视图中创建表单并将其映射到 AngularJS控制器

<form name="contactForm" data-ng-submit="sendMail()">
Name: <input type="text" data-ng-model="contact_name">
Message: <input type="text" data-ng-model="contact_msg">
<button type="submit">
</form>

步骤2:在AngularJS控制器中,使用$ http请求将数据发送到Express Route

$scope.sendMail = function() {
// Simple POST request example (passing data) :
$http.post('/mail', {name: contact_name, msg: contact_msg}).
  success(function(data, status, headers, config) {
    // this callback will be called asynchronously
    // when the response is available
  }).
  error(function(data, status, headers, config) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });
}

步骤3:使用ExpressJS Route作为API来呼叫ExpressJS控制器。 (看起来你已经覆盖了这一点)

app.route('/mail').get(mail.createmail);

步骤4:接收并通过$ http POST

传递的数据执行某些操作
exports.createmail = function(req, res) {
var data = req.body;

现在您可以使用数据,例如

   var mailOptions = {
        from: data.name, // sender name
        text: data.msg, // plaintext body
    };

MeanJS 0.4.0也有一个NodeMailer的工作示例,可能有所帮助:https://github.com/meanjs/mean/tree/0.4.0