9

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: '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');
  }
});

Then in my hook

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);
      });
  }

then I got

Server is ready to take our messages

Sent email

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

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

Community
  • 1
  • 1
Ragnar
  • 2,550
  • 6
  • 36
  • 70

3 Answers3

16

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,
Black Mamba
  • 13,632
  • 6
  • 82
  • 105
1

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.

Ragnar
  • 2,550
  • 6
  • 36
  • 70
1

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

James Ikubi
  • 2,552
  • 25
  • 18