mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-10-26 12:27:06 +00:00 
			
		
		
		
	Allow HOST has no port (#22280)
Fix #22274 This PR will allow `HOST` without port. Then a default port will be given in future steps.
This commit is contained in:
		@@ -12,6 +12,7 @@ import (
 | 
			
		||||
	"code.gitea.io/gitea/modules/log"
 | 
			
		||||
 | 
			
		||||
	shellquote "github.com/kballard/go-shellquote"
 | 
			
		||||
	ini "gopkg.in/ini.v1"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Mailer represents mail service.
 | 
			
		||||
@@ -49,8 +50,8 @@ type Mailer struct {
 | 
			
		||||
// MailService the global mailer
 | 
			
		||||
var MailService *Mailer
 | 
			
		||||
 | 
			
		||||
func newMailService() {
 | 
			
		||||
	sec := Cfg.Section("mailer")
 | 
			
		||||
func parseMailerConfig(rootCfg *ini.File) {
 | 
			
		||||
	sec := rootCfg.Section("mailer")
 | 
			
		||||
	// Check mailer setting.
 | 
			
		||||
	if !sec.Key("ENABLED").MustBool() {
 | 
			
		||||
		return
 | 
			
		||||
@@ -70,9 +71,14 @@ func newMailService() {
 | 
			
		||||
	if sec.HasKey("HOST") && !sec.HasKey("SMTP_ADDR") {
 | 
			
		||||
		givenHost := sec.Key("HOST").String()
 | 
			
		||||
		addr, port, err := net.SplitHostPort(givenHost)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
		if err != nil && strings.Contains(err.Error(), "missing port in address") {
 | 
			
		||||
			addr = givenHost
 | 
			
		||||
		} else if err != nil {
 | 
			
		||||
			log.Fatal("Invalid mailer.HOST (%s): %v", givenHost, err)
 | 
			
		||||
		}
 | 
			
		||||
		if addr == "" {
 | 
			
		||||
			addr = "127.0.0.1"
 | 
			
		||||
		}
 | 
			
		||||
		sec.Key("SMTP_ADDR").MustString(addr)
 | 
			
		||||
		sec.Key("SMTP_PORT").MustString(port)
 | 
			
		||||
	}
 | 
			
		||||
@@ -172,6 +178,9 @@ func newMailService() {
 | 
			
		||||
			default:
 | 
			
		||||
				log.Error("unable to infer unspecified mailer.PROTOCOL from mailer.SMTP_PORT = %q, assume using smtps", MailService.SMTPPort)
 | 
			
		||||
				MailService.Protocol = "smtps"
 | 
			
		||||
				if MailService.SMTPPort == "" {
 | 
			
		||||
					MailService.SMTPPort = "465"
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user