Add mail render method

This commit is contained in:
kolaente 2021-02-07 15:45:23 +01:00
parent b6205de08b
commit bdccab1419
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
4 changed files with 97 additions and 2 deletions

View File

@ -30,6 +30,7 @@ import (
// Opts holds infos for a mail
type Opts struct {
From string
To string
Subject string
Message string
@ -81,7 +82,10 @@ func SendTestMail(to string) error {
// SendMail puts a mail in the queue
func SendMail(opts *Opts) {
m := gomail.NewMessage()
m.SetHeader("From", config.MailerFromEmail.GetString())
if opts.From == "" {
opts.From = config.MailerFromEmail.GetString()
}
m.SetHeader("From", opts.From)
m.SetHeader("To", opts.To)
m.SetHeader("Subject", opts.Subject)
for _, h := range opts.Headers {

View File

@ -16,6 +16,14 @@
package notifications
import (
"bytes"
"code.vikunja.io/api/pkg/mail"
"code.vikunja.io/api/pkg/static"
"code.vikunja.io/api/pkg/utils"
"github.com/shurcooL/httpfs/html/vfstemplate"
)
type Mail struct {
from string
to string
@ -70,6 +78,44 @@ func (m *Mail) Line(line string) *Mail {
return m
}
func RenderMail(mail *Mail) {
func RenderMail(m *Mail) (mailOpts *mail.Opts, err error) {
var htmlContent bytes.Buffer
var plainContent bytes.Buffer
t, err := vfstemplate.ParseGlob(static.Templates, nil, "*.tmpl")
if err != nil {
return
}
boundary := "np" + utils.MakeRandomString(13)
data := make(map[string]interface{})
data["Greeting"] = m.greeting
data["IntroLines"] = m.introLines
data["OutroLines"] = m.outroLines
data["ActionText"] = m.actionText
data["ActionURL"] = m.actionURL
data["Boundary"] = boundary
if err := t.ExecuteTemplate(&htmlContent, "mail-notification.html.tmpl", data); err != nil {
return
}
if err := t.ExecuteTemplate(&plainContent, "mail-notification.plain.tmpl", data); err != nil {
return
}
mailOpts = &mail.Opts{
From: m.from,
To: m.to,
Subject: m.subject,
ContentType: mail.ContentTypeMultipart,
Message: plainContent.String(),
HTMLMessage: htmlContent.String(),
Boundary: boundary,
}
return mailOpts, nil
}

View File

@ -0,0 +1,31 @@
{{template "mail-header.tmpl" .}}
<p>
{{ .Greeting }}
</p>
{{ range $line := .IntroLines}}
<p>
{{ $line }}
</p>
{{ end }}
{{ if .ActionURL }}
<a href="{{ .ActionULR }}" title="{{ .ActionText }}"
style="-webkit-box-shadow: 0.3em 0.3em 1em #b2d0ff; box-shadow: 0.3em 0.3em 1em #b2d0ff; background-color: #1973ff; border-color: transparent; color: #fff; text-decoration: none; text-align: center; text-rendering: optimizelegibility; text-transform: uppercase; font-weight: bold; font-size: 14px; padding: 10px 14px; margin: 10px auto; border-radius: 4px; user-select: none; display: block; width: 280px;font-family:sans-serif">
{{ .ActionText }}
</a>
{{end}}
{{ range $line := .OutroLines}}
<p>
{{ $line }}
</p>
{{ end }}
{{ if .ActionURL }}
<p>
If the button above doesn't work, copy the url below and paste it in your browsers address bar:<br/>
{{ .ActionURL }}
</p>
{{ end }}
{{template "mail-footer.tmpl"}}

View File

@ -0,0 +1,14 @@
{{ .Greeting }}
{{ range $line := .IntroLines}}
{{ $line }}
{{ end }}
{{ if .ActionURL }}
{{ .ActionText }}:
{{ .ActionURL }}
{{end}}
{{ range $line := .OutroLines}}
{{ $line }}
{{ end }}