使用带有HTML正文

时间:2017-12-10 05:52:37

标签: node.js ecmascript-6 nodemailer template-strings

我正在尝试使用nodemailer从我的服务器发送电子邮件。不幸的是,由于这个错误,我一直没有测试它:

D:\Full Stack\Node\NodeLoginJWT\functions\password.js:58
        'This token is valid only within two minutes.'
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: Unexpected string
at createScript (vm.js:56:10)
at Object.runInThisContext (vm.js:97:10)
at Module._compile (module.js:542:28)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (D:\Full Stack\Node\NodeLoginJWT\routes.js:9:18)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
[nodemon] app crashed - waiting for file changes before starting...

这是导致错误的代码块:

const transporter = nodeMailer.createTransport(`smtps://${config.email}:${config.password}@smtp.gmail.com`);

  const mailOptions = {
    from: `"${conifg.name}" <${config.email}>`,
    to: email,
    subject: 'Reset Password',
    html: `Hello ${user.name}`, 

        'Your account password token is ${random}'
        'This token is valid only within two minutes.'

        'Thanks,'
        'Team. '
  };

  return transporter.sendMail(mailOptions);

2 个答案:

答案 0 :(得分:1)

我认为插值存在问题。

  const mailOptions = {
    from: `"${conifg.name}" <${config.email}>`,
    to: email,
    subject: 'Reset Password',
    html: `Hello ${user.name}, // the tick should not come here 

        'Your account password token is ${random}'
        'This token is valid only within two minutes.'

        'Thanks,'
        'Team. '` // the tick should come here
  };

答案 1 :(得分:0)

我正在使用Nodemailer,这是我的代码:

var express = require('express');
var router = express.Router();
var nodemailer = require('nodemailer');

router.post('/', handleSendEmail); // handle the route at yourdomain.com/sayHello

function handleSendEmail(req, res) {
    // Not the movie transporter!
    var transporter = nodemailer.createTransport({
     service: 'Gmail',
     auth: {
         user: '', // Your email id
         pass: ‘’// Your password
     }
    });
    var text = 'Hello from \n\n' + req.body.user_name;
    var mailOptions = {
        from: 'sender@gmail.com', // sender address
        to: 'receiver@gmail.com', // list of receivers
        subject: 'Appointment Email Example', // Subject line
        text: text,
        html: '<!DOCTYPE html>'+
        '<html><head><title>Appointment</title>'+
        '</head><body><div>'+
        '<img src="http://evokebeautysalon1.herokuapp.com/main/img/logo.png" alt="" width="160">'+
        '<p>Thank you for your appointment.</p>'+
        '<p>Here is summery:</p>'+
        '<p>Name: James Falcon</p>'+
        '<p>Date: Feb 2, 2017</p>'+
        '<p>Package: Hair Cut </p>'+
        '<p>Arrival time: 4:30 PM</p>'+
        '</div></body></html>'
    };
    transporter.sendMail(mailOptions, function(error, info){
        if(error){
            console.log(error);
            res.json({yo: 'error'});
        }else{
            console.log('Message sent: ' + info.response);
            res.json({yo: info.response});
        };
    });
}

module.exports = router;