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

Unable to use message attachments #250

Closed ghost closed 7 years ago

ghost commented 7 years ago

Hi there,

I'm working on upgrading to v3, and have everything working except message attachments. Given an attachments array on the message object that looks something like:

[{
    filename: 'filename.png',
    path: 'absolute/path/to/filename.png',
    content: createReadStream('absolute/path/to/filename.png'),
    cid: 'EmbeddedImageCid'
}]

And a message object that looks something like:

const message = {
    from: 'me@example.com',
    to: 'you@example.com',
    subject: 'Email!',
    attachments
}

With the above, Email.send returns undefined. If I remove the message.attachments property, everything works great. Ahhhh, what am I missing?!

Update

If I manually call email.render and add my attachments to the message object after rendering, I can at least see that SOMETHING is happening.

const message = {
    from: 'me@example.com',
    to: 'you@example.com',
    subject: 'Email!'
}

message.html = await email.render(`${type}/html`, locals);
message.attachments = attachments;

return await email.send({
    message,
    locals
});

The above at least produces an acknowledgement that the attachment SHOULD have been sent. The preview contains this link. It sounds like the nodemailer author fixed this in version 4.1.1, but I'm running nodemailer@4.2.0.

Update 2

I've confirmed that the issue is not with the nodemailer package. Given the above configuration of the message object, transport.sendMail(message) works as expected (attachments attached). Neither previewMail(message) nor email.send(message, locals) produce the attachments.

niftylettuce commented 7 years ago

I've reproduced your issue and implementing a patch now.

niftylettuce commented 7 years ago

Please download v3.1.0 via npm install email-templates@latest or yarn add email-templates@latest, thank you for reporting this core bug. cc @mycompassspins

ghost commented 7 years ago

@niftylettuce Thanks for looking into that! I was able to get attachments to send via email, but I had to remove the content property from each attachment. Is that expected behavior?

Also, while the attachments are rendered fine via email, they do not render in the preview. Is that also expected?

niftylettuce commented 7 years ago

@mycompassspins I'm looking into this now

niftylettuce commented 7 years ago

I don't think you need to specify content if you also specify path? https://nodemailer.com/message/attachments/

Could you write a test case that fails?

niftylettuce commented 7 years ago

also I'm fixing preview-email right now per https://github.com/nodemailer/nodemailer/issues/799, this will get released shortly

niftylettuce commented 7 years ago

@mycompassspins here's a preview for you of preview-email@0.0.3 which is now available in email-templates@3.1.4. the links for attachments are clickable and have data-uri inlining so you can test opening your attachments as well.

demo

ghost commented 6 years ago

@niftylettuce Thanks for the work! So far I have not been able to write a test that fails, but I'm still determined ;)