Ragnar
Ragnar

Reputation: 2690

Nodemailer - email send but not receive

I'm build an API with feathersjs and I need to send an email with an attachment.

The email seems to be send but I receive nothing.

In my 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: '[email protected]',
    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');
  }
});

Then in my hook

hook => {
    const file = hook.params.file;

    const email = {
      from: '[email protected]', // sender address
      to: '[email protected]', // 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);
      });
  }

then I got

Server is ready to take our messages

Sent email

Object {from: "[email protected]", to: "[email protected]", subject: "Test Nodemailer", html: "Hello world 🐴"}

I have no idea how to check where the problem come from.

Upvotes: 10

Views: 17021

Answers (3)

James Ikubi
James Ikubi

Reputation: 2948

For me this was what I realized; if the html does not have the html tags, then the email is not sent. i.e.

This template will work

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

This template will not work:

 <body>
    Hello and welcome
 </body>

This is especially when sending to office365, refer to this other question here: Nodemailer doesn't send emails to outlook.office365 accounts

Upvotes: 1

Black Mamba
Black Mamba

Reputation: 15595

I was missing the from part while creating a mail while I was able to send mail via google smtp but my own smtp was failing with the above configuration

Was working with google

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

Working with my smtp as well:

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

Consider adding name while defining nodemailer configuration as well

let transporter = nodemailer.createTransport({
name: 'example.com' // <= Add this
host: 'smtp.example.email',
port: 587,

Upvotes: 19

Ragnar
Ragnar

Reputation: 2690

Ok I figure it out !

I needed to add the transporter.sendMail() inside the mail.class.js to trigger this action when I call hook.app.service('mail').create(email)

Working and the attachement file that is 0 byte in the mail but the good size inside my variable.

Upvotes: 1

Related Questions