github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/setting/mailer.go (about) 1 // Copyright 2023 The GitBundle Inc. All rights reserved. 2 // Copyright 2017 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 6 package setting 7 8 import ( 9 "net/mail" 10 "time" 11 12 "github.com/gitbundle/modules/log" 13 14 shellquote "github.com/kballard/go-shellquote" 15 ) 16 17 // Mailer represents mail service. 18 type Mailer struct { 19 // Mailer 20 Name string 21 From string 22 EnvelopeFrom string 23 OverrideEnvelopeFrom bool `ini:"-"` 24 FromName string 25 FromEmail string 26 SendAsPlainText bool 27 MailerType string 28 SubjectPrefix string 29 30 // SMTP sender 31 Host string 32 User, Passwd string 33 DisableHelo bool 34 HeloHostname string 35 SkipVerify bool 36 UseCertificate bool 37 CertFile, KeyFile string 38 IsTLSEnabled bool 39 40 // Sendmail sender 41 SendmailPath string 42 SendmailArgs []string 43 SendmailTimeout time.Duration 44 SendmailConvertCRLF bool 45 } 46 47 // MailService the global mailer 48 var MailService *Mailer 49 50 func newMailService() { 51 sec := Cfg.Section("mailer") 52 // Check mailer setting. 53 if !sec.Key("ENABLED").MustBool() { 54 return 55 } 56 57 MailService = &Mailer{ 58 Name: sec.Key("NAME").MustString(AppName), 59 SendAsPlainText: sec.Key("SEND_AS_PLAIN_TEXT").MustBool(false), 60 MailerType: sec.Key("MAILER_TYPE").In("", []string{"smtp", "sendmail", "dummy"}), 61 62 Host: sec.Key("HOST").String(), 63 User: sec.Key("USER").String(), 64 Passwd: sec.Key("PASSWD").String(), 65 DisableHelo: sec.Key("DISABLE_HELO").MustBool(), 66 HeloHostname: sec.Key("HELO_HOSTNAME").String(), 67 SkipVerify: sec.Key("SKIP_VERIFY").MustBool(), 68 UseCertificate: sec.Key("USE_CERTIFICATE").MustBool(), 69 CertFile: sec.Key("CERT_FILE").String(), 70 KeyFile: sec.Key("KEY_FILE").String(), 71 IsTLSEnabled: sec.Key("IS_TLS_ENABLED").MustBool(), 72 SubjectPrefix: sec.Key("SUBJECT_PREFIX").MustString(""), 73 74 SendmailPath: sec.Key("SENDMAIL_PATH").MustString("sendmail"), 75 SendmailTimeout: sec.Key("SENDMAIL_TIMEOUT").MustDuration(5 * time.Minute), 76 SendmailConvertCRLF: sec.Key("SENDMAIL_CONVERT_CRLF").MustBool(true), 77 } 78 MailService.From = sec.Key("FROM").MustString(MailService.User) 79 MailService.EnvelopeFrom = sec.Key("ENVELOPE_FROM").MustString("") 80 81 // FIXME: DEPRECATED to be removed in v1.18.0 82 deprecatedSetting("mailer", "ENABLE_HTML_ALTERNATIVE", "mailer", "SEND_AS_PLAIN_TEXT") 83 if sec.HasKey("ENABLE_HTML_ALTERNATIVE") { 84 MailService.SendAsPlainText = !sec.Key("ENABLE_HTML_ALTERNATIVE").MustBool(false) 85 } 86 87 // FIXME: DEPRECATED to be removed in v1.18.0 88 deprecatedSetting("mailer", "USE_SENDMAIL", "mailer", "MAILER_TYPE") 89 if sec.HasKey("USE_SENDMAIL") { 90 if MailService.MailerType == "" && sec.Key("USE_SENDMAIL").MustBool(false) { 91 MailService.MailerType = "sendmail" 92 } 93 } 94 95 parsed, err := mail.ParseAddress(MailService.From) 96 if err != nil { 97 log.Fatal("Invalid mailer.FROM (%s): %v", MailService.From, err) 98 } 99 MailService.FromName = parsed.Name 100 if MailService.Name != "" { 101 MailService.FromName = MailService.Name 102 } 103 MailService.FromEmail = parsed.Address 104 105 switch MailService.EnvelopeFrom { 106 case "": 107 MailService.OverrideEnvelopeFrom = false 108 case "<>": 109 MailService.EnvelopeFrom = "" 110 MailService.OverrideEnvelopeFrom = true 111 default: 112 parsed, err = mail.ParseAddress(MailService.EnvelopeFrom) 113 if err != nil { 114 log.Fatal("Invalid mailer.ENVELOPE_FROM (%s): %v", MailService.EnvelopeFrom, err) 115 } 116 MailService.OverrideEnvelopeFrom = true 117 MailService.EnvelopeFrom = parsed.Address 118 } 119 120 if MailService.MailerType == "" { 121 MailService.MailerType = "smtp" 122 } 123 124 if MailService.MailerType == "sendmail" { 125 MailService.SendmailArgs, err = shellquote.Split(sec.Key("SENDMAIL_ARGS").String()) 126 if err != nil { 127 log.Error("Failed to parse Sendmail args: %s with error %v", CustomConf, err) 128 } 129 } 130 131 log.Info("Mail Service Enabled") 132 } 133 134 func newRegisterMailService() { 135 if !Cfg.Section("service").Key("REGISTER_EMAIL_CONFIRM").MustBool() { 136 return 137 } else if MailService == nil { 138 log.Warn("Register Mail Service: Mail Service is not enabled") 139 return 140 } 141 Service.RegisterEmailConfirm = true 142 log.Info("Register Mail Service Enabled") 143 } 144 145 func newNotifyMailService() { 146 if !Cfg.Section("service").Key("ENABLE_NOTIFY_MAIL").MustBool() { 147 return 148 } else if MailService == nil { 149 log.Warn("Notify Mail Service: Mail Service is not enabled") 150 return 151 } 152 Service.EnableNotifyMail = true 153 log.Info("Notify Mail Service Enabled") 154 }