diff --git a/mail/mail.go b/mail/mail.go index aef03c1..8403664 100644 --- a/mail/mail.go +++ b/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)) diff --git a/mail/send-template.go b/mail/send-template.go index a15fdd3..89d1ca6 100644 --- a/mail/send-template.go +++ b/mail/send-template.go @@ -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) } diff --git a/mail/templates/mail-account-delete.go.html b/mail/templates/mail-account-delete.go.html new file mode 100644 index 0000000..1b795d2 --- /dev/null +++ b/mail/templates/mail-account-delete.go.html @@ -0,0 +1,10 @@ + + + +

Hello, {{.Name}}

+

Your account with {{.ServiceName}} has been disabled and marked for deletion.

+

Your account will be fully deleted within 48-hours.

+

You will no longer receive emails from {{.ServiceName}}, unless your email address is used to set up an account.

+

Regards,
{{.ServiceName}}

+ + diff --git a/mail/templates/mail-register-admin.go.html b/mail/templates/mail-register-admin.go.html new file mode 100644 index 0000000..62480d0 --- /dev/null +++ b/mail/templates/mail-register-admin.go.html @@ -0,0 +1,11 @@ + + + +

Hello, {{.Name}}

+

Your email address has been registered with {{.ServiceName}} by an administrator.

+

Please open this link to verify your email address and register your account

+

If you did not wish to register for {{.ServiceName}}, then your email has probably been used by mistake.

+

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.

+

Regards,
{{.ServiceName}}

+ + diff --git a/mail/templates/mail-reset-password.go.html b/mail/templates/mail-reset-password.go.html new file mode 100644 index 0000000..b511045 --- /dev/null +++ b/mail/templates/mail-reset-password.go.html @@ -0,0 +1,9 @@ + + + +

Hello, {{.Name}}

+

Please open this link to reset your password

+

This link is valid for 10 minutes.

+

Regards,
{{.ServiceName}}

+ + diff --git a/mail/templates/mail-verify.go.html b/mail/templates/mail-verify.go.html index 74e24b4..d4fc337 100644 --- a/mail/templates/mail-verify.go.html +++ b/mail/templates/mail-verify.go.html @@ -1,12 +1,10 @@ - - -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}} + + + +

Hello, {{.Name}}

+

Please open this link to verify your email address

+

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

+ +