2014-07-27 04:07:18 +05:30
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
2019-04-02 13:18:31 +05:30
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2014-07-27 04:07:18 +05:30
|
|
|
|
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
2021-03-02 02:38:10 +05:30
|
|
|
"fmt"
|
2014-07-27 04:07:18 +05:30
|
|
|
"net/smtp"
|
|
|
|
"strings"
|
2021-03-02 02:38:10 +05:30
|
|
|
|
2021-07-24 21:33:58 +05:30
|
|
|
"code.gitea.io/gitea/modules/json"
|
2014-07-27 04:07:18 +05:30
|
|
|
)
|
|
|
|
|
2019-04-02 13:18:31 +05:30
|
|
|
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-07 05:55:14 +05:30
|
|
|
WriterLogger
|
2014-07-27 04:07:18 +05:30
|
|
|
Username string `json:"Username"`
|
|
|
|
Password string `json:"password"`
|
2019-04-02 13:18:31 +05:30
|
|
|
Host string `json:"host"`
|
2014-07-27 04:07:18 +05:30
|
|
|
Subject string `json:"subject"`
|
|
|
|
RecipientAddresses []string `json:"sendTos"`
|
2019-04-02 13:18:31 +05:30
|
|
|
sendMailFn func(string, smtp.Auth, string, []string, []byte) error
|
2014-07-27 04:07:18 +05:30
|
|
|
}
|
|
|
|
|
2019-04-02 13:18:31 +05:30
|
|
|
// NewSMTPLogger creates smtp writer.
|
|
|
|
func NewSMTPLogger() LoggerProvider {
|
|
|
|
s := &SMTPLogger{}
|
|
|
|
s.Level = TRACE
|
|
|
|
s.sendMailFn = smtp.SendMail
|
|
|
|
return s
|
2014-07-27 04:07:18 +05:30
|
|
|
}
|
|
|
|
|
2016-11-26 17:23:29 +05:30
|
|
|
// Init smtp writer with json config.
|
2014-07-27 04:07:18 +05:30
|
|
|
// config like:
|
2022-08-31 07:45:45 +05:30
|
|
|
//
|
2014-07-27 04:07:18 +05:30
|
|
|
// {
|
|
|
|
// "Username":"example@gmail.com",
|
|
|
|
// "password:"password",
|
|
|
|
// "host":"smtp.gmail.com:465",
|
|
|
|
// "subject":"email title",
|
|
|
|
// "sendTos":["email1","email2"],
|
|
|
|
// "level":LevelError
|
|
|
|
// }
|
2019-04-02 13:18:31 +05:30
|
|
|
func (log *SMTPLogger) Init(jsonconfig string) error {
|
|
|
|
err := json.Unmarshal([]byte(jsonconfig), log)
|
|
|
|
if err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return fmt.Errorf("Unable to parse JSON: %w", err)
|
2019-04-02 13:18:31 +05:30
|
|
|
}
|
2019-04-07 05:55:14 +05:30
|
|
|
log.NewWriterLogger(&smtpWriter{
|
2019-04-02 13:18:31 +05:30
|
|
|
owner: log,
|
|
|
|
})
|
|
|
|
log.sendMailFn = smtp.SendMail
|
|
|
|
return nil
|
2014-07-27 04:07:18 +05:30
|
|
|
}
|
|
|
|
|
2016-11-26 17:23:29 +05:30
|
|
|
// WriteMsg writes message in smtp writer.
|
2014-07-27 04:07:18 +05:30
|
|
|
// it will send an email with subject and only this message.
|
2019-04-02 13:18:31 +05:30
|
|
|
func (log *SMTPLogger) sendMail(p []byte) (int, error) {
|
|
|
|
hp := strings.Split(log.Host, ":")
|
2014-07-27 04:07:18 +05:30
|
|
|
|
|
|
|
// Set up authentication information.
|
|
|
|
auth := smtp.PlainAuth(
|
|
|
|
"",
|
2019-04-02 13:18:31 +05:30
|
|
|
log.Username,
|
|
|
|
log.Password,
|
2014-07-27 04:07:18 +05:30
|
|
|
hp[0],
|
|
|
|
)
|
|
|
|
// Connect to the server, authenticate, set the sender and recipient,
|
|
|
|
// and send the email all in one step.
|
2016-11-26 17:23:29 +05:30
|
|
|
contentType := "Content-Type: text/plain" + "; charset=UTF-8"
|
2019-04-02 13:18:31 +05:30
|
|
|
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-27 04:07:18 +05:30
|
|
|
auth,
|
2019-04-02 13:18:31 +05:30
|
|
|
log.Username,
|
|
|
|
log.RecipientAddresses,
|
2014-07-27 04:07:18 +05:30
|
|
|
mailmsg,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-02-25 14:50:50 +05:30
|
|
|
// Content returns the content accumulated in the content provider
|
|
|
|
func (log *SMTPLogger) Content() (string, error) {
|
|
|
|
return "", fmt.Errorf("not supported")
|
|
|
|
}
|
|
|
|
|
2016-11-26 17:23:29 +05:30
|
|
|
// Flush when log should be flushed
|
2019-04-02 13:18:31 +05:30
|
|
|
func (log *SMTPLogger) Flush() {
|
2014-07-27 04:07:18 +05:30
|
|
|
}
|
|
|
|
|
2020-07-06 05:37:07 +05:30
|
|
|
// ReleaseReopen does nothing
|
|
|
|
func (log *SMTPLogger) ReleaseReopen() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-02 13:18:31 +05:30
|
|
|
// GetName returns the default name for this implementation
|
|
|
|
func (log *SMTPLogger) GetName() string {
|
|
|
|
return "smtp"
|
2014-07-27 04:07:18 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2019-04-02 13:18:31 +05:30
|
|
|
Register("smtp", NewSMTPLogger)
|
2014-07-27 04:07:18 +05:30
|
|
|
}
|