1
0

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
4 changed files with 16 additions and 5 deletions

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,
},
}