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.

mailer.go 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package setting
  5. import (
  6. "net/mail"
  7. "time"
  8. "code.gitea.io/gitea/modules/log"
  9. shellquote "github.com/kballard/go-shellquote"
  10. )
  11. // Mailer represents mail service.
  12. type Mailer struct {
  13. // Mailer
  14. QueueLength int
  15. Name string
  16. From string
  17. FromName string
  18. FromEmail string
  19. SendAsPlainText bool
  20. MailerType string
  21. SubjectPrefix string
  22. // SMTP sender
  23. Host string
  24. User, Passwd string
  25. DisableHelo bool
  26. HeloHostname string
  27. SkipVerify bool
  28. UseCertificate bool
  29. CertFile, KeyFile string
  30. IsTLSEnabled bool
  31. // Sendmail sender
  32. SendmailPath string
  33. SendmailArgs []string
  34. SendmailTimeout time.Duration
  35. }
  36. var (
  37. // MailService the global mailer
  38. MailService *Mailer
  39. )
  40. func newMailService() {
  41. sec := Cfg.Section("mailer")
  42. // Check mailer setting.
  43. if !sec.Key("ENABLED").MustBool() {
  44. return
  45. }
  46. MailService = &Mailer{
  47. QueueLength: sec.Key("SEND_BUFFER_LEN").MustInt(100),
  48. Name: sec.Key("NAME").MustString(AppName),
  49. SendAsPlainText: sec.Key("SEND_AS_PLAIN_TEXT").MustBool(false),
  50. MailerType: sec.Key("MAILER_TYPE").In("", []string{"smtp", "sendmail", "dummy"}),
  51. Host: sec.Key("HOST").String(),
  52. User: sec.Key("USER").String(),
  53. Passwd: sec.Key("PASSWD").String(),
  54. DisableHelo: sec.Key("DISABLE_HELO").MustBool(),
  55. HeloHostname: sec.Key("HELO_HOSTNAME").String(),
  56. SkipVerify: sec.Key("SKIP_VERIFY").MustBool(),
  57. UseCertificate: sec.Key("USE_CERTIFICATE").MustBool(),
  58. CertFile: sec.Key("CERT_FILE").String(),
  59. KeyFile: sec.Key("KEY_FILE").String(),
  60. IsTLSEnabled: sec.Key("IS_TLS_ENABLED").MustBool(),
  61. SubjectPrefix: sec.Key("SUBJECT_PREFIX").MustString(""),
  62. SendmailPath: sec.Key("SENDMAIL_PATH").MustString("sendmail"),
  63. SendmailTimeout: sec.Key("SENDMAIL_TIMEOUT").MustDuration(5 * time.Minute),
  64. }
  65. MailService.From = sec.Key("FROM").MustString(MailService.User)
  66. if sec.HasKey("ENABLE_HTML_ALTERNATIVE") {
  67. log.Warn("ENABLE_HTML_ALTERNATIVE is deprecated, use SEND_AS_PLAIN_TEXT")
  68. MailService.SendAsPlainText = !sec.Key("ENABLE_HTML_ALTERNATIVE").MustBool(false)
  69. }
  70. if sec.HasKey("USE_SENDMAIL") {
  71. log.Warn("USE_SENDMAIL is deprecated, use MAILER_TYPE=sendmail")
  72. if MailService.MailerType == "" && sec.Key("USE_SENDMAIL").MustBool(false) {
  73. MailService.MailerType = "sendmail"
  74. }
  75. }
  76. parsed, err := mail.ParseAddress(MailService.From)
  77. if err != nil {
  78. log.Fatal("Invalid mailer.FROM (%s): %v", MailService.From, err)
  79. }
  80. MailService.FromName = parsed.Name
  81. MailService.FromEmail = parsed.Address
  82. if MailService.MailerType == "" {
  83. MailService.MailerType = "smtp"
  84. }
  85. if MailService.MailerType == "sendmail" {
  86. MailService.SendmailArgs, err = shellquote.Split(sec.Key("SENDMAIL_ARGS").String())
  87. if err != nil {
  88. log.Error("Failed to parse Sendmail args: %s with error %v", CustomConf, err)
  89. }
  90. }
  91. log.Info("Mail Service Enabled")
  92. }
  93. func newRegisterMailService() {
  94. if !Cfg.Section("service").Key("REGISTER_EMAIL_CONFIRM").MustBool() {
  95. return
  96. } else if MailService == nil {
  97. log.Warn("Register Mail Service: Mail Service is not enabled")
  98. return
  99. }
  100. Service.RegisterEmailConfirm = true
  101. log.Info("Register Mail Service Enabled")
  102. }
  103. func newNotifyMailService() {
  104. if !Cfg.Section("service").Key("ENABLE_NOTIFY_MAIL").MustBool() {
  105. return
  106. } else if MailService == nil {
  107. log.Warn("Notify Mail Service: Mail Service is not enabled")
  108. return
  109. }
  110. Service.EnableNotifyMail = true
  111. log.Info("Notify Mail Service Enabled")
  112. }