2021-04-10 01:07:13 +03:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-03-06 22:31:31 +03:00
|
|
|
uuid "github.com/satori/go.uuid"
|
2021-04-10 01:07:13 +03:00
|
|
|
"strings"
|
2023-03-06 22:31:31 +03:00
|
|
|
"time"
|
2021-04-10 01:07:13 +03:00
|
|
|
)
|
|
|
|
|
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 {
|
2023-03-06 22:31:31 +03:00
|
|
|
From MailAddress
|
|
|
|
To MailAddresses
|
|
|
|
Subject string
|
|
|
|
Body string
|
|
|
|
Type string
|
|
|
|
Date time.Time
|
|
|
|
MessageID string
|
2021-04-10 01:07:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-03-06 22:31:31 +03:00
|
|
|
func (m *Mail) Sanitized() *Mail {
|
|
|
|
if m.Type == "" {
|
|
|
|
m.Type = PlainType
|
|
|
|
}
|
|
|
|
if m.Date.IsZero() {
|
|
|
|
m.Date = time.Now()
|
|
|
|
}
|
|
|
|
if m.MessageID == "" {
|
|
|
|
m.MessageID = fmt.Sprintf("<%s@%s>", uuid.NewV4().String(), m.From.Domain())
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2021-04-10 01:07:13 +03:00
|
|
|
func (m *Mail) String() string {
|
|
|
|
return fmt.Sprintf("To: %s\r\n"+
|
|
|
|
"From: %s\r\n"+
|
|
|
|
"Subject: %s\r\n"+
|
2023-03-06 22:31:31 +03:00
|
|
|
"Message-ID: %s\r\n"+
|
|
|
|
"MIME-Version: 1.0\r\n"+
|
2021-04-10 01:07:13 +03:00
|
|
|
"Content-Type: %s\r\n"+
|
2023-03-06 22:31:31 +03:00
|
|
|
"Content-Transfer-Encoding: 8bit\r\n"+
|
|
|
|
"Date: %s\r\n"+
|
2021-04-10 01:07:13 +03:00
|
|
|
"\r\n"+
|
|
|
|
"%s\r\n",
|
|
|
|
strings.Join(m.To.RawStrings(), ", "),
|
|
|
|
m.From.String(),
|
|
|
|
m.Subject,
|
2023-03-06 22:31:31 +03:00
|
|
|
m.MessageID,
|
2021-04-10 01:07:13 +03:00
|
|
|
m.Type,
|
2023-03-06 22:31:31 +03:00
|
|
|
m.Date.Format(time.RFC1123Z),
|
2021-04-10 01:07:13 +03:00
|
|
|
m.Body,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Mail) Reader() *strings.Reader {
|
|
|
|
return strings.NewReader(m.String())
|
|
|
|
}
|