From 3d91bb2f2dc8584b76a49a1a40c3f688c21380f5 Mon Sep 17 00:00:00 2001
From: zeripath <art27@cantab.net>
Date: Sun, 3 Feb 2019 02:06:52 +0000
Subject: [PATCH] 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
---
 custom/conf/app.ini.sample                         |  4 ++--
 .../doc/advanced/config-cheat-sheet.en-us.md       |  6 +++++-
 integrations/mssql.ini.tmpl                        |  5 ++++-
 integrations/mysql.ini.tmpl                        |  5 ++++-
 integrations/pgsql.ini.tmpl                        |  5 ++++-
 integrations/sqlite.ini                            |  6 ++++++
 modules/mailer/mailer.go                           | 24 +++++++++++++++++++---
 modules/setting/setting.go                         | 17 ++++++++++++---
 8 files changed, 60 insertions(+), 12 deletions(-)

diff --git a/custom/conf/app.ini.sample b/custom/conf/app.ini.sample
index 6b46456f4e..aebcfafa96 100644
--- a/custom/conf/app.ini.sample
+++ b/custom/conf/app.ini.sample
@@ -414,8 +414,8 @@ USER =
 PASSWD =
 ; Send mails as plain text
 SEND_AS_PLAIN_TEXT = false
-; Enable sendmail (override SMTP)
-USE_SENDMAIL = false
+; Set Mailer Type (either SMTP, sendmail or dummy to just send to the log)
+MAILER_TYPE = smtp
 ; Specify an alternative sendmail binary
 SENDMAIL_PATH = sendmail
 ; Specify any extra sendmail arguments
