2017-08-02 133 views
0

我试图用nodemailer模块发送电子邮件给用户使用node.js,但它抛出了下面的错误。使用Node.js实现nodemailer发送邮件时出错error

错误:

this.transporter.mailer = this; 
           ^

TypeError: Cannot create property 'mailer' on string 'SMTP' 
    at Mail (/opt/lampp/htdocs/heroku/FGDP/node_modules/nodemailer/lib/mailer/index.js:45:33) 
    at Object.module.exports.createTransport (/opt/lampp/htdocs/heroku/FGDP/node_modules/nodemailer/lib/nodemailer.js:46:14) 
    at Object.<anonymous> (/opt/lampp/htdocs/heroku/FGDP/api/api.js:8:32) 
    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) 
    at require (internal/module.js:20:19) 

我解释下面我的代码。

var nodemailer = require("nodemailer"); 
var smtpTransport = nodemailer.createTransport("SMTP",{ 
    service: "Gmail", 
    auth: { 
     user: "[email protected]", 
     pass: "***********" 
    } 
}); 

在这里我需要发送邮件给有效的用户,但得到上述错误。

+0

根据[本文档(https://nodemailer.com/smtp/),第一个参数是一个JavaScript对象,而不是字符串“ SMTP”。 – Myonara

回答

2

我认为正确的语法是这样的

var nodemailer = require("nodemailer"); 
var smtpTransport = nodemailer.createTransport({ 
service: "Gmail", 
auth: { 
    user: "[email protected]", 
    pass: "***********" 
} 
}); 
+0

它不能与heroku部署一起使用。 – satya

+0

这只是一个例子。我的意思是说你必须删除“SMTP”。它不在nodemailer文档中 –