2014-07-26 18:37:18 -04:00
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
2019-04-02 03:48:31 -04:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2014-07-26 18:37:18 -04:00
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
2021-03-01 16:08:10 -05:00
|
|
|
"fmt"
|
2014-07-26 18:37:18 -04:00
|
|
|
"net/smtp"
|
|
|
|
"strings"
|
2021-03-01 16:08:10 -05:00
|
|
|
|
2021-07-24 12:03:58 -04:00
|
|
|
"code.gitea.io/gitea/modules/json"
|
2014-07-26 18:37:18 -04:00
|
|
|
)
|
|
|
|
|
2019-04-02 03:48:31 -04:00
|
|
|
type smtpWriter struct {
|
|
|
|
owner *SMTPLogger
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write sends the message as an email
|
|
|
|
func (s *smtpWriter) Write(p []byte) (int, error) {
|
|
|
|
return s.owner.sendMail(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close does nothing
|
|
|
|
func (s *smtpWriter) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SMTPLogger implements LoggerProvider and is used to send emails via given SMTP-server.
|
|
|
|
type SMTPLogger struct {
|
2019-04-06 20:25:14 -04:00
|
|
|
WriterLogger
|
2014-07-26 18:37:18 -04:00
|
|
|
Username string `json:"Username"`
|
|
|
|
Password string `json:"password"`
|
2019-04-02 03:48:31 -04:00
|
|
|
Host string `json:"host"`
|
2014-07-26 18:37:18 -04:00
|
|
|
Subject string `json:"subject"`
|
|
|
|
RecipientAddresses []string `json:"sendTos"`
|
2019-04-02 03:48:31 -04:00
|
|
|
sendMailFn func(string, smtp.Auth, string, []string, []byte) error
|
2014-07-26 18:37:18 -04:00
|
|
|
}
|
|
|
|
|
2019-04-02 03:48:31 -04:00
|
|
|
// NewSMTPLogger creates smtp writer.
|
|
|
|
func NewSMTPLogger() LoggerProvider {
|
|
|
|
s := &SMTPLogger{}
|
|
|
|
s.Level = TRACE
|
|
|
|
s.sendMailFn = smtp.SendMail
|
|
|
|
return s
|
2014-07-26 18:37:18 -04:00
|
|
|
}
|
|
|
|
|
2016-11-26 06:53:29 -05:00
|
|
|
// Init smtp writer with json config.
|
2014-07-26 18:37:18 -04:00
|
|
|
// config like:
|
2022-08-30 22:15:45 -04:00
|
|
|
//
|
2014-07-26 18:37:18 -04:00
|
|
|
// {
|
|
|
|
// "Username":"example@gmail.com",
|
|
|
|
// "password:"password",
|
|
|
|
// "host":"smtp.gmail.com:465",
|
|
|
|
// "subject":"email title",
|
|
|
|
// "sendTos":["email1","email2"],
|
|
|
|
// "level":LevelError
|
|
|
|
// }
|
2019-04-02 03:48:31 -04:00
|
|
|
func (log *SMTPLogger) Init(jsonconfig string) error {
|
|
|
|
err := json.Unmarshal([]byte(jsonconfig), log)
|
|
|
|
if err != nil {
|
2022-10-24 15:29:17 -04:00
|
|
|
return fmt.Errorf("Unable to parse JSON: %w", err)
|
2019-04-02 03:48:31 -04:00
|
|
|
}
|
2019-04-06 20:25:14 -04:00
|
|
|
log.NewWriterLogger(&smtpWriter{
|
2019-04-02 03:48:31 -04:00
|
|
|
owner: log,
|
|
|
|
})
|
|
|
|
log.sendMailFn = smtp.SendMail
|
|
|
|
return nil
|
2014-07-26 18:37:18 -04:00
|
|
|
}
|
|
|
|
|
2016-11-26 06:53:29 -05:00
|
|
|
// WriteMsg writes message in smtp writer.
|
2014-07-26 18:37:18 -04:00
|
|
|
// it will send an email with subject and only this message.
|
2019-04-02 03:48:31 -04:00
|
|
|
func (log *SMTPLogger) sendMail(p []byte) (int, error) {
|
|
|
|
hp := strings.Split(log.Host, ":")
|
2014-07-26 18:37:18 -04:00
|
|
|
|
|
|
|
// Set up authentication information.
|
|
|
|
auth := smtp.PlainAuth(
|
|
|
|
"",
|
2019-04-02 03:48:31 -04:00
|
|
|
log.Username,
|
|
|
|
log.Password,
|
2014-07-26 18:37:18 -04:00
|
|
|
hp[0],
|
|
|
|
)
|
|
|
|
// Connect to the server, authenticate, set the sender and recipient,
|
|
|
|
// and send the email all in one step.
|
2016-11-26 06:53:29 -05:00
|
|
|
contentType := "Content-Type: text/plain" + "; charset=UTF-8"
|
2019-04-02 03:48:31 -04:00
|
|
|
mailmsg := []byte("To: " + strings.Join(log.RecipientAddresses, ";") + "\r\nFrom: " + log.Username + "<" + log.Username +
|
|
|
|
">\r\nSubject: " + log.Subject + "\r\n" + contentType + "\r\n\r\n")
|
|
|
|
mailmsg = append(mailmsg, p...)
|
|
|
|
return len(p), log.sendMailFn(
|
|
|
|
log.Host,
|
2014-07-26 18:37:18 -04:00
|
|
|
auth,
|
2019-04-02 03:48:31 -04:00
|
|
|
log.Username,
|
|
|
|
log.RecipientAddresses,
|
2014-07-26 18:37:18 -04:00
|
|
|
mailmsg,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-02-25 04:20:50 -05:00
|
|
|
// Content returns the content accumulated in the content provider
|
|
|
|
func (log *SMTPLogger) Content() (string, error) {
|
|
|
|
return "", fmt.Errorf("not supported")
|
|
|
|
}
|
|
|
|
|
2016-11-26 06:53:29 -05:00
|
|
|
// Flush when log should be flushed
|
2019-04-02 03:48:31 -04:00
|
|
|
func (log *SMTPLogger) Flush() {
|
2014-07-26 18:37:18 -04:00
|
|
|
}
|
|
|
|
|
2020-07-05 20:07:07 -04:00
|
|
|
// ReleaseReopen does nothing
|
|
|
|
func (log *SMTPLogger) ReleaseReopen() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-02 03:48:31 -04:00
|
|
|
// GetName returns the default name for this implementation
|
|
|
|
func (log *SMTPLogger) GetName() string {
|
|
|
|
return "smtp"
|
2014-07-26 18:37:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2019-04-02 03:48:31 -04:00
|
|
|
Register("smtp", NewSMTPLogger)
|
2014-07-26 18:37:18 -04:00
|
|
|
}
|