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.66k stars 339 forks source link

Email Preview: Promise not resolved when using a stream as attachment #392

Closed sgaestel closed 4 years ago

sgaestel commented 4 years ago

Hi.

Using version 7.0.5, if I use a stream for my email content, the promise for sending the email is never getting resolved.

Short example:

const Email = require('email-templates');
const fs = require('fs');
const path = require('path');

const email = new Email({
  message: {
    from: 'sgaestel@email.com',
    to: 'sgaestel@email.com',
    attachments: [
      {
        filename: 'report.xlsx',
        // content: 'test',
        content: fs.createReadStream('/myPath/report.xlsx'),
      },
    ],
  },
  transport: {
    jsonTransport: true,
  },
  views: {
    options: {
      extension: 'ejs',
    },
  },
});

email
  .send({
    template: path.join(__dirname, '..', '..', 'resources', 'emails', 'test'),
  })
  .then(() => {
    console.log('Sent !');
  }).catch((e) => {
    console.error(e)
  });

Switching the content to a string, the log is correctly printed.

niftylettuce commented 4 years ago

You are using the jsonTransport method. You cannot use this method with streams.

Instead, you should follow how I made preview-email to support streams.

https://github.com/forwardemail/preview-email/blob/bfe4741b90fefd77a286f989a90849b4c55045bd/src/index.js#L16-L17

sanchezweezer commented 3 years ago

Have same issue, transport do not help....

sanchezweezer commented 3 years ago

If I send message from nodemailer - promise resolves, but under email-templates - nope

sanchezweezer commented 3 years ago

I got it - https://github.com/forwardemail/email-templates/blob/c43914f2a9db7b5bfb44b28435f0b15dbb49ef06/src/index.js#L350-L351

this lines change transport, so... we can't not sent message and get stream attachments

sanchezweezer commented 3 years ago

solution: set send to true + change transport