Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send message with nodemailer and email-templates?

I try to send email with nodemailer and email-templates. Now I have example from here example email templates. But when I check this code, I have error a promise was rejected with a non-error: [object Undefined]

Help me please. This is my code

var nodemailer = require('nodemailer');
var EmailTemplate = require('email-templates').EmailTemplate;

exports.sendOne = function () {
var templatesDir = config.templatesDir;
var template = new EmailTemplate(path.join(templatesDir, 'hello.jade'))
var transport = nodemailer.createTransport({
    service: config.service,
    auth: config.auth
});

// An example users object with formatted email function
var locals = {
    email: '[email protected]',
    name: {
        first: 'Mamma',
        last: 'Mia'
    }
}

// Send a single email
template.render(locals, function (err, results) {
    if (err) {
        return console.error(err)
    }

    transport.sendMail({
        from: 'Spicy Meatball <[email protected]>',
        to: locals.email,
        subject: 'Mangia gli spaghetti con polpette!',
        html: results.html,
        text: results.text
    }, function (err, responseStatus) {
        if (err) {
            return console.error(err)
        }
        console.log(responseStatus.message)
    })
})

}

My error :

Warning: a promise was rejected with a non-error: [object Undefined]
at /home/project/node_modules/email-templates/lib/util.js:31:39
at FSReqWrap.oncomplete (fs.js:82:15)
From previous event: ...

Tell me please how to fix this error? thanks!

UPDATE code

exports.sendOne = function () {
  var nodemailer = require("nodemailer");

  var transport = nodemailer.createTransport({
    service: "gmail",
    auth: {
      user: "[email protected]",
      pass: "123456",
    },
  });

  var EmailTemplate = require("email-templates").EmailTemplate;
  var path = require("path");

  var templateDir = path.join(__dirname, "templates", "hello");

  var myTemplate = new EmailTemplate(templateDir);

  var locals = {
    email: "[email protected]",
    name: {
      first: "Mamma",
      last: "Mia",
    },
  };

  myTemplate.render(locals, function (err, result) {
    // result.html
    // result.text
    if (err) {
      return console.error(err);
    }

    transport.sendMail(
      {
        from: "Spicy Meatball <[email protected]>",
        to: locals.email,
        subject: "Mangia gli spaghetti con polpette!",
        html: results.html,
        text: results.text,
      },
      function (err, responseStatus) {
        if (err) {
          return console.error(err);
        }
        console.log(responseStatus.message);
        return responseStatus; // return from status or as you need;
      }
    );
  });
};

I updated my code but now i have error { [Error: ENOENT: no such file or directory, stat '/path-to-my-project/templates/hello''] errno: -2, code: 'ENOENT', syscall: 'stat', path: '/path-to-my-project/templates/hello' }

like image 768
ennet Avatar asked Mar 30 '16 14:03

ennet


1 Answers

I guess template rendering issue and you should return something from function (err, responseStatus){} for success

Here I assume hello.jade in templates folder and templates folder in root directory and ensure jade is using as template engine

can try it

var EmailTemplate = require('email-templates').EmailTemplate;
var path = require('path');

var templateDir = path.join(__dirname, 'templates', 'hello');

var myTemplate = new EmailTemplate(templateDir);
var locals = {
      email: '[email protected]',
      name: {
           first: 'Mamma',
           last: 'Mia'
      }
 }
myTemplate .render(locals , function (err, result) {
  // result.html 
  // result.text 
    if (err) {
       return console.error(err)
    }
    // check here what is showing in your result
    transport.sendMail({
        from: 'Spicy Meatball <[email protected]>',
        to: locals.email,
        subject: 'Mangia gli spaghetti con polpette!',
        html: results.html,
        text: results.text
     }, function (err, responseStatus) {
        if (err) {
            return console.error(err)
        }
        console.log(responseStatus.message)
        return responseStatus;// return from status or as you need;
    })
})

Updated: As far I guess it's not nodemailer related issue it's may be template rendering issue. can check by directory or by html page.

like image 167
Shaishab Roy Avatar answered Oct 18 '22 16:10

Shaishab Roy