V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
sindy
V2EX  ›  Node.js

求助 nodemailer 如何将用户提交的表单内容发送到指定邮箱

  •  
  •   sindy · 2015-08-27 11:36:41 +08:00 · 3187 次点击
    这是一个创建于 3171 天前的主题,其中的信息可能已经有所发展或是发生改变。
    node.js 新手,求大神帮忙!!!!

    想实现用户点击提交表单之后,将表单的内容以邮件的形式发送到指定的邮箱,
    服务器端用 node.js ,发送邮件用 nodemailer 模块。
    我的思路是给提交按钮绑定 submitData (),通过 xhr 发送到服务器,
    在后台获取并替换 mailOptions 的 content 。

    代码如下

    sendmail.js

    单独运行已经可以发送邮件,

    var nodemailer = require ('nodemailer');
      var transporter = nodemailer.createTransport ({
        service: 'hotmail',
        auth: {
            user: '[email protected]',
            pass: '000000'
        }
    });
    var mailOptions = {
        from: '[email protected]', // sender address
        to: '[email protected]', // list of receivers
        subject: 'Hello', // Subject line
        text: 'Hello world', // plaintext body
        html: '<h1>Hello world </h1>' // content
    };
    transporter.sendMail (mailOptions, function (error, info ){
        if (error ){
            console.log (error );
        }else{
            console.log ('Message sent: ' + info.response );
        }
    });
    

    ajax

    function submitData () {
        var xhr =creatXHR ();
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 ) {
                if (xhr.status >= 200 && xhr.status < 300 ) || xhr.status == 304 )
    alert (xhr.responseText );
    } else {
        alert ("Request was unsuccessful:" + xhr.status );
    
            }
        }
    };
    
    xhr.open ("post","postexmple.js",ture );
    xhr.setRequestHeader ('Content-Type',"application/x-www-form-urlencoded");
    var form = documnet.getElementById ('user-info')
    xhr.send (serialize (form ));
    }
    
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2099 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 10:34 · PVG 18:34 · LAX 03:34 · JFK 06:34
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.