Fall back to os.Hostname() when SMTP_HELLO_NAME is not set

Using localhost as the EHLO identity is the Go net/smtp default but is
rejected by strict relays such as Google Workspace. os.Hostname() is
the conventional SMTP client fallback and returns the actual machine or
pod name in production environments.

Signed-off-by: Bryan Frimin <bryan@probo.com>
This commit is contained in:
Bryan Frimin
2026-06-05 07:27:36 +02:00
parent 552246b5d3
commit 1998052a0d

View File

@@ -22,6 +22,7 @@ import (
"fmt"
"net"
"net/smtp"
"os"
"time"
"github.com/jhillyerd/enmime"
@@ -320,12 +321,17 @@ func (h *sendingHandler) sendMail(ctx context.Context, to []string, msg []byte)
defer func() { _ = c.Quit() }()
if h.smtp.HelloName != "" {
if err := c.Hello(h.smtp.HelloName); err != nil {
return fmt.Errorf("SMTP EHLO error: %w", err)
helloName := h.smtp.HelloName
if helloName == "" {
if helloName, err = os.Hostname(); err != nil {
helloName = "localhost"
}
}
if err := c.Hello(helloName); err != nil {
return fmt.Errorf("SMTP EHLO error: %w", err)
}
if h.smtp.TLSRequired {
if err := c.StartTLS(&tls.Config{ServerName: host}); err != nil {
return fmt.Errorf("TLS negotiation error: %w", err)