Browse Source

Add pool.SetHelloHostname() to override SMTP HELLO hostname (FQDN) (#101)

Kailash Nadh 5 years ago
parent
commit
8eef2508c3
1 changed files with 25 additions and 10 deletions
  1. 25 10
      pool.go

+ 25 - 10
pool.go

@@ -14,16 +14,17 @@ import (
 )
 
 type Pool struct {
-	addr         string
-	auth         smtp.Auth
-	max          int
-	created      int
-	clients      chan *client
-	rebuild      chan struct{}
-	mut          *sync.Mutex
-	lastBuildErr *timestampedErr
-	closing      chan struct{}
-	tlsConfig    *tls.Config
+	addr          string
+	auth          smtp.Auth
+	max           int
+	created       int
+	clients       chan *client
+	rebuild       chan struct{}
+	mut           *sync.Mutex
+	lastBuildErr  *timestampedErr
+	closing       chan struct{}
+	tlsConfig     *tls.Config
+	helloHostname string
 }
 
 type client struct {
@@ -68,6 +69,14 @@ func (c *client) Close() error {
 	return c.Text.Close()
 }
 
+// SetHelloHostname optionally sets the hostname that the Go smtp.Client will
+// use when doing a HELLO with the upstream SMTP server. By default, Go uses
+// "localhost" which may not be accepted by certain SMTP servers that demand
+// an FQDN.
+func (p *Pool) SetHelloHostname(h string) {
+	p.helloHostname = h
+}
+
 func (p *Pool) get(timeout time.Duration) *client {
 	select {
 	case c := <-p.clients:
@@ -200,6 +209,12 @@ func (p *Pool) build() (*client, error) {
 	if err != nil {
 		return nil, err
 	}
+
+	// Is there a custom hostname for doing a HELLO with the SMTP server?
+	if p.helloHostname != "" {
+		cl.Hello(p.helloHostname)
+	}
+
 	c := &client{cl, 0}
 
 	if _, err := startTLS(c, p.tlsConfig); err != nil {