smtp.go 2.4 KB
Newer Older
A
astaxie 已提交
1
// Beego (http://beego.me/)
A
astaxie 已提交
2
//
A
astaxie 已提交
3
// @description beego is an open-source, high-performance web framework for the Go programming language.
A
astaxie 已提交
4
//
A
astaxie 已提交
5
// @link        http://github.com/astaxie/beego for the canonical source repository
A
astaxie 已提交
6
//
A
astaxie 已提交
7
// @license     http://github.com/astaxie/beego/blob/master/LICENSE
A
astaxie 已提交
8
//
A
astaxie 已提交
9
// @authors     astaxie
A
astaxie 已提交
10 11 12 13
package logs

import (
	"encoding/json"
14
	"fmt"
A
astaxie 已提交
15 16
	"net/smtp"
	"strings"
17
	"time"
A
astaxie 已提交
18 19 20 21 22 23
)

const (
	subjectPhrase = "Diagnostic message from server"
)

F
FuXiaoHei 已提交
24
// smtpWriter implements LoggerInterface and is used to send emails via given SMTP-server.
A
astaxie 已提交
25
type SmtpWriter struct {
1
1fei 已提交
26 27 28 29 30 31
	Username           string   `json:"Username"`
	Password           string   `json:"password"`
	Host               string   `json:"Host"`
	Subject            string   `json:"subject"`
	RecipientAddresses []string `json:"sendTos"`
	Level              int      `json:"level"`
A
astaxie 已提交
32 33
}

F
FuXiaoHei 已提交
34
// create smtp writer.
A
astaxie 已提交
35
func NewSmtpWriter() LoggerInterface {
1
1fei 已提交
36
	return &SmtpWriter{Level: LevelTrace}
A
astaxie 已提交
37 38
}

F
FuXiaoHei 已提交
39 40 41 42 43 44 45 46 47 48
// init smtp writer with json config.
// config like:
//	{
//		"Username":"example@gmail.com",
//		"password:"password",
//		"host":"smtp.gmail.com:465",
//		"subject":"email title",
//		"sendTos":["email1","email2"],
//		"level":LevelError
//	}
A
astaxie 已提交
49
func (s *SmtpWriter) Init(jsonconfig string) error {
1
1fei 已提交
50
	err := json.Unmarshal([]byte(jsonconfig), s)
A
astaxie 已提交
51 52 53 54 55 56
	if err != nil {
		return err
	}
	return nil
}

F
FuXiaoHei 已提交
57 58
// write message in smtp writer.
// it will send an email with subject and only this message.
A
astaxie 已提交
59
func (s *SmtpWriter) WriteMsg(msg string, level int) error {
1
1fei 已提交
60
	if level < s.Level {
A
astaxie 已提交
61 62 63
		return nil
	}

1
1fei 已提交
64
	hp := strings.Split(s.Host, ":")
A
astaxie 已提交
65 66 67 68

	// Set up authentication information.
	auth := smtp.PlainAuth(
		"",
1
1fei 已提交
69 70
		s.Username,
		s.Password,
A
astaxie 已提交
71 72 73 74 75
		hp[0],
	)
	// Connect to the server, authenticate, set the sender and recipient,
	// and send the email all in one step.
	content_type := "Content-Type: text/plain" + "; charset=UTF-8"
1
1fei 已提交
76 77
	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)
78

A
astaxie 已提交
79
	err := smtp.SendMail(
1
1fei 已提交
80
		s.Host,
A
astaxie 已提交
81
		auth,
1
1fei 已提交
82 83
		s.Username,
		s.RecipientAddresses,
A
astaxie 已提交
84 85
		mailmsg,
	)
86

A
astaxie 已提交
87 88 89
	return err
}

F
FuXiaoHei 已提交
90
// implementing method. empty.
A
astaxie 已提交
91 92 93
func (s *SmtpWriter) Flush() {
	return
}
F
FuXiaoHei 已提交
94 95

// implementing method. empty.
A
astaxie 已提交
96 97 98 99 100 101 102
func (s *SmtpWriter) Destroy() {
	return
}

func init() {
	Register("smtp", NewSmtpWriter)
}