Skip to content

Commit 3d91bb2

Browse files
zeripathtechknowlogick
authored andcommitted
Make log mailer for testing (#5893)
* Create log mailer for testing email settings Signed-off-by: Andrew Thornton <art27@cantab.net> * Switch on the log mailer for the integration tests This ensures that the sending mail process works Signed-off-by: Andrew Thornton <art27@cantab.net> * rename the from user for mysql/mssql * rename log sender to dummy sender * update the integration tests
1 parent 67567ef commit 3d91bb2

File tree

8 files changed

+60
-12
lines changed

8 files changed

+60
-12
lines changed

custom/conf/app.ini.sample

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,8 +414,8 @@ USER =
414414
PASSWD =
415415
; Send mails as plain text
416416
SEND_AS_PLAIN_TEXT = false
417-
; Enable sendmail (override SMTP)
418-
USE_SENDMAIL = false
417+
; Set Mailer Type (either SMTP, sendmail or dummy to just send to the log)
418+
MAILER_TYPE = smtp
419419
; Specify an alternative sendmail binary
420420
SENDMAIL_PATH = sendmail
421421
; Specify any extra sendmail arguments

docs/content/doc/advanced/config-cheat-sheet.en-us.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,14 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`.
228228
- `PASSWD`: **\<empty\>**: Password of mailing user. Use \`your password\` for quoting if you use special characters in the password.
229229
- `SKIP_VERIFY`: **\<empty\>**: Do not verify the self-signed certificates.
230230
- **Note:** Gitea only supports SMTP with STARTTLS.
231-
- `USE_SENDMAIL`: **false** Use the operating system's `sendmail` command instead of SMTP.
231+
- `MAILER_TYPE`: **smtp**: \[smtp, sendmail, dummy\]
232+
- **smtp** Use SMTP to send mail
233+
- **sendmail** Use the operating system's `sendmail` command instead of SMTP.
232234
This is common on linux systems.
235+
- **dummy** Send email messages to the log as a testing phase.
233236
- Note that enabling sendmail will ignore all other `mailer` settings except `ENABLED`,
234237
`FROM` and `SENDMAIL_PATH`.
238+
- Enabling dummy will ignore all settings except `ENABLED` and `FROM`.
235239
- `SENDMAIL_PATH`: **sendmail**: The location of sendmail on the operating system (can be
236240
command or full path).
237241
- ``IS_TLS_ENABLED`` : **false** : Decide if SMTP connections should use TLS.

integrations/mssql.ini.tmpl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ LFS_JWT_SECRET = Tv_MjmZuHqpIY6GFl12ebgkRAMt4RlWt0v4EHKSXO0w
3636
APP_DATA_PATH = integrations/gitea-integration-mssql/data
3737

3838
[mailer]
39-
ENABLED = false
39+
ENABLED = true
40+
MAILER_TYPE = dummy
41+
FROM = mssql-integration-test@gitea.io
4042

4143
[service]
4244
REGISTER_EMAIL_CONFIRM = false
@@ -47,6 +49,7 @@ REQUIRE_SIGNIN_VIEW = false
4749
DEFAULT_KEEP_EMAIL_PRIVATE = false
4850
DEFAULT_ALLOW_CREATE_ORGANIZATION = true
4951
NO_REPLY_ADDRESS = noreply.example.org
52+
ENABLE_NOTIFY_MAIL = true
5053

5154
[picture]
5255
DISABLE_GRAVATAR = false

integrations/mysql.ini.tmpl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ LFS_JWT_SECRET = Tv_MjmZuHqpIY6GFl12ebgkRAMt4RlWt0v4EHKSXO0w
3636
APP_DATA_PATH = integrations/gitea-integration-mysql/data
3737

3838
[mailer]
39-
ENABLED = false
39+
ENABLED = true
40+
MAILER_TYPE = dummy
41+
FROM = mysql-integration-test@gitea.io
4042

4143
[service]
4244
REGISTER_EMAIL_CONFIRM = false
@@ -47,6 +49,7 @@ REQUIRE_SIGNIN_VIEW = false
4749
DEFAULT_KEEP_EMAIL_PRIVATE = false
4850
DEFAULT_ALLOW_CREATE_ORGANIZATION = true
4951
NO_REPLY_ADDRESS = noreply.example.org
52+
ENABLE_NOTIFY_MAIL = true
5053

5154
[picture]
5255
DISABLE_GRAVATAR = false

integrations/pgsql.ini.tmpl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ LFS_JWT_SECRET = Tv_MjmZuHqpIY6GFl12ebgkRAMt4RlWt0v4EHKSXO0w
3636
APP_DATA_PATH = integrations/gitea-integration-pgsql/data
3737

3838
[mailer]
39-
ENABLED = false
39+
ENABLED = true
40+
MAILER_TYPE = dummy
41+
FROM = pgsql-integration-test@gitea.io
4042

4143
[service]
4244
REGISTER_EMAIL_CONFIRM = false
@@ -47,6 +49,7 @@ REQUIRE_SIGNIN_VIEW = false
4749
DEFAULT_KEEP_EMAIL_PRIVATE = false
4850
DEFAULT_ALLOW_CREATE_ORGANIZATION = true
4951
NO_REPLY_ADDRESS = noreply.example.org
52+
ENABLE_NOTIFY_MAIL = true
5053

5154
[picture]
5255
DISABLE_GRAVATAR = false

integrations/sqlite.ini

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ REQUIRE_SIGNIN_VIEW = false
4444
DEFAULT_KEEP_EMAIL_PRIVATE = false
4545
DEFAULT_ALLOW_CREATE_ORGANIZATION = true
4646
NO_REPLY_ADDRESS = noreply.example.org
47+
ENABLE_NOTIFY_MAIL = true
4748

4849
[picture]
4950
DISABLE_GRAVATAR = false
@@ -66,3 +67,8 @@ LEVEL = Debug
6667
INSTALL_LOCK = true
6768
SECRET_KEY = 9pCviYTWSb
6869
INTERNAL_TOKEN = eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOjE0OTI3OTU5ODN9.OQkH5UmzID2XBdwQ9TAI6Jj2t1X-wElVTjbE7aoN4I8
70+
71+
[mailer]
72+
ENABLED = true
73+
MAILER_TYPE = dummy
74+
FROM = sqlite-integration-test@gitea.io

modules/mailer/mailer.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package mailer
77

88
import (
9+
"bytes"
910
"crypto/tls"
1011
"fmt"
1112
"io"
@@ -237,6 +238,20 @@ func (s *sendmailSender) Send(from string, to []string, msg io.WriterTo) error {
237238
}
238239
}
239240

241+
// Sender sendmail mail sender
242+
type dummySender struct {
243+
}
244+
245+
// Send send email
246+
func (s *dummySender) Send(from string, to []string, msg io.WriterTo) error {
247+
buf := bytes.Buffer{}
248+
if _, err := msg.WriteTo(&buf); err != nil {
249+
return err
250+
}
251+
log.Info("Mail From: %s To: %v Body: %s", from, to, buf.String())
252+
return nil
253+
}
254+
240255
func processMailQueue() {
241256
for {
242257
select {
@@ -265,10 +280,13 @@ func NewContext() {
265280
return
266281
}
267282

268-
if setting.MailService.UseSendmail {
269-
Sender = &sendmailSender{}
270-
} else {
283+
switch setting.MailService.MailerType {
284+
case "smtp":
271285
Sender = &smtpSender{}
286+
case "sendmail":
287+
Sender = &sendmailSender{}
288+
case "dummy":
289+
Sender = &dummySender{}
272290
}
273291

274292
mailQueue = make(chan *Message, setting.MailService.QueueLength)

modules/setting/setting.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1529,6 +1529,7 @@ type Mailer struct {
15291529
FromName string
15301530
FromEmail string
15311531
SendAsPlainText bool
1532+
MailerType string
15321533

15331534
// SMTP sender
15341535
Host string
@@ -1541,7 +1542,6 @@ type Mailer struct {
15411542
IsTLSEnabled bool
15421543

15431544
// Sendmail sender
1544-
UseSendmail bool
15451545
SendmailPath string
15461546
SendmailArgs []string
15471547
}
@@ -1562,6 +1562,7 @@ func newMailService() {
15621562
QueueLength: sec.Key("SEND_BUFFER_LEN").MustInt(100),
15631563
Name: sec.Key("NAME").MustString(AppName),
15641564
SendAsPlainText: sec.Key("SEND_AS_PLAIN_TEXT").MustBool(false),
1565+
MailerType: sec.Key("MAILER_TYPE").In("", []string{"smtp", "sendmail", "dummy"}),
15651566

15661567
Host: sec.Key("HOST").String(),
15671568
User: sec.Key("USER").String(),
@@ -1574,7 +1575,6 @@ func newMailService() {
15741575
KeyFile: sec.Key("KEY_FILE").String(),
15751576
IsTLSEnabled: sec.Key("IS_TLS_ENABLED").MustBool(),
15761577

1577-
UseSendmail: sec.Key("USE_SENDMAIL").MustBool(),
15781578
SendmailPath: sec.Key("SENDMAIL_PATH").MustString("sendmail"),
15791579
}
15801580
MailService.From = sec.Key("FROM").MustString(MailService.User)
@@ -1584,14 +1584,25 @@ func newMailService() {
15841584
MailService.SendAsPlainText = !sec.Key("ENABLE_HTML_ALTERNATIVE").MustBool(false)
15851585
}
15861586

1587+
if sec.HasKey("USE_SENDMAIL") {
1588+
log.Warn("USE_SENDMAIL is deprecated, use MAILER_TYPE=sendmail")
1589+
if MailService.MailerType == "" && sec.Key("USE_SENDMAIL").MustBool(false) {
1590+
MailService.MailerType = "sendmail"
1591+
}
1592+
}
1593+
15871594
parsed, err := mail.ParseAddress(MailService.From)
15881595
if err != nil {
15891596
log.Fatal(4, "Invalid mailer.FROM (%s): %v", MailService.From, err)
15901597
}
15911598
MailService.FromName = parsed.Name
15921599
MailService.FromEmail = parsed.Address
15931600

1594-
if MailService.UseSendmail {
1601+
if MailService.MailerType == "" {
1602+
MailService.MailerType = "smtp"
1603+
}
1604+
1605+
if MailService.MailerType == "sendmail" {
15951606
MailService.SendmailArgs, err = shellquote.Split(sec.Key("SENDMAIL_ARGS").String())
15961607
if err != nil {
15971608
log.Error(4, "Failed to parse Sendmail args: %v", CustomConf, err)

0 commit comments

Comments
 (0)