使用nodejs

时间:2015-11-06 06:43:17

标签: node.js email nodemailer

我有使用angularjs和nodejs构建的应用程序,管理员必须从应用程序中联系我们获取有关详细信息的邮件

我正在使用nodemailer但是我无法弄清楚如何发送来自邮件正文中的请求的用户输入的详细信息我得到的是req.body.name但是excepted是用户的名字(例如:sam)

var nodemailer = require('nodemailer');

var smtpTransport = nodemailer.createTransport("SMTP",{
  service: "Gmail",  // sets automatically host, port and connection security settings
  auth: {
    user: "dhanalakshmi.05k@gmail.com",
    pass: "*****"
  }
});

router.route('/sendmail')
  .post(function(req,res){
    console.log("!!!!!!!!!!!!!!!!1");
    console.log(req.body);
    smtpTransport.sendMail({  //email options
    from: "dhanalakshmi.05k@gmail.com", // sender address.  Must be the same as authenticated user if using Gmail.
    to: "dhanalakshmi@zendynamix.com", // receiver
    subject: "UsersQuery", // subject
    ***html: "<b style='color: #006600'>UserQuery</b><p>name:req.body.name</p><p>name:req.body.email</p><p>name:req.body.message</p>"***
    }, function(error, response){  //callback
  if(error){
    console.log(error);
  }else{
    console.log("Message sent: " + response.message);
  }
  smtpTransport.close(); // shut down the connection pool, no more messages.  Comment this line out to continue sending emails.
});
    res.send("done");
  });

2 个答案:

答案 0 :(得分:3)

您必须将变量放在引号之外并可能更改它们:

html: '<b style="color: #006600">UserQuery</b><p>name:' + req.body.name + '</p><p>name:' + req.body.email + '</p><p>name:' + req.body.message + '</p>'

仅对HTML属性(样式,类,id等)使用双引号,其余部分使用单引号。

答案 1 :(得分:3)

如果您使用node 4.0或更高版本,则应尝试使用template strings;你使用反引号字符而不是单引号或双引号。这允许多行字符串,而不必在每行的末尾添加"\n"+

这更容易阅读,也更酷!

html: `<b style='color: #006600'>UserQuery</b>
       <p>name: ${req.body.name}</p>
       <p>email: ${req.body.email}</p>
       <p>message: ${req.body.message}</p>`