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:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user