1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-18 02:30:54 +00:00
Beego/logs/smtp.go

94 lines
2.1 KiB
Go
Raw Normal View History

2013-08-27 15:48:58 +00:00
package logs
import (
"encoding/json"
"fmt"
2013-08-27 15:48:58 +00:00
"net/smtp"
"strings"
"time"
2013-08-27 15:48:58 +00:00
)
const (
subjectPhrase = "Diagnostic message from server"
)
2013-12-30 15:32:57 +00:00
// smtpWriter implements LoggerInterface and is used to send emails via given SMTP-server.
2013-08-27 15:48:58 +00:00
type SmtpWriter struct {
2013-12-19 10:16:06 +00:00
Username string `json:"Username"`
Password string `json:"password"`
Host string `json:"Host"`
Subject string `json:"subject"`
RecipientAddresses []string `json:"sendTos"`
Level int `json:"level"`
2013-08-27 15:48:58 +00:00
}
2013-12-30 15:32:57 +00:00
// create smtp writer.
2013-08-27 15:48:58 +00:00
func NewSmtpWriter() LoggerInterface {
2013-12-19 10:16:06 +00:00
return &SmtpWriter{Level: LevelTrace}
2013-08-27 15:48:58 +00:00
}
2013-12-30 15:32:57 +00:00
// 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
// }
2013-08-27 15:48:58 +00:00
func (s *SmtpWriter) Init(jsonconfig string) error {
2013-12-19 10:16:06 +00:00
err := json.Unmarshal([]byte(jsonconfig), s)
2013-08-27 15:48:58 +00:00
if err != nil {
return err
}
return nil
}
2013-12-30 15:32:57 +00:00
// write message in smtp writer.
// it will send an email with subject and only this message.
2013-08-27 15:48:58 +00:00
func (s *SmtpWriter) WriteMsg(msg string, level int) error {
2013-12-19 10:16:06 +00:00
if level < s.Level {
2013-08-27 15:48:58 +00:00
return nil
}
2013-12-19 10:16:06 +00:00
hp := strings.Split(s.Host, ":")
2013-08-27 15:48:58 +00:00
// Set up authentication information.
auth := smtp.PlainAuth(
"",
2013-12-19 10:16:06 +00:00
s.Username,
s.Password,
2013-08-27 15:48:58 +00:00
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"
2013-12-19 10:16:06 +00:00
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)
2013-08-27 15:48:58 +00:00
err := smtp.SendMail(
2013-12-19 10:16:06 +00:00
s.Host,
2013-08-27 15:48:58 +00:00
auth,
2013-12-19 10:16:06 +00:00
s.Username,
s.RecipientAddresses,
2013-08-27 15:48:58 +00:00
mailmsg,
)
2013-08-27 15:48:58 +00:00
return err
}
2013-12-30 15:32:57 +00:00
// implementing method. empty.
2013-11-27 09:50:10 +00:00
func (s *SmtpWriter) Flush() {
return
}
2013-12-30 15:32:57 +00:00
// implementing method. empty.
2013-08-27 15:48:58 +00:00
func (s *SmtpWriter) Destroy() {
return
}
func init() {
Register("smtp", NewSmtpWriter)
}