Nodemailer与email-templates未发送电子邮件。

huangapple go评论99阅读模式
英文:

Nodemailer with email-templates not sending email

问题

我正在使用 npm 包 email-templates 来使用 nodemailer。我无法收到电子邮件。当我设置 preview:true 时,模板可以工作。在控制台上出现错误 错误:未定义收件人。为什么会出现这个错误?我尝试了很多方法,但 nodemailer 每次都发送空电子邮件。希望你能理解我的问题。

错误信息:

  1. 错误:未定义收件人
  2. SMTPConnection._formatError (E:\testing\node_modules\nodemailer\lib\smtp-connection\index.js:784:19)
  3. SMTPConnection._setEnvelope (E:\testing\node_modules\nodemailer\lib\smtp-connection\index.js:995:34)
  4. SMTPConnection.send (E:\testing\node_modules\nodemailer\lib\smtp-connection\index.js:615:14)
  5. sendMessage (E:\testing\node_modules\nodemailer\lib\smtp-transport\index.js:227:28)
  6. E:\testing\node_modules\nodemailer\lib\smtp-transport\index.js:285:25
  7. SMTPConnection._actionAUTHComplete (E:\testing\node_modules\nodemailer\lib\smtp-connection\index.js:1537:9)
  8. SMTPConnection.<anonymous> (E:\testing\node_modules\nodemailer\lib\smtp-connection\index.js:550:26)
  9. SMTPConnection._processResponse (E:\testing\node_modules\nodemailer\lib\smtp-connection\index.js:942:20)
  10. SMTPConnection._onData (E:\testing\node_modules\nodemailer\lib\smtp-connection\index.js:749:14)
  11. TLSSocket.SMTPConnection._onSocketData (E:\testing\node_modules\nodemailer\lib\smtp-connection\index.js:195:44)
  12. TLSSocket.emit (events.js:210:5)
  13. addChunk (_stream_readable.js:309:12)
  14. readableAddChunk (_stream_readable.js:290:11)
  15. TLSSocket.Readable.push (_stream_readable.js:224:10)
  16. TLSWrap.onStreamRead (internal/stream_base_commons.js:182:23) {
  17. code: 'EENVELOPE',
  18. command: 'API'
  19. }

我的目录结构:

  1. ├── app.js
  2. └── emails
  3. └── forget-password
  4. ├── html.pug
  5. ├── subject.pug

Node mailer 与模板:

  1. const nodemailer = require('nodemailer');
  2. var generator = require('generate-password');
  3. const Email = require('email-templates');
  4. exports.sendNodeForgotPasswordMail = function (email, GeneratePassword) {
  5. var transporter = nodemailer.createTransport({
  6. host: 'smtp.gmail.com',
  7. port: 587,
  8. secure: false,
  9. auth: {
  10. user: 'my_email',
  11. pass: 'my_password'
  12. },
  13. tls: {
  14. rejectUnauthorized: false
  15. }
  16. });
  17. const emailVar = new Email({
  18. message: {
  19. from: 'testing@gmail.com'
  20. },
  21. preview: true,
  22. send: true,
  23. transport: {
  24. jsonTransport: true
  25. }
  26. });
  27. emailVar.send({
  28. template: 'forget-password',
  29. message: {
  30. to: email
  31. },
  32. locals: {
  33. password: GeneratePassword
  34. }
  35. }) .then(res => {
  36. console.log('res.originalMessage', res.originalMessage)
  37. }).catch(console.error);
  38. return transporter.sendMail(emailVar, function (err, info) {
  39. if (err)
  40. console.log(err);
  41. else
  42. console.log(info);
  43. });
  44. };

html.pug:

  1. p 你的新密码是:#{password}

subject.pug:

  1. = `重置密码请求`
英文:

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 coming? Tried many things but nodemailer is sending empty email every time. Hope you understand my issue.

