2021-04-10 01:07:13 +03:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2021-04-30 16:14:29 +03:00
|
|
|
const HtmlType = "text/html; charset=UTF-8"
|
|
|
|
const PlainType = "text/html; charset=UTF-8"
|
|
|
|
|
2021-04-10 01:07:13 +03:00
|
|
|
type Mail struct {
|
|
|
|
From MailAddress
|
|
|
|
To MailAddresses
|
|
|
|
Subject string
|
|
|
|
Body string
|
|
|
|
Type string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Mail) WithText(text string) *Mail {
|
|
|
|
m.Body = text
|
2021-04-30 16:14:29 +03:00
|
|
|
m.Type = PlainType
|
2021-04-10 01:07:13 +03:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Mail) WithHTML(html string) *Mail {
|
|
|
|
m.Body = html
|
2021-04-30 16:14:29 +03:00
|
|
|
m.Type = HtmlType
|
2021-04-10 01:07:13 +03:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Mail) String() string {
|
|
|
|
return fmt.Sprintf("To: %s\r\n"+
|
|
|
|
"From: %s\r\n"+
|
|
|
|
"Subject: %s\r\n"+
|
|
|
|
"Content-Type: %s\r\n"+
|
|
|
|
"\r\n"+
|
|
|
|
"%s\r\n",
|
|
|
|
strings.Join(m.To.RawStrings(), ", "),
|
|
|
|
m.From.String(),
|
|
|
|
m.Subject,
|
|
|
|
m.Type,
|
|
|
|
m.Body,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Mail) Reader() *strings.Reader {
|
|
|
|
return strings.NewReader(m.String())
|
|
|
|
}
|