mailsrv/mail/mail.go

47 lines
838 B
Go

package mail
import (
"encoding/json"
"fmt"
"os"
"strings"
)
type Email struct {
Path string
Sender string `json:"sender"`
Receivers string `json:"receivers"`
Subject string `json:"subject"`
Content string `json:"content"`
}
func FromJSON(path string) (Email, error) {
var mail Email
content, err := os.ReadFile(path)
if err != nil {
return mail, fmt.Errorf("%w, unable to read the file: %s", err, path)
}
if err := json.Unmarshal(content, &mail); err != nil {
return mail, err
}
return mail, nil
}
func (e Email) GetReceivers() []string {
return strings.Split(e.Receivers, ",")
}
func (e Email) Generate() []byte {
mail := fmt.Sprintf(
"To: %s\nFrom: %s\nContent-Type: text/html;charset=utf-8\nSubject: %s\n\n%s",
e.Receivers,
e.Sender,
e.Subject,
e.Content,
)
return []byte(mail)
}