Skip to content

Commit

Permalink
Correct encryption types SSL/TLS and STARTTLS
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
xhit committed Apr 2, 2021
1 parent c1a7db7 commit ed81767
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func main() {
server.Port = 587
server.Username = "[email protected]"
server.Password = "examplepass"
server.Encryption = mail.EncryptionTLS
server.Encryption = mail.EncryptionSTARTTLS

// Since v2.3.0 you can specified authentication type:
// - PLAIN (default)
Expand Down
18 changes: 11 additions & 7 deletions email.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions example/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func TestWithTLS(t *testing.T) {
client.Port = 587
client.Username = "[email protected]"
client.Password = "asdfghh"
client.Encryption = mail.EncryptionTLS
client.Encryption = mail.EncryptionSTARTTLS
client.ConnectTimeout = 10 * time.Second
client.SendTimeout = 10 * time.Second

Expand Down Expand Up @@ -198,7 +198,7 @@ func TestWithSSL(t *testing.T) {
client.Port = 465
client.Username = "[email protected]"
client.Password = "asdfghh"
client.Encryption = mail.EncryptionSSL
client.Encryption = mail.EncryptionSSLTLS
client.ConnectTimeout = 10 * time.Second
client.SendTimeout = 10 * time.Second

Expand Down

0 comments on commit ed81767

Please sign in to comment.