Skip to content

Commit c5c6f53

Browse files
unknwonachilleas-k
authored andcommitted
conf: overhaul email settings (#5940)
1 parent 41b9e25 commit c5c6f53

File tree

20 files changed

+253
-223
lines changed

20 files changed

+253
-223
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ All notable changes to Gogs are documented in this file.
2222
- Configuration option `[server] LANDING_PAGE` is deprecated and will end support in 0.13.0, please start using `[server] LANDING_URL`.
2323
- Configuration option `[database] DB_TYPE` is deprecated and will end support in 0.13.0, please start using `[database] TYPE`.
2424
- Configuration option `[database] PASSWD` is deprecated and will end support in 0.13.0, please start using `[database] PASSWORD`.
25+
- Configuration section `[mailer]` is deprecated and will end support in 0.13.0, please start using `[email]`.
2526

2627
### Fixed
2728

Makefile

+2-5
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,8 @@ less: public/css/gogs.css
7171
public/css/gogs.css: $(LESS_FILES)
7272
@type lessc >/dev/null 2>&1 && lessc --source-map "public/less/gogs.less" $@ || echo "lessc command not found or failed"
7373

74-
clean:
75-
go clean -i ./...
76-
77-
clean-mac: clean
78-
find . -name ".DS_Store" -print0 | xargs -0 rm
74+
clean-mac:
75+
find . -name "*.DS_Store" -type f -delete
7976

8077
test:
8178
go test -cover -race ./...

conf/app.ini

+34-31
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,40 @@ ENABLE_LOGIN_STATUS_COOKIE = false
179179
; The cookie name to store user login status.
180180
LOGIN_STATUS_COOKIE_NAME = login_status
181181

182+
[email]
183+
; Whether to enable the email service.
184+
ENABLED = false
185+
; The prefix prepended to the subject line.
186+
SUBJECT_PREFIX = `[%(BRAND_NAME)s] `
187+
; The SMTP server with its port, e.g. smtp.mailgun.org:587, smtp.gmail.com:587, smtp.qq.com:465
188+
; If the port ends is "465", SMTPS will be used. Using STARTTLS on port 587 is recommended per RFC 6409.
189+
; If the server supports STARTTLS it will always be used.
190+
HOST = smtp.mailgun.org:587
191+
; The email from address (RFC 5322). This can be just an email address, or the `"Name" <[email protected]>` format.
192+
193+
; The login user.
194+
195+
; The login password.
196+
PASSWORD =
197+
198+
; Whether to disable HELO operation when the hostname is different.
199+
DISABLE_HELO =
200+
; The custom hostname for HELO operation, default is from system.
201+
HELO_HOSTNAME =
202+
203+
; Whether to skip verifying the certificate of the server. Only use this for self-signed certificates.
204+
SKIP_VERIFY = false
205+
; Whether to use client certificates.
206+
USE_CERTIFICATE = false
207+
CERT_FILE = custom/email/cert.pem
208+
KEY_FILE = custom/email/key.pem
209+
210+
; Whether to use "text/plain" as content format.
211+
USE_PLAIN_TEXT = false
212+
; Whether to attach a plaintext alternative to the MIME message while sending HTML emails.
213+
; It is used to support older mail clients and make spam filters happier.
214+
ADD_PLAIN_TEXT_ALT = false
215+
182216
; Attachment settings for releases
183217
[release.attachment]
184218
; Whether attachments are enabled. Defaults to `true`
@@ -246,37 +280,6 @@ SKIP_TLS_VERIFY = false
246280
; Number of history information in each page
247281
PAGING_NUM = 10
248282

249-
[mailer]
250-
ENABLED = false
251-
; Buffer length of channel, keep it as it is if you don't know what it is.
252-
SEND_BUFFER_LEN = 100
253-
; Prefix prepended to the subject line
254-
SUBJECT_PREFIX = `[%(BRAND_NAME)s] `
255-
; Mail server
256-
; Gmail: smtp.gmail.com:587
257-
; QQ: smtp.qq.com:465
258-
; Note, if the port ends with "465", SMTPS will be used. Using STARTTLS on port 587 is recommended per RFC 6409. If the server supports STARTTLS it will always be used.
259-
HOST =
260-
; Disable HELO operation when hostname are different.
261-
DISABLE_HELO =
262-
; Custom hostname for HELO operation, default is from system.
263-
HELO_HOSTNAME =
264-
; Do not verify the certificate of the server. Only use this for self-signed certificates
265-
SKIP_VERIFY =
266-
; Use client certificate
267-
USE_CERTIFICATE = false
268-
CERT_FILE = custom/mailer/cert.pem
269-
KEY_FILE = custom/mailer/key.pem
270-
; Mail from address, RFC 5322. This can be just an email address, or the `"Name" <[email protected]>` format
271-
FROM =
272-
; Mailer user name and password
273-
USER =
274-
PASSWD =
275-
; Use text/plain as format of content
276-
USE_PLAIN_TEXT = false
277-
; If sending html emails, then also attach a plaintext alternative to the MIME message, to support older mail clients and make spam filters happier.
278-
ADD_PLAIN_TEXT_ALT = false
279-
280283
[cache]
281284
; Either "memory", "redis", or "memcache", default is "memory"
282285
ADAPTER = memory

conf/locale/locale_en-US.ini

+18-10
Original file line numberDiff line numberDiff line change
@@ -1252,6 +1252,24 @@ config.security.reverse_proxy_auth_user = Reverse proxy authentication header
12521252
config.security.enable_login_status_cookie = Enable login status cookie
12531253
config.security.login_status_cookie_name = Login status cookie
12541254
1255+
config.email_config = Email configuration
1256+
config.email.enabled = Enabled
1257+
config.email.subject_prefix = Subject prefix
1258+
config.email.host = Host
1259+
config.email.from = From
1260+
config.email.user = User
1261+
config.email.disable_helo = Disable HELO
1262+
config.email.helo_hostname = HELO hostname
1263+
config.email.skip_verify = Skip certificate verify
1264+
config.email.use_certificate = Use custom certificate
1265+
config.email.cert_file = Certificate file
1266+
config.email.key_file = Key file
1267+
config.email.use_plain_text = Use plain text
1268+
config.email.add_plain_text_alt = Add plain text alternative
1269+
config.email.send_test_mail = Send test email
1270+
config.email.test_mail_failed = Failed to send test email to '%s': %v
1271+
config.email.test_mail_sent = Test email has been sent to '%s'.
1272+
12551273
config.log_file_root_path = Log File Root Path
12561274
12571275
config.http_config = HTTP Configuration
@@ -1273,16 +1291,6 @@ config.queue_length = Queue Length
12731291
config.deliver_timeout = Deliver Timeout
12741292
config.skip_tls_verify = Skip TLS Verify
12751293
1276-
config.mailer_config = Mailer Configuration
1277-
config.mailer_enabled = Enabled
1278-
config.mailer_disable_helo = Disable HELO
1279-
config.mailer_subject_prefix = Subject Prefix
1280-
config.mailer_host = Host
1281-
config.mailer_user = User
1282-
config.send_test_mail = Send Test Email
1283-
config.test_mail_failed = Failed to send test email to '%s': %v
1284-
config.test_mail_sent = Test email has been sent to '%s'.
1285-
12861294
config.oauth_config = OAuth Configuration
12871295
config.oauth_enabled = Enabled
12881296

internal/assets/conf/conf_gen.go

+8-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/assets/templates/templates_gen.go

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/cmd/hook.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ import (
2323
"github.com/G-Node/gogs/internal/conf"
2424
"github.com/G-Node/gogs/internal/db"
2525
"github.com/G-Node/gogs/internal/db/errors"
26+
"github.com/G-Node/gogs/internal/email"
2627
"github.com/G-Node/gogs/internal/httplib"
27-
"github.com/G-Node/gogs/internal/mailer"
2828
"github.com/G-Node/gogs/internal/template"
2929
)
3030

@@ -199,7 +199,7 @@ func runHookPostReceive(c *cli.Context) error {
199199
// Post-receive hook does more than just gather Git information,
200200
// so we need to setup additional services for email notifications.
201201
conf.NewPostReceiveHookServices()
202-
mailer.NewContext()
202+
email.NewContext()
203203

204204
isWiki := strings.Contains(os.Getenv(db.ENV_REPO_CUSTOM_HOOKS_PATH), ".wiki.git/")
205205

internal/conf/conf.go

+26-65
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,30 @@ func Init(customConf string) error {
208208
}
209209
}
210210

211+
// **************************
212+
// ----- Email settings -----
213+
// **************************
214+
215+
if err = File.Section("email").MapTo(&Email); err != nil {
216+
return errors.Wrap(err, "mapping [email] section")
217+
}
218+
// LEGACY [0.13]: In case there are values with old section name.
219+
if err = File.Section("mailer").MapTo(&Email); err != nil {
220+
return errors.Wrap(err, "mapping [mailer] section")
221+
}
222+
223+
if Email.Enabled {
224+
if Email.From == "" {
225+
Email.From = Email.User
226+
}
227+
228+
parsed, err := mail.ParseAddress(Email.From)
229+
if err != nil {
230+
return errors.Wrapf(err, "parse mail address %q", Email.From)
231+
}
232+
Email.FromEmail = parsed.Address
233+
}
234+
211235
handleDeprecated()
212236

213237
// TODO
@@ -720,71 +744,10 @@ func newSessionService() {
720744
log.Trace("Session service is enabled")
721745
}
722746

723-
// Mailer represents mail service.
724-
type Mailer struct {
725-
QueueLength int
726-
SubjectPrefix string
727-
Host string
728-
From string
729-
FromEmail string
730-
User, Passwd string
731-
DisableHelo bool
732-
HeloHostname string
733-
SkipVerify bool
734-
UseCertificate bool
735-
CertFile, KeyFile string
736-
UsePlainText bool
737-
AddPlainTextAlt bool
738-
}
739-
740-
var (
741-
MailService *Mailer
742-
)
743-
744-
// newMailService initializes mail service options from configuration.
745-
// No non-error log will be printed in hook mode.
746-
func newMailService() {
747-
sec := File.Section("mailer")
748-
if !sec.Key("ENABLED").MustBool() {
749-
return
750-
}
751-
752-
MailService = &Mailer{
753-
QueueLength: sec.Key("SEND_BUFFER_LEN").MustInt(100),
754-
SubjectPrefix: sec.Key("SUBJECT_PREFIX").MustString("[" + App.BrandName + "] "),
755-
Host: sec.Key("HOST").String(),
756-
User: sec.Key("USER").String(),
757-
Passwd: sec.Key("PASSWD").String(),
758-
DisableHelo: sec.Key("DISABLE_HELO").MustBool(),
759-
HeloHostname: sec.Key("HELO_HOSTNAME").String(),
760-
SkipVerify: sec.Key("SKIP_VERIFY").MustBool(),
761-
UseCertificate: sec.Key("USE_CERTIFICATE").MustBool(),
762-
CertFile: sec.Key("CERT_FILE").String(),
763-
KeyFile: sec.Key("KEY_FILE").String(),
764-
UsePlainText: sec.Key("USE_PLAIN_TEXT").MustBool(),
765-
AddPlainTextAlt: sec.Key("ADD_PLAIN_TEXT_ALT").MustBool(),
766-
}
767-
MailService.From = sec.Key("FROM").MustString(MailService.User)
768-
769-
if len(MailService.From) > 0 {
770-
parsed, err := mail.ParseAddress(MailService.From)
771-
if err != nil {
772-
log.Fatal("Failed to parse value %q for '[mailer] FROM': %v", MailService.From, err)
773-
return
774-
}
775-
MailService.FromEmail = parsed.Address
776-
}
777-
778-
if HookMode {
779-
return
780-
}
781-
log.Trace("Mail service is enabled")
782-
}
783-
784747
func newRegisterMailService() {
785748
if !File.Section("service").Key("REGISTER_EMAIL_CONFIRM").MustBool() {
786749
return
787-
} else if MailService == nil {
750+
} else if !Email.Enabled {
788751
log.Warn("Email confirmation is not enabled due to the mail service is not available")
789752
return
790753
}
@@ -797,7 +760,7 @@ func newRegisterMailService() {
797760
func newNotifyMailService() {
798761
if !File.Section("service").Key("ENABLE_NOTIFY_MAIL").MustBool() {
799762
return
800-
} else if MailService == nil {
763+
} else if !Email.Enabled {
801764
log.Warn("Email notification is not enabled due to the mail service is not available")
802765
return
803766
}
@@ -817,7 +780,6 @@ func NewServices() {
817780
newService()
818781
newCacheService()
819782
newSessionService()
820-
newMailService()
821783
newRegisterMailService()
822784
newNotifyMailService()
823785
}
@@ -830,6 +792,5 @@ var HookMode bool
830792
func NewPostReceiveHookServices() {
831793
HookMode = true
832794
newService()
833-
newMailService()
834795
newNotifyMailService()
835796
}

internal/conf/static.go

+32
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,33 @@ var (
159159
EnableLoginStatusCookie bool
160160
LoginStatusCookieName string
161161
}
162+
163+
// Email settings
164+
Email struct {
165+
Enabled bool
166+
SubjectPrefix string
167+
Host string
168+
From string
169+
User string
170+
Password string
171+
172+
DisableHELO bool `ini:"DISABLE_HELO"`
173+
HELOHostname string `ini:"HELO_HOSTNAME"`
174+
175+
SkipVerify bool
176+
UseCertificate bool
177+
CertFile string
178+
KeyFile string
179+
180+
UsePlainText bool
181+
AddPlainTextAlt bool
182+
183+
// Derived from other static values
184+
FromEmail string `ini:"-"` // Parsed email address of From without person's name.
185+
186+
// Deprecated: Use Password instead, will be removed in 0.13.
187+
Passwd string
188+
}
162189
)
163190

164191
// handleDeprecated transfers deprecated values to the new ones when set.
@@ -185,4 +212,9 @@ func handleDeprecated() {
185212
Database.Password = Database.Passwd
186213
Database.Passwd = ""
187214
}
215+
216+
if Email.Passwd != "" {
217+
Email.Password = Email.Passwd
218+
Email.Passwd = ""
219+
}
188220
}

internal/db/issue_mail.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
log "unknwon.dev/clog/v2"
1212

1313
"github.com/G-Node/gogs/internal/conf"
14-
"github.com/G-Node/gogs/internal/mailer"
14+
"github.com/G-Node/gogs/internal/email"
1515
"github.com/G-Node/gogs/internal/markup"
1616
)
1717

@@ -44,7 +44,7 @@ func (this mailerUser) GenerateEmailActivateCode(email string) string {
4444
return this.user.GenerateEmailActivateCode(email)
4545
}
4646

47-
func NewMailerUser(u *User) mailer.User {
47+
func NewMailerUser(u *User) email.User {
4848
return mailerUser{u}
4949
}
5050

@@ -65,7 +65,7 @@ func (this mailerRepo) ComposeMetas() map[string]string {
6565
return this.repo.ComposeMetas()
6666
}
6767

68-
func NewMailerRepo(repo *Repository) mailer.Repository {
68+
func NewMailerRepo(repo *Repository) email.Repository {
6969
return mailerRepo{repo}
7070
}
7171

@@ -86,7 +86,7 @@ func (this mailerIssue) HTMLURL() string {
8686
return this.issue.HTMLURL()
8787
}
8888

89-
func NewMailerIssue(issue *Issue) mailer.Issue {
89+
func NewMailerIssue(issue *Issue) email.Issue {
9090
return mailerIssue{issue}
9191
}
9292

@@ -148,7 +148,7 @@ func mailIssueCommentToParticipants(issue *Issue, doer *User, mentions []string)
148148
names = append(names, issue.Assignee.Name)
149149
}
150150
}
151-
mailer.SendIssueCommentMail(NewMailerIssue(issue), NewMailerRepo(issue.Repo), NewMailerUser(doer), tos)
151+
email.SendIssueCommentMail(NewMailerIssue(issue), NewMailerRepo(issue.Repo), NewMailerUser(doer), tos)
152152

153153
// Mail mentioned people and exclude watchers.
154154
names = append(names, doer.Name)
@@ -160,7 +160,7 @@ func mailIssueCommentToParticipants(issue *Issue, doer *User, mentions []string)
160160

161161
tos = append(tos, mentions[i])
162162
}
163-
mailer.SendIssueMentionMail(NewMailerIssue(issue), NewMailerRepo(issue.Repo), NewMailerUser(doer), GetUserEmailsByNames(tos))
163+
email.SendIssueMentionMail(NewMailerIssue(issue), NewMailerRepo(issue.Repo), NewMailerUser(doer), GetUserEmailsByNames(tos))
164164
return nil
165165
}
166166

0 commit comments

Comments
 (0)