Error:

  1. Error: No recipients defined
  2. at SMTPConnection._formatError (E:\testing\node_modules\nodemailer\lib\smtp-connection\index.js:784:19)
  3. at SMTPConnection._setEnvelope (E:\testing\node_modules\nodemailer\lib\smtp-connection\index.js:995:34)
  4. at SMTPConnection.send (E:\testing\node_modules\nodemailer\lib\smtp-connection\index.js:615:14)
  5. at sendMessage (E:\testing\node_modules\nodemailer\lib\smtp-transport\index.js:227:28)
  6. at E:\testing\node_modules\nodemailer\lib\smtp-transport\index.js:285:25
  7. at SMTPConnection._actionAUTHComplete (E:\testing\node_modules\nodemailer\lib\smtp-connection\index.js:1537:9)
  8. at SMTPConnection.<anonymous> (E:\testing\node_modules\nodemailer\lib\smtp-connection\index.js:550:26)
  9. at SMTPConnection._processResponse (E:\testing\node_modules\nodemailer\lib\smtp-connection\index.js:942:20)
  10. at SMTPConnection._onData (E:\testing\node_modules\nodemailer\lib\smtp-connection\index.js:749:14)
  11. at TLSSocket.SMTPConnection._onSocketData (E:\testing\node_modules\nodemailer\lib\smtp-connection\index.js:195:44)
  12. at TLSSocket.emit (events.js:210:5)
  13. at addChunk (_stream_readable.js:309:12)
  14. at readableAddChunk (_stream_readable.js:290:11)
  15. at TLSSocket.Readable.push (_stream_readable.js:224:10)
  16. at TLSWrap.onStreamRead (internal/stream_base_commons.js:182:23) {
  17. code: 'EENVELOPE',
  18. command: 'API'
  19. }

My directory structure:

  1. ├── app.js
  2. └── emails
  3. └── forget-password
  4. ├── html.pug
  5. ├── subject.pug

Node mailer with template:

  1. const nodemailer = require('nodemailer');
  2. var generator = require('generate-password');
  3. const Email = require('email-templates');
  4. exports.sendNodeForgotPasswordMail = function (email, GeneratePassword) {
  5. var transporter = nodemailer.createTransport({
  6. host: 'smtp.gmail.com',
  7. port: 587,
  8. secure: false,
  9. auth: {
  10. user: 'my_email',
  11. pass: 'my_password'
  12. },
  13. tls: {
  14. rejectUnauthorized: false
  15. }
  16. });
  17. const emailVar = new Email({
  18. message: {
  19. from: 'testing@gmail.com'
  20. },
  21. preview: true,
  22. send: true,
  23. transport: {
  24. jsonTransport: true
  25. }
  26. });
  27. emailVar.send({
  28. template: 'forget-password',
  29. message: {
  30. to: email
  31. },
  32. locals: {
  33. password: GeneratePassword
  34. }
  35. }) .then(res => {
  36. console.log('res.originalMessage', res.originalMessage)
  37. }).catch(console.error);
  38. return transporter.sendMail(emailVar, function (err, info) {
  39. if (err)
  40. console.log(err);
  41. else
  42. console.log(info);
  43. });
  44. };

Html.pug:

  1. p Your new password is: #{password}

subject.pug

  1. = `Password reset request`

答案1

得分: 2