diff --git a/docs/content/doc/advanced/config-cheat-sheet.en-us.md b/docs/content/doc/advanced/config-cheat-sheet.en-us.md
index 7d7f4d9e0a..c2d22f1d83 100644
--- a/docs/content/doc/advanced/config-cheat-sheet.en-us.md
+++ b/docs/content/doc/advanced/config-cheat-sheet.en-us.md
@@ -228,10 +228,14 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`.
 - `PASSWD`: **\<empty\>**: Password of mailing user.  Use \`your password\` for quoting if you use special characters in the password.
 - `SKIP_VERIFY`: **\<empty\>**: Do not verify the self-signed certificates.
    - **Note:** Gitea only supports SMTP with STARTTLS.
-- `USE_SENDMAIL`: **false** Use the operating system's `sendmail` command instead of SMTP.
+- `MAILER_TYPE`: **smtp**: \[smtp, sendmail, dummy\]
+   - **smtp** Use SMTP to send mail
+   - **sendmail** Use the operating system's `sendmail` command instead of SMTP.
    This is common on linux systems.
+   - **dummy** Send email messages to the log as a testing phase.
    - Note that enabling sendmail will ignore all other `mailer` settings except `ENABLED`,
      `FROM` and `SENDMAIL_PATH`.
+   - Enabling dummy will ignore all settings except `ENABLED` and `FROM`.
 - `SENDMAIL_PATH`: **sendmail**: The location of sendmail on the operating system (can be
    command or full path).
 - ``IS_TLS_ENABLED`` :  **false** : Decide if SMTP connections should use TLS.
diff --git a/integrations/mssql.ini.tmpl b/integrations/mssql.ini.tmpl
index 08f7e9ca37..86d100845d 100644
--- a/integrations/mssql.ini.tmpl
+++ b/integrations/mssql.ini.tmpl
@@ -36,7 +36,9 @@ LFS_JWT_SECRET   = Tv_MjmZuHqpIY6GFl12ebgkRAMt4RlWt0v4EHKSXO0w
 APP_DATA_PATH    = integrations/gitea-integration-mssql/data
 
 [mailer]
-ENABLED = false
+ENABLED = true
+MAILER_TYPE = dummy
+FROM = mssql-integration-test@gitea.io
 
 [service]
 REGISTER_EMAIL_CONFIRM            = false
@@ -47,6 +49,7 @@ REQUIRE_SIGNIN_VIEW               = false
 DEFAULT_KEEP_EMAIL_PRIVATE        = false
 DEFAULT_ALLOW_CREATE_ORGANIZATION = true
 NO_REPLY_ADDRESS                  = noreply.example.org
+ENABLE_NOTIFY_MAIL                = true
 
 [picture]
 DISABLE_GRAVATAR        = false
diff --git a/integrations/mysql.ini.tmpl b/integrations/mysql.ini.tmpl
index e75df81bd6..0ba34f2d86 100644
--- a/integrations/mysql.ini.tmpl
+++ b/integrations/mysql.ini.tmpl
@@ -36,7 +36,9 @@ LFS_JWT_SECRET   = Tv_MjmZuHqpIY6GFl12ebgkRAMt4RlWt0v4EHKSXO0w
 APP_DATA_PATH    = integrations/gitea-integration-mysql/data
 
 [mailer]
-ENABLED = false
+ENABLED = true
+MAILER_TYPE = dummy
+FROM = mysql-integration-test@gitea.io
 
 [service]
 REGISTER_EMAIL_CONFIRM            = false
@@ -47,6 +49,7 @@ REQUIRE_SIGNIN_VIEW               = false
 DEFAULT_KEEP_EMAIL_PRIVATE        = false
 DEFAULT_ALLOW_CREATE_ORGANIZATION = true
 NO_REPLY_ADDRESS                  = noreply.example.org
+ENABLE_NOTIFY_MAIL                = true
 
 [picture]
 DISABLE_GRAVATAR        = false
diff --git a/integrations/pgsql.ini.tmpl b/integrations/pgsql.ini.tmpl
index 923608235e..e617b1899c 100644
--- a/integrations/pgsql.ini.tmpl
+++ b/integrations/pgsql.ini.tmpl
@@ -36,7 +36,9 @@ LFS_JWT_SECRET   = Tv_MjmZuHqpIY6GFl12ebgkRAMt4RlWt0v4EHKSXO0w
 APP_DATA_PATH    = integrations/gitea-integration-pgsql/data
 
 [mailer]
-ENABLED = false
+ENABLED = true
+MAILER_TYPE = dummy
+FROM = pgsql-integration-test@gitea.io
 
 [service]
 REGISTER_EMAIL_CONFIRM            = false
@@ -47,6 +49,7 @@ REQUIRE_SIGNIN_VIEW               = false
 DEFAULT_KEEP_EMAIL_PRIVATE        = false
 DEFAULT_ALLOW_CREATE_ORGANIZATION = true
 NO_REPLY_ADDRESS                  = noreply.example.org
+ENABLE_NOTIFY_MAIL                = true
 
 [picture]
 DISABLE_GRAVATAR        = false
diff --git a/integrations/sqlite.ini b/integrations/sqlite.ini
index 33c1015eca..f1e48617ac 100644
--- a/integrations/sqlite.ini
+++ b/integrations/sqlite.ini
@@ -44,6 +44,7 @@ REQUIRE_SIGNIN_VIEW               = false
 DEFAULT_KEEP_EMAIL_PRIVATE        = false
 DEFAULT_ALLOW_CREATE_ORGANIZATION = true
 NO_REPLY_ADDRESS                  = noreply.example.org
+ENABLE_NOTIFY_MAIL                = true
 
 [picture]
 DISABLE_GRAVATAR        = false
@@ -66,3 +67,8 @@ LEVEL = Debug
 INSTALL_LOCK   = true
 SECRET_KEY     = 9pCviYTWSb
 INTERNAL_TOKEN = eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOjE0OTI3OTU5ODN9.OQkH5UmzID2XBdwQ9TAI6Jj2t1X-wElVTjbE7aoN4I8
+
+[mailer]
+ENABLED = true
+MAILER_TYPE = dummy
+FROM = sqlite-integration-test@gitea.io
diff --git a/modules/mailer/mailer.go b/modules/mailer/mailer.go
index e9b752e14d..6f4c9488d5 100644
--- a/modules/mailer/mailer.go
+++ b/modules/mailer/mailer.go
@@ -6,6 +6,7 @@
 package mailer
 
 import (
+	"bytes"
 	"crypto/tls"
 	"fmt"
 	"io"
@@ -237,6 +238,20 @@ func (s *sendmailSender) Send(from string, to []string, msg io.WriterTo) error {
 	}
 }
 
+// Sender sendmail mail sender
+type dummySender struct {
+}
+
+// Send send email
+func (s *dummySender) Send(from string, to []string, msg io.WriterTo) error {
+	buf := bytes.Buffer{}
+	if _, err := msg.WriteTo(&buf); err != nil {
+		return err
+	}
+	log.Info("Mail From: %s To: %v Body: %s", from, to, buf.String())
+	return nil
+}
+
 func processMailQueue() {
 	for {
 		select {
@@ -265,10 +280,13 @@ func NewContext() {
 		return
 	}
 
-	if setting.MailService.UseSendmail {
-		Sender = &sendmailSender{}
-	} else {
+	switch setting.MailService.MailerType {
+	case "smtp":
 		Sender = &smtpSender{}
+	case "sendmail":
+		Sender = &sendmailSender{}
+	case "dummy":
+		Sender = &dummySender{}
 	}
 
 	mailQueue = make(chan *Message, setting.MailService.QueueLength)
diff --git a/modules/setting/setting.go b/modules/setting/setting.go
index 4aca579617..a2fdbf5192 100644
--- a/modules/setting/setting.go
+++ b/modules/setting/setting.go
@@ -1529,6 +1529,7 @@ type Mailer struct {
 	FromName        string
 	FromEmail       string
 	SendAsPlainText bool
+	MailerType      string
 
 	// SMTP sender
 	Host              string
@@ -1541,7 +1542,6 @@ type Mailer struct {
 	IsTLSEnabled      bool
 
 	// Sendmail sender
-	UseSendmail  bool
 	SendmailPath string
 	SendmailArgs []string
 }
@@ -1562,6 +1562,7 @@ func newMailService() {
 		QueueLength:     sec.Key("SEND_BUFFER_LEN").MustInt(100),
 		Name:            sec.Key("NAME").MustString(AppName),
 		SendAsPlainText: sec.Key("SEND_AS_PLAIN_TEXT").MustBool(false),
+		MailerType:      sec.Key("MAILER_TYPE").In("", []string{"smtp", "sendmail", "dummy"}),
 
 		Host:           sec.Key("HOST").String(),
 		User:           sec.Key("USER").String(),
@@ -1574,7 +1575,6 @@ func newMailService() {
 		KeyFile:        sec.Key("KEY_FILE").String(),
 		IsTLSEnabled:   sec.Key("IS_TLS_ENABLED").MustBool(),
 
-		UseSendmail:  sec.Key("USE_SENDMAIL").MustBool(),
 		SendmailPath: sec.Key("SENDMAIL_PATH").MustString("sendmail"),
 	}
 	MailService.From = sec.Key("FROM").MustString(MailService.User)
@@ -1584,6 +1584,13 @@ func newMailService() {
 		MailService.SendAsPlainText = !sec.Key("ENABLE_HTML_ALTERNATIVE").MustBool(false)
 	}
 
+	if sec.HasKey("USE_SENDMAIL") {
+		log.Warn("USE_SENDMAIL is deprecated, use MAILER_TYPE=sendmail")
+		if MailService.MailerType == "" && sec.Key("USE_SENDMAIL").MustBool(false) {
+			MailService.MailerType = "sendmail"
+		}
+	}
+
 	parsed, err := mail.ParseAddress(MailService.From)
 	if err != nil {
 		log.Fatal(4, "Invalid mailer.FROM (%s): %v", MailService.From, err)
@@ -1591,7 +1598,11 @@ func newMailService() {
 	MailService.FromName = parsed.Name
 	MailService.FromEmail = parsed.Address
 
-	if MailService.UseSendmail {
+	if MailService.MailerType == "" {
+		MailService.MailerType = "smtp"
+	}
+
+	if MailService.MailerType == "sendmail" {
 		MailService.SendmailArgs, err = shellquote.Split(sec.Key("SENDMAIL_ARGS").String())
 		if err != nil {
 			log.Error(4, "Failed to parse Sendmail args: %v", CustomConf, err)