b87e122a by 1fei

Update smtp.go

1 parent dcaff38c
...@@ -2,7 +2,6 @@ package logs ...@@ -2,7 +2,6 @@ package logs
2 2
3 import ( 3 import (
4 "encoding/json" 4 "encoding/json"
5 "errors"
6 "fmt" 5 "fmt"
7 "net/smtp" 6 "net/smtp"
8 "strings" 7 "strings"
...@@ -15,77 +14,51 @@ const ( ...@@ -15,77 +14,51 @@ const (
15 14
16 // smtpWriter is used to send emails via given SMTP-server. 15 // smtpWriter is used to send emails via given SMTP-server.
17 type SmtpWriter struct { 16 type SmtpWriter struct {
18 username string 17 Username string `json:"Username"`
19 password string 18 Password string `json:"password"`
20 host string 19 Host string `json:"Host"`
21 subject string 20 Subject string `json:"subject"`
22 recipientAddresses []string 21 RecipientAddresses []string `json:"sendTos"`
23 level int 22 Level int `json:"level"`
24 } 23 }
25 24
26 func NewSmtpWriter() LoggerInterface { 25 func NewSmtpWriter() LoggerInterface {
27 return &SmtpWriter{level: LevelTrace} 26 return &SmtpWriter{Level: LevelTrace}
28 } 27 }
29 28
30 func (s *SmtpWriter) Init(jsonconfig string) error { 29 func (s *SmtpWriter) Init(jsonconfig string) error {
31 var m map[string]interface{} 30 err := json.Unmarshal([]byte(jsonconfig), s)
32 err := json.Unmarshal([]byte(jsonconfig), &m)
33 if err != nil { 31 if err != nil {
34 return err 32 return err
35 } 33 }
36 if username, ok := m["username"]; !ok {
37 return errors.New("smtp config must have auth username")
38 } else if password, ok := m["password"]; !ok {
39 return errors.New("smtp config must have auth password")
40 } else if hostname, ok := m["host"]; !ok {
41 return errors.New("smtp config must have host like 'mail.example.com:25'")
42 } else if sendTos, ok := m["sendTos"]; !ok {
43 return errors.New("smtp config must have sendTos")
44 } else {
45 s.username = username.(string)
46 s.password = password.(string)
47 s.host = hostname.(string)
48 for _, v := range sendTos.([]interface{}) {
49 s.recipientAddresses = append(s.recipientAddresses, v.(string))
50 }
51 }
52
53 if subject, ok := m["subject"]; ok {
54 s.subject = subject.(string)
55 } else {
56 s.subject = subjectPhrase
57 }
58 if lv, ok := m["level"]; ok {
59 s.level = int(lv.(float64))
60 }
61 return nil 34 return nil
62 } 35 }
63 36
64 func (s *SmtpWriter) WriteMsg(msg string, level int) error { 37 func (s *SmtpWriter) WriteMsg(msg string, level int) error {
65 if level < s.level { 38 if level < s.Level {
66 return nil 39 return nil
67 } 40 }
68 41
69 hp := strings.Split(s.host, ":") 42 hp := strings.Split(s.Host, ":")
70 43
71 // Set up authentication information. 44 // Set up authentication information.
72 auth := smtp.PlainAuth( 45 auth := smtp.PlainAuth(
73 "", 46 "",
74 s.username, 47 s.Username,
75 s.password, 48 s.Password,
76 hp[0], 49 hp[0],
77 ) 50 )
78 // Connect to the server, authenticate, set the sender and recipient, 51 // Connect to the server, authenticate, set the sender and recipient,
79 // and send the email all in one step. 52 // and send the email all in one step.
80 content_type := "Content-Type: text/plain" + "; charset=UTF-8" 53 content_type := "Content-Type: text/plain" + "; charset=UTF-8"
81 mailmsg := []byte("To: " + strings.Join(s.recipientAddresses, ";") + "\r\nFrom: " + s.username + "<" + s.username + 54 mailmsg := []byte("To: " + strings.Join(s.RecipientAddresses, ";") + "\r\nFrom: " + s.Username + "<" + s.Username +
82 ">\r\nSubject: " + s.subject + "\r\n" + content_type + "\r\n\r\n" + fmt.Sprintf(".%s", time.Now().Format("2006-01-02 15:04:05")) + msg) 55 ">\r\nSubject: " + s.Subject + "\r\n" + content_type + "\r\n\r\n" + fmt.Sprintf(".%s", time.Now().Format("2006-01-02 15:04:05")) + msg)
83 56
84 err := smtp.SendMail( 57 err := smtp.SendMail(
85 s.host, 58 s.Host,
86 auth, 59 auth,
87 s.username, 60 s.Username,
88 s.recipientAddresses, 61 s.RecipientAddresses,
89 mailmsg, 62 mailmsg,
90 ) 63 )
91 64
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!