feat: use embed fs directly to embed the logo in mails

This commit is contained in:
kolaente 2022-07-07 15:54:38 +02:00
parent 25ffa1bc2e
commit 73c4c399e5
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B
4 changed files with 16 additions and 5 deletions

2
go.mod
View file

@ -58,7 +58,7 @@ require (
github.com/tkuchiki/go-timezone v0.2.2
github.com/ulule/limiter/v3 v3.10.0
github.com/vectordotdev/go-datemath v0.1.1-0.20211214182920-0a4ac8742b93
github.com/wneessen/go-mail v0.2.4
github.com/wneessen/go-mail v0.2.5
github.com/yuin/goldmark v1.4.12
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4
golang.org/x/image v0.0.0-20220302094943-723b81ca9867

2
go.sum
View file

@ -760,6 +760,8 @@ github.com/vectordotdev/go-datemath v0.1.1-0.20211214182920-0a4ac8742b93 h1:bT0Z
github.com/vectordotdev/go-datemath v0.1.1-0.20211214182920-0a4ac8742b93/go.mod h1:PnwzbSst7KD3vpBzzlntZU5gjVa455Uqa5QPiKSYJzQ=
github.com/wneessen/go-mail v0.2.4 h1:fgSyXjNRleT3p7d7iXsaoW/LZmksNovS2Bt0MNFR4JQ=
github.com/wneessen/go-mail v0.2.4/go.mod h1:5JFKEGsldD2Fh5O+Lxzhre3GLOcJznWrzqZaJwTs300=
github.com/wneessen/go-mail v0.2.5 h1:lVQ5Q1hYaUNU/VL9F4AOtYaBAxgmrH+6W6wwMcDpKDE=
github.com/wneessen/go-mail v0.2.5/go.mod h1:m25lkU2GYQnlVr6tdwK533/UXxo57V0kLOjaFYmub0E=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=

View file

@ -17,6 +17,7 @@
package mail
import (
"embed"
"io"
"code.vikunja.io/api/pkg/config"
@ -37,6 +38,7 @@ type Opts struct {
Boundary string
Headers []*header
Embeds map[string]io.Reader
EmbedFS map[string]*embed.FS
}
// ContentType represents mail content types
@ -90,6 +92,13 @@ func getMessage(opts *Opts) *mail.Msg {
m.EmbedReader(name, content)
}
for name, fs := range opts.EmbedFS {
err := m.EmbedFromEmbedFS(name, fs)
if err != nil {
log.Errorf("Error embedding %s via embed.FS into mail: %v", err)
}
}
switch opts.ContentType {
case ContentTypePlain:
m.SetBodyString("text/plain", opts.Message)

View file

@ -18,9 +18,9 @@ package notifications
import (
"bytes"
"embed"
_ "embed"
templatehtml "html/template"
"io"
templatetext "text/template"
"code.vikunja.io/api/pkg/config"
@ -87,7 +87,7 @@ const mailTemplateHTML = `
`
//go:embed logo.png
var logo []byte
var logo embed.FS
// RenderMail takes a precomposed mail message and renders it into a ready to send mail.Opts object
func RenderMail(m *Mail) (mailOpts *mail.Opts, err error) {
@ -160,8 +160,8 @@ func RenderMail(m *Mail) (mailOpts *mail.Opts, err error) {
Message: plainContent.String(),
HTMLMessage: htmlContent.String(),
Boundary: boundary,
Embeds: map[string]io.Reader{
"logo.png": bytes.NewBuffer(logo),
EmbedFS: map[string]*embed.FS{
"logo.png": &logo,
},
}