package mailer
import (
"bytes"
"io"
"net/mail"
"ganigeorgiev.com/pocketbase/pocketbase/tools/hook"
"github.com/gabriel-vasile/mimetype"
)
type Message struct {
From mail.Address `json:"from"`
To []mail.Address `json:"to"`
Bcc []mail.Address `json:"bcc"`
Cc []mail.Address `json:"cc"`
Subject string `json:"subject"`
HTML string `json:"html"`
Text string `json:"text"`
Headers map[string]string `json:"headers"`
Attachments map[string]io.Reader `json:"attachments"`
InlineAttachments map[string]io.Reader `json:"inlineAttachments"`
}
type Mailer interface {
Send(message *Message) error
}
type SendInterceptor interface {
OnSend() *hook.Hook[*SendEvent]
}
type SendEvent struct {
hook.Event
Message *Message
}
func addressesToStrings(addresses []mail.Address, withName bool) []string {
result := make([]string, len(addresses))
for i, addr := range addresses {
if withName && addr.Name != "" {
result[i] = addr.String()
} else {
result[i] = addr.Address
}
}
return result
}
func detectReaderMimeType(r io.Reader) (io.Reader, string, error) {
readCopy := new(bytes.Buffer)
mime, err := mimetype.DetectReader(io.TeeReader(r, readCopy))
if err != nil {
return nil, "", err
}
return io.MultiReader(readCopy, r), mime.String(), nil
}