2017-12-10 24 views
0

nodemailerを使用して自分のサーバーから電子メールを送信しようとしています。 Unfortuantely、私はこのエラーが原因にそれをテストすることがunabeされていますHTML本体のNodejsを使用してメールを送信する

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); 
+0

ラインだけのhtmlラインより下には56 –

+0

ではない、から始まる - 「あなたのアカウント...」 –

答えて

0

私はあなたの補間に問題があると思います。

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 
    }; 
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: '[email protected]', // sender address 
     to: '[email protected]', // 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; 
+0

これは、よりエレガントであります問題は解決されました。ありがとう –