From ed81767804164afe8ca92e6ee2f9a6b88fc61be3 Mon Sep 17 00:00:00 2001 From: Santiago De la Cruz Date: Fri, 2 Apr 2021 12:20:33 -0400 Subject: [PATCH] Correct encryption types SSL/TLS and STARTTLS Previously EncryptionTLS correspond to STARTTLS and EncryptionSSL correspond to SSL/TLS. To avoid confusion in names, two new const were created: - EncryptionSTARTTLS for STARTTLS - EncryptionSSLTLS for SSL/TLS EncryptionTLS and EncryptionSSL are deprecated and will be removed in future. --- README.md | 2 +- email.go | 18 +++++++++++------- example/example_test.go | 4 ++-- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 8cc2894..8a84591 100644 --- a/README.md +++ b/README.md @@ -116,7 +116,7 @@ func main() { server.Port = 587 server.Username = "test@example.com" server.Password = "examplepass" - server.Encryption = mail.EncryptionTLS + server.Encryption = mail.EncryptionSTARTTLS // Since v2.3.0 you can specified authentication type: // - PLAIN (default) diff --git a/email.go b/email.go index aa0bcdd..9b2bc19 100644 --- a/email.go +++ b/email.go @@ -77,13 +77,17 @@ type Encryption int const ( // EncryptionNone uses no encryption when sending email EncryptionNone Encryption = iota - // EncryptionSSL sets encryption type to SSL/TLS when sending email + // EncryptionSSL: DEPRECATED. Use EncryptionSSLTLS. Sets encryption type to SSL/TLS when sending email EncryptionSSL - // EncryptionTLS sets encryption type to STARTTLS when sending email + // EncryptionTLS: DEPRECATED. Use EncryptionSTARTTLS. sets encryption type to STARTTLS when sending email EncryptionTLS + // EncryptionSSLTLS sets encryption type to SSL/TLS when sending email + EncryptionSSLTLS + // EncryptionSTARTTLS sets encryption type to STARTTLS when sending email + EncryptionSTARTTLS ) -var encryptionTypes = [...]string{"None", "SSL/TLS", "STARTTLS"} +var encryptionTypes = [...]string{"None", "SSL/TLS", "STARTTLS", "SSL/TLS", "STARTTLS"} func (encryption Encryption) String() string { return encryptionTypes[encryption] @@ -775,7 +779,7 @@ func dial(host string, port string, encryption Encryption, config *tls.Config) ( // do the actual dial switch encryption { - case EncryptionSSL: + case EncryptionSSL, EncryptionSSLTLS: conn, err = tls.Dial("tcp", address, config) default: conn, err = net.Dial("tcp", address) @@ -814,12 +818,12 @@ func smtpConnect(host, port, helo string, a auth, encryption Encryption, config return nil, fmt.Errorf("Mail Error on Hello: %w", err) } - // start TLS if necessary - if encryption == EncryptionTLS { + // STARTTLS if necessary + if encryption == EncryptionTLS || encryption == EncryptionSTARTTLS { if ok, _ := c.extension("STARTTLS"); ok { if err = c.startTLS(config); err != nil { c.close() - return nil, fmt.Errorf("Mail Error on Start TLS: %w", err) + return nil, fmt.Errorf("Mail Error on STARTTLS: %w", err) } } } diff --git a/example/example_test.go b/example/example_test.go index 486f6a2..6d67c31 100644 --- a/example/example_test.go +++ b/example/example_test.go @@ -170,7 +170,7 @@ func TestWithTLS(t *testing.T) { client.Port = 587 client.Username = "aaa@gmail.com" client.Password = "asdfghh" - client.Encryption = mail.EncryptionTLS + client.Encryption = mail.EncryptionSTARTTLS client.ConnectTimeout = 10 * time.Second client.SendTimeout = 10 * time.Second @@ -198,7 +198,7 @@ func TestWithSSL(t *testing.T) { client.Port = 465 client.Username = "aaa@gmail.com" client.Password = "asdfghh" - client.Encryption = mail.EncryptionSSL + client.Encryption = mail.EncryptionSSLTLS client.ConnectTimeout = 10 * time.Second client.SendTimeout = 10 * time.Second