I'm using nodemailer with npm package email-templates. I'm not getting email. Template is working when i set preview:true. Getting error on console Error: No recipients defined.Why this error ing? Tried many things but nodemailer is sending empty email every time. Hope you understand my issue.
Error:
Error: No recipients defined
at SMTPConnection._formatError (E:\testing\node_modules\nodemailer\lib\smtp-connection\index.js:784:19)
at SMTPConnection._setEnvelope (E:\testing\node_modules\nodemailer\lib\smtp-connection\index.js:995:34)
at SMTPConnection.send (E:\testing\node_modules\nodemailer\lib\smtp-connection\index.js:615:14)
at sendMessage (E:\testing\node_modules\nodemailer\lib\smtp-transport\index.js:227:28)
at E:\testing\node_modules\nodemailer\lib\smtp-transport\index.js:285:25
at SMTPConnection._actionAUTHComplete (E:\testing\node_modules\nodemailer\lib\smtp-connection\index.js:1537:9)
at SMTPConnection.<anonymous> (E:\testing\node_modules\nodemailer\lib\smtp-connection\index.js:550:26)
at SMTPConnection._processResponse (E:\testing\node_modules\nodemailer\lib\smtp-connection\index.js:942:20)
at SMTPConnection._onData (E:\testing\node_modules\nodemailer\lib\smtp-connection\index.js:749:14)
at TLSSocket.SMTPConnection._onSocketData (E:\testing\node_modules\nodemailer\lib\smtp-connection\index.js:195:44)
at TLSSocket.emit (events.js:210:5)
at addChunk (_stream_readable.js:309:12)
at readableAddChunk (_stream_readable.js:290:11)
at TLSSocket.Readable.push (_stream_readable.js:224:10)
at TLSWrap.onStreamRead (internal/stream_base_mons.js:182:23) {
code: 'EENVELOPE',
mand: 'API'
}
My directory structure:
├── app.js
└── emails
└── forget-password
├── html.pug
├── subject.pug
Node mailer with template:
const nodemailer = require('nodemailer');
var generator = require('generate-password');
const Email = require('email-templates');
exports.sendNodeForgotPasswordMail = function (email, GeneratePassword) {
var transporter = nodemailer.createTransport({
host: 'smtp.gmail',
port: 587,
secure: false,
auth: {
user: 'my_email',
pass: 'my_password'
},
tls: {
rejectUnauthorized: false
}
});
const emailVar = new Email({
message: {
from: '[email protected]'
},
preview: true,
send: true,
transport: {
jsonTransport: true
}
});
emailVar.send({
template: 'forget-password',
message: {
to: email
},
locals: {
password: GeneratePassword
}
}) .then(res => {
console.log('res.originalMessage', res.originalMessage)
}).catch(console.error);
return transporter.sendMail(emailVar, function (err, info) {
if (err)
console.log(err);
else
console.log(info);
});
};
Html.pug:
p Your new password is: #{password}
subject.pug
= `Password reset request`
I'm using nodemailer with npm package email-templates. I'm not getting email. Template is working when i set preview:true. Getting error on console Error: No recipients defined.Why this error ing? Tried many things but nodemailer is sending empty email every time. Hope you understand my issue.
Error:
Error: No recipients defined
at SMTPConnection._formatError (E:\testing\node_modules\nodemailer\lib\smtp-connection\index.js:784:19)
at SMTPConnection._setEnvelope (E:\testing\node_modules\nodemailer\lib\smtp-connection\index.js:995:34)
at SMTPConnection.send (E:\testing\node_modules\nodemailer\lib\smtp-connection\index.js:615:14)
at sendMessage (E:\testing\node_modules\nodemailer\lib\smtp-transport\index.js:227:28)
at E:\testing\node_modules\nodemailer\lib\smtp-transport\index.js:285:25
at SMTPConnection._actionAUTHComplete (E:\testing\node_modules\nodemailer\lib\smtp-connection\index.js:1537:9)
at SMTPConnection.<anonymous> (E:\testing\node_modules\nodemailer\lib\smtp-connection\index.js:550:26)
at SMTPConnection._processResponse (E:\testing\node_modules\nodemailer\lib\smtp-connection\index.js:942:20)
at SMTPConnection._onData (E:\testing\node_modules\nodemailer\lib\smtp-connection\index.js:749:14)
at TLSSocket.SMTPConnection._onSocketData (E:\testing\node_modules\nodemailer\lib\smtp-connection\index.js:195:44)
at TLSSocket.emit (events.js:210:5)
at addChunk (_stream_readable.js:309:12)
at readableAddChunk (_stream_readable.js:290:11)
at TLSSocket.Readable.push (_stream_readable.js:224:10)
at TLSWrap.onStreamRead (internal/stream_base_mons.js:182:23) {
code: 'EENVELOPE',
mand: 'API'
}
My directory structure:
├── app.js
└── emails
└── forget-password
├── html.pug
├── subject.pug
Node mailer with template:
const nodemailer = require('nodemailer');
var generator = require('generate-password');
const Email = require('email-templates');
exports.sendNodeForgotPasswordMail = function (email, GeneratePassword) {
var transporter = nodemailer.createTransport({
host: 'smtp.gmail.',
port: 587,
secure: false,
auth: {
user: 'my_email',
pass: 'my_password'
},
tls: {
rejectUnauthorized: false
}
});
const emailVar = new Email({
message: {
from: '[email protected]'
},
preview: true,
send: true,
transport: {
jsonTransport: true
}
});
emailVar.send({
template: 'forget-password',
message: {
to: email
},
locals: {
password: GeneratePassword
}
}) .then(res => {
console.log('res.originalMessage', res.originalMessage)
}).catch(console.error);
return transporter.sendMail(emailVar, function (err, info) {
if (err)
console.log(err);
else
console.log(info);
});
};
Html.pug:
p Your new password is: #{password}
subject.pug
= `Password reset request`
Share
Improve this question
asked Jan 3, 2020 at 10:48
user12561026user12561026
7
- Message.from is defined but not "to" as error says you have not defined where you .sendMail to. – Zydnar Commented Jan 3, 2020 at 10:55
- I tried it. Still not working. – user12561026 Commented Jan 3, 2020 at 11:13
-
Check if
email
var is defined. – Zydnar Commented Jan 3, 2020 at 11:26 - Look at the example in documentation and see what is missing: nodemailer./about your code looks quite different – Zydnar Commented Jan 3, 2020 at 11:28
- @Zydnar var email is defined and this documentation is for only nodemailer not for email-templates. – user12561026 Commented Jan 3, 2020 at 12:21
3 Answers
Reset to default 3If anyone else is looking how to solve this, i manage to make it work based on @Irving Caarmal answer and the code is the following:
function sendTokenEmail(userEmail, token, templateName) {
const email = new Email({
message: {
from: '[email protected]',
},
preview: true,
send: true,
transport: {
host: process.env.MAIL_HOST,
port: process.env.MAIL_PORT,
auth: {
user: process.env.MAIL_USER,
pass: process.env.MAIL_PASS,
},
},
})
return email
.send({
template: templateName,
message: {
to: userEmail,
},
locals: {
token,
},
})
.then((res) => {
console.log('res.originalMessage', res.originalMessage)
})
}
the reason is that you dont need to require nodemailer instead of make your transporter with nodemailer you need to pass it in JSON like so.
exports.sendNodeForgotPasswordMail = function (email, GeneratePassword) {
var transporter = nodemailer.createTransport({
host: 'smtp.gmail.',
port: 587,
secure: false,
auth: {
user: 'my_email',
pass: 'my_password'
},
tls: {
rejectUnauthorized: false
}
});
bees:
transport: {
host: 'smtp.gmail.',
port: 587,
secure: false,
auth: {
user: 'my_email',
pass: 'my_password'
},
tls: {
rejectUnauthorized: false
}
}
const Email = require('email-templates'); <-- with this is enough.
if anyone has an issue with getting on their email address the mail generated you should add at the level with send: true
also the option preview: false
(PS: this worked for me) without having the preview
set to false
the email would not e even if the docs say:
If you want to send emails in development or test environments, set options.send to true.
Example:
const email = new Email({
message: {
from: process.env.SMTP_USER,
},
send: true,
preview: false,
transport: {
host: process.env.SMTP_HOST,
auth: {
user: process.env.SMTP_USER,
pass: process.env.SMTP_PASS,
},
},
})
await email.send({
template: 'template-of-choice',
message: {
from: 'email-address'
},
locales: {}
})
Hope it helps :)