You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

smtp.go 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package log
  6. import (
  7. "encoding/json"
  8. "net/smtp"
  9. "strings"
  10. )
  11. const (
  12. subjectPhrase = "Diagnostic message from server"
  13. )
  14. type smtpWriter struct {
  15. owner *SMTPLogger
  16. }
  17. // Write sends the message as an email
  18. func (s *smtpWriter) Write(p []byte) (int, error) {
  19. return s.owner.sendMail(p)
  20. }
  21. // Close does nothing
  22. func (s *smtpWriter) Close() error {
  23. return nil
  24. }
  25. // SMTPLogger implements LoggerProvider and is used to send emails via given SMTP-server.
  26. type SMTPLogger struct {
  27. WriterLogger
  28. Username string `json:"Username"`
  29. Password string `json:"password"`
  30. Host string `json:"host"`
  31. Subject string `json:"subject"`
  32. RecipientAddresses []string `json:"sendTos"`
  33. sendMailFn func(string, smtp.Auth, string, []string, []byte) error
  34. }
  35. // NewSMTPLogger creates smtp writer.
  36. func NewSMTPLogger() LoggerProvider {
  37. s := &SMTPLogger{}
  38. s.Level = TRACE
  39. s.sendMailFn = smtp.SendMail
  40. return s
  41. }
  42. // Init smtp writer with json config.
  43. // config like:
  44. // {
  45. // "Username":"example@gmail.com",
  46. // "password:"password",
  47. // "host":"smtp.gmail.com:465",
  48. // "subject":"email title",
  49. // "sendTos":["email1","email2"],
  50. // "level":LevelError
  51. // }
  52. func (log *SMTPLogger) Init(jsonconfig string) error {
  53. err := json.Unmarshal([]byte(jsonconfig), log)
  54. if err != nil {
  55. return err
  56. }
  57. log.NewWriterLogger(&smtpWriter{
  58. owner: log,
  59. })
  60. log.sendMailFn = smtp.SendMail
  61. return nil
  62. }
  63. // WriteMsg writes message in smtp writer.
  64. // it will send an email with subject and only this message.
  65. func (log *SMTPLogger) sendMail(p []byte) (int, error) {
  66. hp := strings.Split(log.Host, ":")
  67. // Set up authentication information.
  68. auth := smtp.PlainAuth(
  69. "",
  70. log.Username,
  71. log.Password,
  72. hp[0],
  73. )
  74. // Connect to the server, authenticate, set the sender and recipient,
  75. // and send the email all in one step.
  76. contentType := "Content-Type: text/plain" + "; charset=UTF-8"
  77. mailmsg := []byte("To: " + strings.Join(log.RecipientAddresses, ";") + "\r\nFrom: " + log.Username + "<" + log.Username +
  78. ">\r\nSubject: " + log.Subject + "\r\n" + contentType + "\r\n\r\n")
  79. mailmsg = append(mailmsg, p...)
  80. return len(p), log.sendMailFn(
  81. log.Host,
  82. auth,
  83. log.Username,
  84. log.RecipientAddresses,
  85. mailmsg,
  86. )
  87. }
  88. // Flush when log should be flushed
  89. func (log *SMTPLogger) Flush() {
  90. }
  91. // GetName returns the default name for this implementation
  92. func (log *SMTPLogger) GetName() string {
  93. return "smtp"
  94. }
  95. func init() {
  96. Register("smtp", NewSMTPLogger)
  97. }