mirror of
https://github.com/1f349/tulip.git
synced 2024-11-12 14:51:32 +00:00
Add html and custom mail templates
This commit is contained in:
parent
34f0950851
commit
e45fdfac6a
41
mail/mail.go
41
mail/mail.go
@ -2,7 +2,6 @@ package mail
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/emersion/go-message"
|
||||
"github.com/emersion/go-message/mail"
|
||||
"github.com/emersion/go-sasl"
|
||||
"github.com/emersion/go-smtp"
|
||||
@ -47,33 +46,45 @@ func (m *Mail) mailCall(to []string, r io.Reader) error {
|
||||
return smtp.SendMail(m.Server, m.loginInfo(), m.From.String(), to, r)
|
||||
}
|
||||
|
||||
func (m *Mail) genHeaders(subject string, to []*mail.Address, htmlBody bool) mail.Header {
|
||||
func (m *Mail) SendMail(subject string, to []*mail.Address, htmlBody, textBody io.Reader) error {
|
||||
// generate the email in this template
|
||||
buf := new(bytes.Buffer)
|
||||
|
||||
// setup mail headers
|
||||
var h mail.Header
|
||||
h.SetDate(time.Now())
|
||||
h.SetSubject(subject)
|
||||
h.SetAddressList("From", []*mail.Address{m.From.Address})
|
||||
h.SetAddressList("To", to)
|
||||
h.Set("Content-Type", "multipart/alternative")
|
||||
|
||||
if htmlBody {
|
||||
h.Set("Content-Type", "text/html; charset=utf-8")
|
||||
} else {
|
||||
h.Set("Content-Type", "text/plain; charset=utf-8")
|
||||
}
|
||||
return h
|
||||
}
|
||||
// setup html and text alternative headers
|
||||
var hHtml, hTxt mail.InlineHeader
|
||||
hHtml.Set("Content-Type", "text/html; charset=utf-8")
|
||||
hTxt.Set("Content-Type", "text/plain; charset=utf-8")
|
||||
|
||||
func (m *Mail) SendMail(subject string, to []*mail.Address, htmlBody bool, body io.Reader) error {
|
||||
// generate the email in this template
|
||||
buf := new(bytes.Buffer)
|
||||
h := m.genHeaders(subject, to, htmlBody)
|
||||
entity, err := message.New(h.Header, body)
|
||||
createWriter, err := mail.CreateWriter(buf, h)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = entity.WriteTo(buf)
|
||||
inline, err := createWriter.CreateInline()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
partHtml, err := inline.CreatePart(hHtml)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := io.Copy(partHtml, htmlBody); err != nil {
|
||||
return err
|
||||
}
|
||||
partTxt, err := inline.CreatePart(hTxt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := io.Copy(partTxt, textBody); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// convert all to addresses to strings
|
||||
toStr := make([]string, len(to))
|
||||
|
@ -8,11 +8,11 @@ import (
|
||||
)
|
||||
|
||||
func (m *Mail) SendEmailTemplate(templateName, subject, nameOfUser string, to *mail.Address, data map[string]any) error {
|
||||
buf := new(bytes.Buffer)
|
||||
templates.RenderMailTemplate(buf, templateName, map[string]any{
|
||||
var bufHtml, bufTxt bytes.Buffer
|
||||
templates.RenderMailTemplate(&bufHtml, &bufTxt, templateName, map[string]any{
|
||||
"ServiceName": m.Name,
|
||||
"Name": nameOfUser,
|
||||
"Data": data,
|
||||
})
|
||||
return m.SendMail(fmt.Sprintf("%s - %s", subject, m.Name), []*mail.Address{to}, false, buf)
|
||||
return m.SendMail(fmt.Sprintf("%s - %s", subject, m.Name), []*mail.Address{to}, &bufHtml, &bufTxt)
|
||||
}
|
||||
|
10
mail/templates/mail-account-delete.go.html
Normal file
10
mail/templates/mail-account-delete.go.html
Normal file
@ -0,0 +1,10 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<body>
|
||||
<p>Hello, {{.Name}}</p>
|
||||
<p>Your account with {{.ServiceName}} has been disabled and marked for deletion.</p>
|
||||
<p>Your account will be fully deleted within 48-hours.</p>
|
||||
<p>You will no longer receive emails from {{.ServiceName}}, unless your email address is used to set up an account.</p>
|
||||
<p>Regards,<br/>{{.ServiceName}}</p>
|
||||
</body>
|
||||
</html>
|
11
mail/templates/mail-register-admin.go.html
Normal file
11
mail/templates/mail-register-admin.go.html
Normal file
@ -0,0 +1,11 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<body>
|
||||
<p>Hello, {{.Name}}</p>
|
||||
<p>Your email address has been registered with {{.ServiceName}} by an administrator.</p>
|
||||
<p>Please open this link to verify your email address and <a href="{{.Data.RegisterUrl}}" target="_blank">register your account</a></p>
|
||||
<p>If you did not wish to register for {{.ServiceName}}, then your email has probably been used by mistake.</p>
|
||||
<p>If the link above is not used to register an account, then no further contact will be made from {{.ServiceName}} and your email address will be deleted from our systems within a 48-hour period.</p>
|
||||
<p>Regards,<br/>{{.ServiceName}}</p>
|
||||
</body>
|
||||
</html>
|
9
mail/templates/mail-reset-password.go.html
Normal file
9
mail/templates/mail-reset-password.go.html
Normal file
@ -0,0 +1,9 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<body>
|
||||
<p>Hello, {{.Name}}</p>
|
||||
<p>Please open this link to <a href="{{.Data.ResetUrl}}" target="_blank">reset your password</a></p>
|
||||
<p>This link is valid for 10 minutes.</p>
|
||||
<p>Regards,<br/>{{.ServiceName}}</p>
|
||||
</body>
|
||||
</html>
|
@ -1,12 +1,10 @@
|
||||
<!-- TODO: format this as HTML -->
|
||||
|
||||
Hello, {{.Name}}
|
||||
|
||||
Please open this link to verify your email address: {{.Data.VerifyUrl}}
|
||||
|
||||
This link is valid for 10 minutes.
|
||||
|
||||
If you did not create an account with {{.ServiceName}} then please ignore this email and the account will be deleted within a 48-hour period.
|
||||
|
||||
Regards,
|
||||
{{.ServiceName}}
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<body>
|
||||
<p>Hello, {{.Name}}</p>
|
||||
<p>Please open this link to <a href="{{.Data.VerifyUrl}}" target="_blank">verify your email address</a></p>
|
||||
<p>This link is valid for 10 minutes.</p>
|
||||
<p>If you did not create an account with {{.ServiceName}} then please ignore this email and the account will be deleted within a 48-hour period.</p>
|
||||
<p>Regards,<br/>{{.ServiceName}}</p>
|
||||
</body>
|
||||
</html>
|
||||
|
Loading…
Reference in New Issue
Block a user