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.5KB

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