Beego/logs/smtp.go

116 lines
2.8 KiB
Go
Raw Normal View History

2014-08-18 08:41:43 +00:00
// Copyright 2014 beego Author. All Rights Reserved.
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// http://www.apache.org/licenses/LICENSE-2.0
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
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
}
func (s *SmtpWriter) GetSmtpAuth(host string) smtp.Auth {
if len(strings.Trim(s.Username, " ")) == 0 && len(strings.Trim(s.Password, " ")) == 0 {
return nil
}
return smtp.PlainAuth(
"",
s.Username,
s.Password,
host,
)
}
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 {
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 := s.GetSmtpAuth(hp[0])
2014-08-18 08:41:43 +00:00
2013-08-27 15:48:58 +00:00
// 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)
}