-
Notifications
You must be signed in to change notification settings - Fork 28
/
mail.go
34 lines (28 loc) · 862 Bytes
/
mail.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package job
import (
"encoding/json"
"fmt"
"github.com/RobyFerro/go-web-framework/kernel"
"github.com/pkg/errors"
"log"
"net/smtp"
)
type MailStruct struct {
To []string `json:"to"`
Message string `json:"message"`
}
// Mail method will sends an email
// The payload must be a JSON object compliant with the MailStruct structure.
func (Job) Mail(payload string) (bool, error) {
var data MailStruct
conf, _ := kernel.RetrieveAppConf()
auth := smtp.PlainAuth("", conf.Mail.Host, conf.Mail.Password, conf.Mail.Host)
if err := json.Unmarshal([]byte(payload), &data); err != nil {
return false, errors.New("Cannot unmarshal payload")
}
server := fmt.Sprintf("%s:%d", conf.Mail.Host, conf.Mail.Port)
if err := smtp.SendMail(server, auth, conf.Mail.From, data.To, []byte(data.Message)); err != nil {
log.Fatal(err)
}
return true, nil
}