V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
sindy
V2EX  ›  Node.js

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

  •  
  •   sindy · Aug 27, 2015 · 3639 views
    This topic created in 3896 days ago, the information mentioned may be changed or developed.
    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 ));
    }
    
    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   6079 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 67ms · UTC 02:06 · PVG 10:06 · LAX 19:06 · JFK 22:06
    ♥ Do have faith in what you're doing.