1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 20:50:54 +00:00

Update smtp.go

This commit is contained in:
1fei 2013-12-19 18:16:06 +08:00
parent dcaff38cb9
commit b87e122ac4

View File

@ -2,7 +2,6 @@ package logs
import ( import (
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"net/smtp" "net/smtp"
"strings" "strings"
@ -15,77 +14,51 @@ const (
// smtpWriter is used to send emails via given SMTP-server. // smtpWriter is used to send emails via given SMTP-server.
type SmtpWriter struct { type SmtpWriter struct {
username string Username string `json:"Username"`
password string Password string `json:"password"`
host string Host string `json:"Host"`
subject string Subject string `json:"subject"`
recipientAddresses []string RecipientAddresses []string `json:"sendTos"`
level int Level int `json:"level"`
} }
func NewSmtpWriter() LoggerInterface { func NewSmtpWriter() LoggerInterface {
return &SmtpWriter{level: LevelTrace} return &SmtpWriter{Level: LevelTrace}
} }
func (s *SmtpWriter) Init(jsonconfig string) error { func (s *SmtpWriter) Init(jsonconfig string) error {
var m map[string]interface{} err := json.Unmarshal([]byte(jsonconfig), s)
err := json.Unmarshal([]byte(jsonconfig), &m)
if err != nil { if err != nil {
return err return err
} }
if username, ok := m["username"]; !ok {
return errors.New("smtp config must have auth username")
} else if password, ok := m["password"]; !ok {
return errors.New("smtp config must have auth password")
} else if hostname, ok := m["host"]; !ok {
return errors.New("smtp config must have host like 'mail.example.com:25'")
} else if sendTos, ok := m["sendTos"]; !ok {
return errors.New("smtp config must have sendTos")
} else {
s.username = username.(string)
s.password = password.(string)
s.host = hostname.(string)
for _, v := range sendTos.([]interface{}) {
s.recipientAddresses = append(s.recipientAddresses, v.(string))
}
}
if subject, ok := m["subject"]; ok {
s.subject = subject.(string)
} else {
s.subject = subjectPhrase
}
if lv, ok := m["level"]; ok {
s.level = int(lv.(float64))
}
return nil return nil
} }
func (s *SmtpWriter) WriteMsg(msg string, level int) error { func (s *SmtpWriter) WriteMsg(msg string, level int) error {
if level < s.level { if level < s.Level {
return nil return nil
} }
hp := strings.Split(s.host, ":") hp := strings.Split(s.Host, ":")
// Set up authentication information. // Set up authentication information.
auth := smtp.PlainAuth( auth := smtp.PlainAuth(
"", "",
s.username, s.Username,
s.password, s.Password,
hp[0], hp[0],
) )
// Connect to the server, authenticate, set the sender and recipient, // Connect to the server, authenticate, set the sender and recipient,
// and send the email all in one step. // and send the email all in one step.
content_type := "Content-Type: text/plain" + "; charset=UTF-8" content_type := "Content-Type: text/plain" + "; charset=UTF-8"
mailmsg := []byte("To: " + strings.Join(s.recipientAddresses, ";") + "\r\nFrom: " + s.username + "<" + s.username + mailmsg := []byte("To: " + strings.Join(s.RecipientAddresses, ";") + "\r\nFrom: " + s.Username + "<" + s.Username +
">\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) ">\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)
err := smtp.SendMail( err := smtp.SendMail(
s.host, s.Host,
auth, auth,
s.username, s.Username,
s.recipientAddresses, s.RecipientAddresses,
mailmsg, mailmsg,
) )