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

mailer.go 3.5 kB

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