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

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

Nodemailer with email-templates not sending email

问题

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

错误信息:

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

我的目录结构:

├── app.js
└── emails
    └── forget-password
        ├── html.pug
        ├── subject.pug

Node mailer 与模板:

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.com',
        port: 587,
        secure: false,
        auth: {
            user: 'my_email',
            pass: 'my_password'
        },
        tls: {
            rejectUnauthorized: false
        }

    });

    const emailVar = new Email({
        message: {
            from: 'testing@gmail.com'
        },
        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 你的新密码是:#{password}

subject.pug:

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

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:

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_commons.js:182:23) {
  code: 'EENVELOPE',
  command: '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.com',
        port: 587,
        secure: false,
        auth: {
            user: 'my_email',
            pass: 'my_password'
        },
        tls: {
            rejectUnauthorized: false
        }

    });

    const emailVar = new Email({
        message: {
            from: 'testing@gmail.com'
        },
        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`

答案1

得分: 2

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

exports.sendNodeForgotPasswordMail = function (email, GeneratePassword) {
    var transporter = nodemailer.createTransport({
        host: 'smtp.gmail.com',
        port: 587,
        secure: false,
        auth: {
            user: 'my_email',
            pass: 'my_password'
        },
        tls: {
            rejectUnauthorized: false
        }
    });

变成:

transport: {
    host: 'smtp.gmail.com',
    port: 587,
    secure: false,
    auth: {
        user: 'my_email',
        pass: 'my_password'
    },
    tls: {
        rejectUnauthorized: false
    }
}

只需使用以下代码即可:

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.

exports.sendNodeForgotPasswordMail = function (email, GeneratePassword) {
    var transporter = nodemailer.createTransport({
        host: 'smtp.gmail.com',
        port: 587,
        secure: false,
        auth: {
            user: 'my_email',
            pass: 'my_password'
        },
        tls: {
            rejectUnauthorized: false
        }

    });

becomes:

transport: {
        host: 'smtp.gmail.com',
        port: 587,
        secure: false,
        auth: {
            user: 'my_email',
            pass: 'my_password'
        },
        tls: {
            rejectUnauthorized: false
        }

    }

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

答案2

得分: 2

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

function sendTokenEmail(userEmail, token, templateName) {
  const email = new Email({
    message: {
      from: 'someemail@gmail.com',
    },
    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)
    })
}
英文:

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:

function sendTokenEmail(userEmail, token, templateName) {
  const email = new Email({
    message: {
      from: &#39;someemail@gmail.com&#39;,
    },
    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) =&gt; {
      console.log(&#39;res.originalMessage&#39;, res.originalMessage)
    })
   }


</details>



# 答案3
**得分**: 1

如果有人在获取生成的邮件时遇到问题,您应该在级别上添加`send: true`以及选项`preview: false`(PS:这对我有效),否则即使文档说:

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


**示例:**

```javascript
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: {},
})

希望对您有所帮助 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:

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: &#39;template-of-choice&#39;, 
  message: {
    from: &#39;email-address&#39;
  },
  locales: {}
})

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:

确定