代码之家  ›  专栏  ›  技术社区  ›  Ragnar

NodeEmailer-发送但不接收电子邮件

  •  8
  • Ragnar  · 技术社区  · 7 年前

    这封邮件似乎已发送,但我什么也没有收到。

    const nodemailer = require('nodemailer');
    const transporter = nodemailer.createTransport({
      host: 'smtp.office365.com',
      port: 587,
      secure: false, // secure:true for port 465, secure:false for port 587
      auth: {
        user: 'gil.felot@myaccount.com',
        pass: 'MyPassWord'
      }
    });
    
    // Check the connection to the service.
    transporter.verify(function(error, success) {
      if (error) {
        console.log(error);
      } else {
        console.log('Server is ready to take our messages');
      }
    });
    

    然后在我的钩子里

    hook => {
        const file = hook.params.file;
    
        const email = {
          from: 'gil.felot@myaccount.com', // sender address
          to: 'mygmailaccount@gmail.com', // list of receivers
          subject: 'Test Nodemailer', // Subject line
          // text: req.body.text, // plaintext body
          html: '<b>Hello world 🐴</b>', // html body
          attachments: [
            {
              filename: file.originalname,
              contents: new Buffer(file.buffer, 'base64'),
              encoding: 'base64'
            }
          ]
        };
    
        return hook.app.service('mail').create(email)
          .then(function (result) {
            console.log('Sent email', result);
          }).catch(err => {
            console.log(err);
          });
      }
    

    然后我得到了

    对象{来自:”gil.felot@myaccount.com,收件人:mygmailaccount@gmail.com,主题:“Test NodeEmailer”,html: 你好,世界 "}

    我不知道如何检查问题来自哪里。

    3 回复  |  直到 4 年前
        1
  •  13
  •   Black Mamba    6 年前

    当我能够通过谷歌smtp发送邮件时,我在创建邮件时缺少了“发件人”部分,但我自己的smtp由于上述配置而失败

    与谷歌合作

    var mailOptions = { to: email, subject: 'Forgot Password', html: mailData };
    

    var mailOptions = { from: 'serverName.com', to: email, subject: 'Forgot Password', html: mailData };
    

    在定义时考虑添加名称 nodemailer

    let transporter = nodemailer.createTransport({
    name: 'example.com' // <= Add this
    host: 'smtp.example.email',
    port: 587,
    
        2
  •  1
  •   Ragnar    7 年前

    好吧,我想出来了!

    我需要添加 transporter.sendMail() 内部 mail.class.js hook.app.service('mail').create(email)

        3
  •  0
  •   james ace    3 年前

    对我来说,这是我意识到的;如果html没有html标记,则不会发送电子邮件。即

    此模板将起作用

    <html>
     <body>
      Hello and welcome
     </body>
    </html>
    

    此模板不起作用:

     <body>
        Hello and welcome
     </body>
    

    Nodemailer doesn't send emails to outlook.office365 accounts