简体   繁体   English

MEAN.JS联系表格

[英]MEAN.JS Contact Form

Trying to create a contact form and feedback form for my website. 尝试为我的网站创建联系表和反馈表。 Here is my route and controller I'm using however I need to understand what's going on with my routes and how to capture the input fields from the form implement this inside MEAN.JS: 这是我正在使用的路由和控制器,但是我需要了解路由发生了什么以及如何从表单中捕获输入字段,该表单在MEAN.JS内部实现:

route.js : route.js

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

app/controller.js : 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);
        }
    });

 };

Not sure how this work with the HTML with the view: 不确定如何通过视图使用HTML:

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

If I understand the question correctly, you're asking how you can gather data which is input into a form, send that data to expressJS, and then use that data to send an outbound email. 如果我对问题的理解正确,那么您在问如何收集输入到表单中的数据,将该数据发送到expressJS,然后使用该数据发送出站电子邮件。

If so, then here is your flow: 如果是这样,那么您的流程如下:

Step 1: Create a form in a view and map it to an AngularJS controller 步骤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>

Step 2: In your AngularJS controller, use a $http request to send your data to your Express Route 步骤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.
  });
}

Step 3: Use your ExpressJS Route as an API to call your ExpressJS Controller. 步骤3:使用ExpressJS Route作为API来调用ExpressJS Controller。 (It looks like you've got this bit covered) (看来您已经了解了这一点)

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

Step 4: Receive and do something with the data passed through the $http POST 步骤4:接收并处理通过$ http POST传递的数据

exports.createmail = function(req, res) {
var data = req.body;

Now you can use the data, like this 现在您可以像这样使用数据了

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

MeanJS 0.4.0 also has a working example of NodeMailer which might help: https://github.com/meanjs/mean/tree/0.4.0 MeanJS 0.4.0还有一个NodeMailer的工作示例,可能会有所帮助: https : //github.com/meanjs/mean/tree/0.4.0

使用angularJS,您可以删除action属性,而仅使用angularJS ngSubmit指令,并在控制器中调用一个函数,该函数现在将使用$ http.get访问端点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM