我尝试通过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);
});
}
答案 0 :(得分:3)
您不需要安装npm nodemailer-smtp-transport,只有nodemailer足以向gmail发送电子邮件。但首先,转到https://myaccount.google.com/security Google帐户并向下滚动并选中允许不太安全的应用:开启并保持开启状态。你会发送你的Gmail电子邮件。这里是完整的代码 -
var nodemailer = require(&#39; nodemailer&#39;); app.post(&#39; / contactform&#39;,function(req,res){
var mailOpts, smtpTrans;
//Setup Nodemailer transport, I chose gmail. Create an application-specific password to avoid problems.
smtpTrans = nodemailer.createTransport(smtpTransport({
service: 'gmail',
// host:'smtp.gmail.com',
// port:465,
// secure:true,
auth: {
user: "xxxxxx@gmail.com",
pass: "xxxxxx"
}
}));
var mailoutput = "<html>\n\
<body>\n\
<table>\n\
<tr>\n\
<td>Name: </td>" + req.body.form_name + "<td></td>\n\
</tr>\n\
<tr>\n\
<td>Email: </td><td>" + req.body.form_email + "</td>\n\
</tr>\n\
<tr>\n\
<td>MN: </td>" + req.body.form_phone + "<td></td>\n\
</tr>\n\
<tr>\n\
<td>Messge: </td>" + req.body.form_message + "<td></td>\n\
</tr>\n\
</table></body></html>";
//Mail options
mailOpts = {
to: "Your_App_Name <xxxxxxxx@gmail.com>",
subject: req.body.form_subject,
html: mailoutput
};
smtpTrans.sendMail(mailOpts, function (error, res) {
if (error) {
// res.send("Email could not send due to error" +error);
return console.log(error);
}
});
console.log('Message sent successfully!');
res.render('contact.ejs');
});
//console.log(query.sql);
});
答案 1 :(得分:1)
我的nodemailer目前正在以这种方式工作:创建文件config / mail.js:
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
host: 'yourHost',
port: 2525, //example
auth: {
user: 'yourUser',
pass: 'yourPass'
}
});
module.exports = function(params) {
this.from = 'yourEmail';
this.send = function(){
var options = {
from : this.from,
to : params.to,
subject : params.subject,
text : params.message
};
transporter.sendMail(options, function(err, suc){
err ? params.errorCallback(err) : params.successCallback(suc);
});
}
}
然后,我想随时发送电子邮件:
var Mail = require(path.join(__dirname, '..', '..', 'config', 'mail.js'));
var options = {
to: 'example@example.com',
subject: 'subject',
message: 'your message goes here'
}
var mail = new Mail({
to: options.to,
subject: options.subject,
message: options.message,
successCallback: function(suc) {
console.log('success');
},
errorCallback: function(err) {
console.log('error: ' + err);
}
});
mail.send();
答案 2 :(得分:1)
尝试此代码。首先,您必须在库中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
将这两个保留在您的代码中。请为您提供此内容。
const nodemailer=require('nodemailer');
const xoauth2=require('xoauth2');
var transporter=nodemailer.createTransport({
service:'gmail',
auth:{
type: 'OAuth2',
user:'Your_Email',
clientId:'Your_clientId',//get this from Google Cloud Console
clientSecret:'Your_clientSecret',
refreshToken:'Your_refreshToken',//get this from https://developers.google.com/oauthplayground/
accessToken:'Your_accessToken'
},
});
var mailOptions={
from:'<Your_email>',
to:'Your firends mail',
subject:'Sample mail',
text:'Hello !!!!!!!!!!!!!'
}
transporter.sendMail(mailOptions,function(err,res){
if(err){
console.log('Error');
}
else{
console.log('Email Sent');
}
})
答案 3 :(得分:0)
我找到了解决方案, 如何从=“userEmail”发送电子邮件至=“myEmail”? 这是罢工
var nodemailer = require('nodemailer'); router.post('/contacts-variant-2', (req, res, next) => { var name=req.body.name; var email=req.body.email; var message=req.body.message; const output=`
<h3>Contact Details</h3>
<ul>
<li>Name is : ${req.body.name}</li>
<li>Email is : ${req.body.email}</li>
</ul>
<h3>Message</h3>
<p>${req.body.message}</p>
`; var transporter = nodemailer.createTransport({ service: 'yahoo', auth: { user: 'create_new_email@yahoo.com', pass: 'password' } }); var mailOptions = { from:'create_new_email@yahoo.com', to:'myFriend@gmail.com', subject: name, text: 'Your have a new
contact request', html:output }; transporter.sendMail(mailOptions, function(error, info){ if (error) { console.log("errors is somthing "+error); res.send(404); } else { console.log('Email sent: ' + info.response); res.send(200); } }); });
答案 4 :(得分:-1)
使用Gmail
var nodemailer = require('nodemailer');
// Create the transporter with the required configuration for Gmail
// change the user and pass !
var transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 465,
secure: true, // use SSL
auth: {
user: 'myemail@gmail.com',
pass: 'myPassword'
}
});
// setup e-mail data
var mailOptions = {
from: '"Our Code World " <myemail@gmail.com>', // sender address (who sends)
to: 'mymail@mail.com, mymail2@mail.com', // list of receivers (who receives)
subject: 'Hello', // Subject line
text: 'Hello world ', // plaintext body
html: '<b>Hello world </b><br> This is the first email sent with Nodemailer in Node.js' // html body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info){
if(error){
return console.log(error);
}
console.log('Message sent: ' + info.response);
});
使用Hotmail
var nodemailer = require('nodemailer');
// Create the transporter with the required configuration for Outlook
// change the user and pass !
var transporter = nodemailer.createTransport({
host: "smtp-mail.outlook.com", // hostname
secureConnection: false, // TLS requires secureConnection to be false
port: 587, // port for secure SMTP
tls: {
ciphers:'SSLv3'
},
auth: {
user: 'mymail@outlook.com',
pass: 'myPassword'
}
});
// setup e-mail data, even with unicode symbols
var mailOptions = {
from: '"Our Code World " <mymail@outlook.com>', // sender address (who sends)
to: 'mymail@mail.com, mymail2@mail.com', // list of receivers (who receives)
subject: 'Hello ', // Subject line
text: 'Hello world ', // plaintext body
html: '<b>Hello world </b><br> This is the first email sent with Nodemailer in Node.js' // html body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info){
if(error){
return console.log(error);
}
console.log('Message sent: ' + info.response);
});
或者,如果您的帐户是hotmail而不是outlook,则可以使用以下传输方式使用build-in hotmail服务:
var transport = nodemailer.createTransport("SMTP", {
service: "hotmail",
auth: {
user: "user@hotmail.com",
pass: "password"
}
});
使用Zoho
var nodemailer = require('nodemailer');
// Create the transporter with the required configuration for Gmail
// change the user and pass !
var transporter = nodemailer.createTransport({
host: 'smtp.zoho.com',
port: 465,
secure: true, // use SSL
auth: {
user: 'myzoho@zoho.com',
pass: 'myPassword'
}
});
// setup e-mail data, even with unicode symbols
var mailOptions = {
from: '"Our Code World " <myzoho@zoho.com>', // sender address (who sends)
to: 'mymail@mail.com, mymail2@mail.com', // list of receivers (who receives)
subject: 'Hello ', // Subject line
text: 'Hello world ', // plaintext body
html: '<b>Hello world </b><br> This is the first email sent with Nodemailer in Node.js' // html body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info){
if(error){
return console.log(error);
}
console.log('Message sent: ' + info.response);
});