forwardemail / email-templates

Create, preview (browser/iOS Simulator), and send custom email templates for Node.js. Made for @forwardemail, @ladjs, @cabinjs, @spamscanner, and @breejs.
https://forwardemail.net/docs/send-emails-with-node-js-javascript
MIT License
3.67k stars 337 forks source link

Question: Including juiceResources config if simply using render #251

Closed nwkeeley closed 7 years ago

nwkeeley commented 7 years ago

Hi,

Upgrading from v2 to v3

If I am only using the email.render() method to pass rendered HTML to my SendGrid module will I not be able to use juiceResources?

I have my .ejs and .css in the same folder and it doesn't seem to be inlining the styles like described here: https://github.com/niftylettuce/email-templates#automatic-inline-css-via-stylesheets

/path/to/template/myTemplate.ejs
/path/to/template/style.css
const emailer = new Email({
  views: { 
    root: '/path/to/template',
    options: {
      extension: 'ejs'
    }
  },
  transport: {
    jsonTransport: true
  },
  htmlToText: false,
  juice: true,
  juiceResources: {
    preserveImportant: true,
    webResources: {
      relativeTo: '/path/to/template'
    }
  }
});

const temp = email.render('myTemplate', {my: 'param'}).then(function(result){
 // pass result to sendgrid and send mail
});

in my .ejs file I have

<!doctype html>
<html>
  <head>
    <link rel="stylesheet" href="style.css" data-inline="data-inline">
  </head>
  <body>
niftylettuce commented 7 years ago

Writing a test for this now to see what's wrong.

niftylettuce commented 7 years ago

@nwkeeley please download v3.1.3 via npm install email-templates@latest or yarn add email-templates@latest. the email.render function now uses juiceResources (previously only email.send did). there is also an exposed email.juiceResources function you can use if you want to write a custom render function. see the docs at https://github.com/niftylettuce/email-templates#custom-rendering-eg-from-a-mongodb-database if necessary. lmk of any questions, and thanks for filing the issue.

niftylettuce commented 7 years ago

@nwkeeley also note that previewEmail does not parse attachments array like Nodemailer does.

This means that if you're not using email.send, and you want preview-email to work, you'll need to use it like this:

const previewEmail = require('preview-email');
const nodemailer = require('nodemailer');

const transport = nodemailer.createTransport({
  jsonTransport: true
});

// <https://nodemailer.com/message/>
const message = {
  from: 'niftylettuce+from@gmail.com',
  to: 'niftylettuce+to@gmail.com',
  subject: 'Hello world',
  html: '<p>Hello world</p>',
  text: 'Hello world',
  attachments: [ { filename: 'hello-world.txt', content: 'Hello world' } ]
};

transport.sendMail(message).then(res => {
  message = JSON.parse(res.message);
  previewEmail(message).then(console.log).catch(console.error);
}).catch(console.error);

I've updated preview-email README at https://github.com/niftylettuce/preview-email to note of this:

nwkeeley commented 6 years ago

@niftylettuce Awesome - thank you for the quick turn around - great module and great support!