[英]Nodemailer using gmail, Cannot create property 'mailer' on string 'SMTP'
I am trying to send the data from the form I created to my gmail account, when clicking the sumbit button I always get the same error. 我试图将我创建的表单中的数据发送到我的Gmail帐户,当点击sumbit按钮时我总是得到相同的错误。 I found a lot of issues about nodemailer, but none of them seem to be the same issue as I am experiencing. 我发现了很多关于nodemailer的问题,但它们似乎都不像我遇到的那样。
Ofcourse I have stated my clientId but just deleted for this post. 当然我已经说过我的clientId但是刚刚删除了这篇文章。
TypeError: Cannot create property 'mailer' on string 'SMTP'
at Mail (C:\Users\snowr\Documents\dev\zorgkaartnl\zorgkaartnl\node_modules\nodemailer\lib\mailer\index.js:45:33)
at Object.module.exports.createTransport (C:\Users\snowr\Documents\dev\zorgkaartnl\zorgkaartnl\node_modules\nodemailer\lib\nodemailer.js:46:14)
at C:\Users\snowr\Documents\dev\zorgkaartnl\zorgkaartnl\src\app.js:39:26
at Layer.handle [as handle_request] (C:\Users\snowr\Documents\dev\zorgkaartnl\zorgkaartnl\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\snowr\Documents\dev\zorgkaartnl\zorgkaartnl\node_modules\express\lib\router\route.js:131:13)
at Route.dispatch (C:\Users\snowr\Documents\dev\zorgkaartnl\zorgkaartnl\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\snowr\Documents\dev\zorgkaartnl\zorgkaartnl\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\snowr\Documents\dev\zorgkaartnl\zorgkaartnl\node_modules\express\lib\router\index.js:277:22
at Function.process_params (C:\Users\snowr\Documents\dev\zorgkaartnl\zorgkaartnl\node_modules\express\lib\router\index.js:330:12)
at next (C:\Users\snowr\Documents\dev\zorgkaartnl\zorgkaartnl\node_modules\express\lib\router\index.js:271:10)
at serveStatic (C:\Users\snowr\Documents\dev\zorgkaartnl\zorgkaartnl\node_modules\express\node_modules\serve-static\index.js:75:16)
at Layer.handle [as handle_request] (C:\Users\snowr\Documents\dev\zorgkaartnl\zorgkaartnl\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (C:\Users\snowr\Documents\dev\zorgkaartnl\zorgkaartnl\node_modules\express\lib\router\index.js:312:13)
at C:\Users\snowr\Documents\dev\zorgkaartnl\zorgkaartnl\node_modules\express\lib\router\index.js:280:7
at Function.process_params (C:\Users\snowr\Documents\dev\zorgkaartnl\zorgkaartnl\node_modules\express\lib\router\index.js:330:12)
at next (C:\Users\snowr\Documents\dev\zorgkaartnl\zorgkaartnl\node_modules\express\lib\router\index.js:271:10)
this is my app.js: 这是我的app.js:
// require modules
const express = require('express');
const app = express();
const pug = require('pug');
const fs = require('fs')
const bodyParser = require('body-parser');
const pg = require('pg');
const nodemailer = require('nodemailer');
const xoauth2 = require('xoauth2');
//set view engine and views
app.set('views', 'src/views');
app.set('view engine', 'pug');
app.use(bodyParser.urlencoded({extended: false}));
app.use(express.static('./resources/'));
//display index page
app.get('/', function ( req, res ){
console.log('Index is displayed on localhost');
res.render('index');
});
app.post('/zorginstelling/ziekenhuis-olvg-locatie-west-voorheen-sint-lucas-andreas-ziekenhuis-amsterdam-109428/waardeer', function (req, res) {
var mailOpts, smtpTrans;
console.log('form word gepost')
//Setup Nodemailer transport, I chose gmail.
smtpTrans = nodemailer.createTransport('SMTP', {
service: 'Gmail',
auth: {
xoauth2: xoauth2.createXOAuth2Generator({
user: 'kylevantil14@gmail.com',
clientId: '-' ,
clientSecret: '-' ,
refreshToken: '-'
})
}
});
//Mail options
mailOpts = {
from: req.body.rating[name] + ' <' + req.body.rating[email][first] + '>',
to: 'kylevantil14@gmail.com',
subject: 'Test',
text: req.body.rating[comment] + req.body.rating[questions][behandeling] + req.body.rating[name]
};
smtpTrans.sendMail(mailOpts, function (error, response) {
//Email not sent
if (error) {
console.log('There was a problem')
}
//Yay!! Email sent
else {
console.log('Email sent!')
}
});
});
var server = app.listen(4000, function () {
console.log('Example app listening on port: ' + server.address().port);
});
The nodemailer has been reworked, so old code structure could throw such error. 节点编辑器已经过重新设计,因此旧的代码结构可能会抛出这样的错误。 Try use the following structure: 尝试使用以下结构:
var xoauth2 = require('xoauth2');
smtpTrans = nodemailer.createTransport({
service: 'Gmail',
auth: {
xoauth2: xoauth2.createXOAuth2Generator({
user: 'kylevantil14@gmail.com',
//and other stuff
Please, check official resource for more details: 请查看官方资源了解更多详情:
https://community.nodemailer.com/2-0-0-beta/using-oauth2/ https://community.nodemailer.com/2-0-0-beta/using-oauth2/
The current problem Today for help is the change of information faster, and Alexander was good when he said "nodemailer is reworked"; 当前的问题今天寻求帮助的是信息的更快变换,亚历山大在说“节点制作者被重做”时表现得很好;
i use this easy line to define the transporter: 我使用这个简单的行来定义转运者:
var smtpTransport = nodemailer.createTransport("smtps://youruser%40gmail.com:"+encodeURIComponent('yourpass#123') + "@smtp.gmail.com:465");
i got it from here https://community.nodemailer.com/ 我从这里得到它https://community.nodemailer.com/
I had similar problem. 我有类似的问题。 'nodemailer-smtp-transport' module solved it 'nodemailer-smtp-transport'模块解决了它
var nodemailer = require('nodemailer');
var smtpTransport = require('nodemailer-smtp-transport');
var transport = nodemailer.createTransport(smtpTransport({
service: 'Gmail',
auth:{
xoauth2: xoauth2.createXOAuth2Generator({
user: 'abc@gmail.com',
//and other stuff
install nodemailer-smtp-transport by 'npm install nodemailer-smtp-transport' 通过'npm install nodemailer-smtp-transport'安装nodemailer-smtp-transport
This is working for me- 这对我有用 -
sendEmail: function(account, params) {
// create reusable transporter object using the default SMTP transport
var smtpTransport = nodemailer.createTransport({
service: 'Gmail', // sets automatically host, port and connection security settings
auth: {
user: account.user,
pass: account.password
}
});
var toEmail = params.to[0];
for (var i = 1; i < params.to.length; i++) {
toEmail += ', ' + params.to[i];
}
// setup email data with unicode symbols
var mailOptions = {
from: params.from, // sender address
to: toEmail, // list of receivers
subject: params.subject, // Subject line
text: params.text, // plain text body
html: params.html, // html body
attachments: params.attachments
};
// send mail with defined transport object
smtpTransport.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log('Error while sending mail: ' + error);
} else {
console.log('Message sent: %s', info.messageId);
}
smtpTransport.close(); // shut down the connection pool, no more messages.
});
}
Do Note that you will have to turn on less secure apps from google. 请注意,您必须从谷歌打开不太安全的应用程序。 Visit https://myaccount.google.com/lesssecureapps . 访问https://myaccount.google.com/lesssecureapps 。 Once turned on, refresh the page just to make sure that it actually got turned on (I got it turned on in 4 attempts). 一旦打开,刷新页面只是为了确保它实际上已打开(我在4次尝试中打开它)。
Try below: 试试以下:
smtpTrans = nodemailer.createTransport({
service: 'Gmail',
auth: {
type: 'OAuth2',
user: 'kylevantil14@gmail.com',
// and other stuff
See: https://nodemailer.com/smtp/oauth2/ 请参阅: https : //nodemailer.com/smtp/oauth2/
This worked for me please try it 这对我有用,请试一试
function sendEmailTechnician() {
// Generate test SMTP service account from gmail
nodemailer.createTestAccount((err, account) => {
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: '', // generated gmail user
pass: '' // generated gmail account password
}
});
// setup email data with unicode symbols
let mailOptions = {
from: 'text <user@gmail.com>', // sender address
to: 'user@gmail.com', // list of receivers
subject: 'text', // Subject line
text: 'text', // plain text body
html: '<b>text</b>' // html body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Message sent: %s', info.messageId);
// Preview only available when sending through an Ethereal account
console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));
// Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321@example.com>
// Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
});
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.