jordan-wright / email

Robust and flexible email library for Go
MIT License
2.65k stars 321 forks source link

Support for bundling multipart/related files to text/html #91

Closed JensRantil closed 4 years ago

JensRantil commented 4 years ago

I am coding up an HTML e-mail where I'd like to present attached images with the HTML and I don't want the images to presented as attachments to the e-mail. From what I understand the way to do this is to bundle the text/html part inside a multipart/related part. The multipart/related then also contains the images.

I've been googling a fair amount for some Golang library that could help with me with this to no avail. As such, I'm willing to contribute this functionality to this library. https://stackoverflow.com/a/53798744 explains what needs to be done.

The open question is how, API-wise, to signal that an attachment should be related to the text/html part and not be part of the MIME root (as it always is today). I see two alternatives:

Note, that both alternatives will require the user to manually modifying field(s) on https://godoc.org/github.com/jordan-wright/email#Email since neither https://godoc.org/github.com/jordan-wright/email#Email.AttachFile nor https://godoc.org/github.com/jordan-wright/email#Email.Attach have parameters allowing a user to specify that it's an HTML attachment. If HTML attachment should be a first-hand feature, I'd vote to deprecate/remove https://godoc.org/github.com/jordan-wright/email#Email.AttachFile and https://godoc.org/github.com/jordan-wright/email#Email.Attach and instead make them builder functions to construct *Attachment instances which then can be additionally modified and/or added to the correct field on https://godoc.org/github.com/jordan-wright/email#Email.

Thoughts? I vote for the non-backwards compatible change above together with adding htmlRelated bool to Attachment.

JensRantil commented 4 years ago

Oh, I just discovered that https://godoc.org/github.com/jordan-wright/email#Email.AttachFile and https://godoc.org/github.com/jordan-wright/email#Email.Attach returns their newly created *Attachments. I still think those functions shouldn't be bundled with the Email type, but in the spirit of remaining backwards compatible, I will then only add the boolean.

slurdge commented 4 years ago

This would be really useful to merge this PR. I'm currently developing https://github.com/slurdge/goeland and I need to use multipart/related to include a header picture for the emails. Is there anything that block the merging ?

jordan-wright commented 4 years ago

Merged. Thanks @JensRantil!