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

Rag*_*nar 3 email nodemailer feathersjs

我正在用feathersjs 构建一个API,我需要发送一封带有附件的电子邮件。

电子邮件似乎已发送,但我什么也没收到。

在我的 mail.service.js

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');
  }
});
Run Code Online (Sandbox Code Playgroud)

然后在我的钩子里

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);
      });
  }
Run Code Online (Sandbox Code Playgroud)

然后我得到了

服务器已准备好接收我们的消息

已发送电子邮件

对象 {from: "gil.felot@myaccount.com", to: "mygmailaccount@gmail.com", subject: "Test Nodemailer", html: " Hello world "}

我不知道如何检查问题出在哪里。

Bla*_*mba 10

我在创建邮件时遗漏了 from 部分,而我可以通过 google smtp 发送邮件,但我自己的 smtp 因上述配置而失败

正在与谷歌合作

var mailOptions = { to: email, subject: 'Forgot Password', html: mailData };
Run Code Online (Sandbox Code Playgroud)

也使用我的 smtp:

var mailOptions = { from: 'serverName.com', to: email, subject: 'Forgot Password', html: mailData };
Run Code Online (Sandbox Code Playgroud)

考虑在定义nodemailer配置时添加名称

let transporter = nodemailer.createTransport({
name: 'example.com' // <= Add this
host: 'smtp.example.email',
port: 587,
Run Code Online (Sandbox Code Playgroud)

  • 非常感谢!Nodemailer 配置中缺少“name”键是我的问题 (2认同)

Rag*_*nar 1

好吧,我明白了!

我需要在调用时添加transporter.sendMail()内部来触发此操作mail.class.jshook.app.service('mail').create(email)

工作和附件文件在邮件中为 0 字节,但在我的变量中大小合适。