1
0
mirror of https://github.com/axllent/mailpit.git synced 2025-08-15 20:13:16 +02:00

Fix: Replace invalid Windows username characters in sendmail (#294)

This commit is contained in:
Ralph Slooten
2024-05-15 16:09:36 +12:00
parent 86e8a126ca
commit 3c36951113

View File

@@ -21,6 +21,7 @@ import (
"net/smtp" "net/smtp"
"os" "os"
"os/user" "os/user"
"regexp"
"strings" "strings"
"github.com/axllent/mailpit/config" "github.com/axllent/mailpit/config"
@@ -42,15 +43,19 @@ var (
) )
func init() { func init() {
// ensure only valid characters are used, ie: windows
re := regexp.MustCompile(`[^a-zA-Z\-\.\_]`)
host, err := os.Hostname() host, err := os.Hostname()
if err != nil { if err != nil {
host = "localhost" host = "localhost"
} else {
host = re.ReplaceAllString(host, "-")
} }
username := "nobody" username := "nobody"
user, err := user.Current() user, err := user.Current()
if err == nil && user != nil && len(user.Username) > 0 { if err == nil && user != nil && len(user.Username) > 0 {
username = user.Username username = re.ReplaceAllString(user.Username, "-")
} }
if FromAddr == "" { if FromAddr == "" {
@@ -62,7 +67,7 @@ func init() {
func Run() { func Run() {
var recipients []string var recipients []string
// defaults from envars if provided // defaults from env vars if provided
if len(os.Getenv("MP_SENDMAIL_SMTP_ADDR")) > 0 { if len(os.Getenv("MP_SENDMAIL_SMTP_ADDR")) > 0 {
SMTPAddr = os.Getenv("MP_SENDMAIL_SMTP_ADDR") SMTPAddr = os.Getenv("MP_SENDMAIL_SMTP_ADDR")
} }