原因是你不需要要求 nodemailer,而是需要将 transporter 传递为 JSON,像这样。

  1. exports.sendNodeForgotPasswordMail = function (email, GeneratePassword) {
  2. var transporter = nodemailer.createTransport({
  3. host: 'smtp.gmail.com',
  4. port: 587,
  5. secure: false,
  6. auth: {
  7. user: 'my_email',
  8. pass: 'my_password'
  9. },
  10. tls: {
  11. rejectUnauthorized: false
  12. }
  13. });

变成:

  1. transport: {
  2. host: 'smtp.gmail.com',
  3. port: 587,
  4. secure: false,
  5. auth: {
  6. user: 'my_email',
  7. pass: 'my_password'
  8. },
  9. tls: {
  10. rejectUnauthorized: false
  11. }
  12. }

只需使用以下代码即可:

  1. const Email = require('email-templates');
英文:

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.

  1. exports.sendNodeForgotPasswordMail = function (email, GeneratePassword) {
  2. var transporter = nodemailer.createTransport({
  3. host: 'smtp.gmail.com',
  4. port: 587,
  5. secure: false,
  6. auth: {
  7. user: 'my_email',
  8. pass: 'my_password'
  9. },
  10. tls: {
  11. rejectUnauthorized: false
  12. }
  13. });

becomes:

  1. transport: {
  2. host: 'smtp.gmail.com',
  3. port: 587,
  4. secure: false,
  5. auth: {
  6. user: 'my_email',
  7. pass: 'my_password'
  8. },
  9. tls: {
  10. rejectUnauthorized: false
  11. }
  12. }

const Email = require('email-templates'); <-- with this is enough.

答案2

得分: 2

如果有其他人在寻找如何解决这个问题,我成功地让它基于@Irving Caarmal的回答工作,代码如下:

  1. function sendTokenEmail(userEmail, token, templateName) {
  2. const email = new Email({
  3. message: {
  4. from: 'someemail@gmail.com',
  5. },
  6. preview: true,
  7. send: true,
  8. transport: {
  9. host: process.env.MAIL_HOST,
  10. port: process.env.MAIL_PORT,
  11. auth: {
  12. user: process.env.MAIL_USER,
  13. pass: process.env.MAIL_PASS,
  14. },
  15. },
  16. })
  17. return email
  18. .send({
  19. template: templateName,
  20. message: {
  21. to: userEmail,
  22. },
  23. locals: {
  24. token,
  25. },
  26. })
  27. .then((res) => {
  28. console.log('res.originalMessage', res.originalMessage)
  29. })
  30. }
英文:

If 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:

  1. function sendTokenEmail(userEmail, token, templateName) {
  2. const email = new Email({
  3. message: {
  4. from: &#39;someemail@gmail.com&#39;,
  5. },
  6. preview: true,
  7. send: true,
  8. transport: {
  9. host: process.env.MAIL_HOST,
  10. port: process.env.MAIL_PORT,
  11. auth: {
  12. user: process.env.MAIL_USER,
  13. pass: process.env.MAIL_PASS,
  14. },
  15. },
  16. })
  17. return email
  18. .send({
  19. template: templateName,
  20. message: {
  21. to: userEmail,
  22. },
  23. locals: {
  24. token,
  25. },
  26. })
  27. .then((res) =&gt; {
  28. console.log(&#39;res.originalMessage&#39;, res.originalMessage)
  29. })
  30. }
  31. </details>
  32. # 答案3
  33. **得分**: 1
  34. 如果有人在获取生成的邮件时遇到问题,您应该在级别上添加`send: true`以及选项`preview: false`PS:这对我有效),否则即使文档说:

如果您想在开发或测试环境中发送电子邮件,请将options.send设置为true。

  1. **示例:**
  2. ```javascript
  3. const email = new Email({
  4. message: {
  5. from: process.env.SMTP_USER,
  6. },
  7. send: true,
  8. preview: false,
  9. transport: {
  10. host: process.env.SMTP_HOST,
  11. auth: {
  12. user: process.env.SMTP_USER,
  13. pass: process.env.SMTP_PASS,
  14. },
  15. },
  16. })
  17. await email.send({
  18. template: 'template-of-choice',
  19. message: {
  20. from: 'email-address',
  21. },
  22. locales: {},
  23. })

希望对您有所帮助 Nodemailer与email-templates未发送电子邮件。

英文:

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 come even if the docs say:


If you want to send emails in development or test environments, set options.send to true.

Example:

  1. const email = new Email({
  2. message: {
  3. from: process.env.SMTP_USER,
  4. },
  5. send: true,
  6. preview: false,
  7. transport: {
  8. host: process.env.SMTP_HOST,
  9. auth: {
  10. user: process.env.SMTP_USER,
  11. pass: process.env.SMTP_PASS,
  12. },
  13. },
  14. })
  15. await email.send({
  16. template: &#39;template-of-choice&#39;,
  17. message: {
  18. from: &#39;email-address&#39;
  19. },
  20. locales: {}
  21. })

Hope it helps Nodemailer与email-templates未发送电子邮件。

huangapple
  • 本文由 发表于 2020年1月3日 18:48:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577204.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定