简体   繁体   English

节点mandrill通过服务器发送邮件

[英]node-mandrill sending mail via server

this is a newbie question on using node-mandrill properly and most probably on node itself as I am still trying to learn it. 这是关于正确使用node-mandrill的一个新手问题,很可能是关于节点本身的,因为我仍在尝试学习它。 But since I have seen many examples to use the api key of mandrill directly from the client side and therefore revealing it, I was wondering how exactly it was working when served but got stuck at this point: 但是,由于我已经看到许多示例直接从客户端使用mandrill的api密钥并因此进行揭示,因此我想知道当服务时它到底有多有效,但此时却卡住了:

I have an app.js serving a public folder... 我有一个提供公共文件夹的app.js ...

app.js app.js

var express = require('express');
var mandrill = require('node-mandrill')(API_KEY); 
var app = express();
app.use(express.static('public'));
app.listen(PORT);

function sendEmail ( _name, _email, _subject, _message) {
    mandrill('/messages/send', {
        message: {
            to: 'EMAIL',
            from: [{email: _email , name: _name}],
            subject: _subject,
            text: _message
        }
    }, function(error, response){
        if (error) console.log( error );
        else console.log(response);
    });
}

...where a client script is used to collect info from a contact form and send an email upon clicking a submit button. ...使用客户端脚本从联系表单收集信息并在单击提交按钮后发送电子邮件。

form.js form.js

var contactForm = document.getElementById( 'contactForm' );
            new stepsForm( contactForm, {
                onSubmit : function( form ) {

                    // send email
                    var _subject = 'Contact Request';
                    var _email = contactForm.elements['q4'].value;
                    var _name = contactForm.elements['q5'].value;
                    var _message = contactForm.elements['q6'].value;

                    sendEmail(_name,_email,_subject,_message);

                }
            } );

Could you please tell me what's missing/wrong? 您能告诉我什么缺失/错误吗? Thanks. 谢谢。

You can't call your back-end function from client. 您不能从客户端调用后端函数。 You can use Jquery to make Ajax call like: 您可以使用Jquery进行Ajax调用,例如:

$(document).ready(function(){

      $('#contactForm').submit(function(event) {
        event.preventDefault();
        var _subject = 'Contact Request';
        var _email = contactForm.elements['q4'].value;
        var _name = contactForm.elements['q5'].value;
        var _message = contactForm.elements['q6'].value;

        $.ajax({
          url: '/sendEmail',
          method: 'post',
          contentType: 'application/json',
          data: JSON.stringify({ subject: _subject, email: _email}),
          success: function(res) {

          }
        })

      })

})

And in your back-end: 在您的后端:

// some lines skipped

app.post('/sendEmail', function(req, res) {

     //do your stuff
})

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

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