我尝试通过nodemailer发送电子邮件但收到错误 - TypeError: Cannot read property 'method' of undefined
. 它看起来像 sendMail
函数未定义 . 有什么建议吗?附:此代码用于在AWS上托管的chatbot
var nodemailer = require('nodemailer');
var smtpTransport = require('nodemailer-smtp-transport');
module.exports = function someName() {
// create reusable transporter object using the default SMTP transport
var transporter = nodemailer.createTransport(smtpTransport({
service: 'gmail',
auth: {
user: '7384093@gmail.com',
pass: '*******'
}
}))
// setup e-mail data with unicode symbols
var mailOptions = {
from: '"nerd studio" <7384093@gmail.com>', // sender address
to: '7384093@gmail.com', // list of receivers
subject: 'Подтверждение запроса \\ разработак чат-ботов \\ nerd studio', // Subject line
text: 'Добрый день! Вы оставили нашему Валере запрос и мы с радостью подтверждаем его получение. В ближайшее время с вами свяжется наш менелдер', // plaintext body
html: '<b>Добрый день! Вы оставили нашему Валере запрос и мы с радостью подтверждаем его получение. В ближайшее время с вами свяжется наш менелдер</b>' // html body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info){
console.log(mailOptions);
console.log(info);
if(error){
return console.log(error);
}
console.log('Message sent: ' + info.response);
});
}
5 回答
您不需要安装npm nodemailer-smtp-transport,只有nodemailer足以向gmail发送电子邮件 . 但首先,转到https://myaccount.google.com/security谷歌帐户并向下滚动并选中允许不太安全的应用程序:打开并保持打开状态 . 你会发送你的Gmail电子邮件 . 这里是完整的代码 -
var nodemailer = require('nodemailer'); app.post('/ contactform',function(req,res){
我有nodemailer目前正在这样工作:创建一个文件config / mail.js:
然后,我想随时发送电子邮件:
试试这个代码 . 首先你必须在库中创建一个应用程序
Google Cloud Console
和Enable Gmail API
. 获取你的应用程序的凭据 . 为了点击Credentials
并在Authorized redirect URIs
的地方保留此链接https://developers.google.com/oauthplayground并保存它 . 在另一个标签中打开这个打开这个链接https://developers.google.com/oauthplayground/点击右侧的设置符号 . 勾选复选框(即,使用您自己的OAuth凭据)之后您必须在左侧同时给您的clientId和clientSecret.And有一个文本框占位符如Input Your Own Scopes
那里保留此链接https://mail.google.com/然后单击授权API然后单击Exchange authorization code for tokens
然后您将获得refreshToken
和accessToken
将这两个保留在您的代码中 . 希望您能为您提供帮助 .I find solution for , 如何从= "userEmail"发送电子邮件至= "myEmail"? THIS IS TRICK
Using Gmail
Using Hotmail
或者,如果您的帐户是hotmail而不是outlook,则可以使用以下传输使用内置hotmail服务:
Using Zoho