move structs in right packages + impl sender service to build and send e-mail
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
package mail
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Email struct {
|
||||
Sender string `json:"sender"`
|
||||
Receivers []string `json:"receivers"`
|
||||
Subject string `json:"subject"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
func NewEmail(sender string, receivers []string, subject, content string) Email {
|
||||
return Email{
|
||||
Sender: sender,
|
||||
Receivers: receivers,
|
||||
Subject: subject,
|
||||
Content: content,
|
||||
}
|
||||
}
|
||||
|
||||
func FromJSON(path string) error {
|
||||
content, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var mail Email
|
||||
if err := json.Unmarshal(content, &mail); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e Email) Generate() []byte {
|
||||
mail := fmt.Sprintf(
|
||||
"To: %s\nFrom: %s\nContent-Type: text/html\nSubject: %s\n\n%s",
|
||||
strings.Join(e.Receivers, ","),
|
||||
e.Sender,
|
||||
e.Sender,
|
||||
e.Content,
|
||||
)
|
||||
return []byte(mail)
|
||||
}
|
||||
Reference in New Issue
Block a user