Fix implicit TLS (SMTPS) handling on SMTP port 465#386
Open
RobinMeis wants to merge 1 commit into
Open
Conversation
TLSPolicy only negotiates STARTTLS, which never happens on port 465 since the server expects the TLS handshake immediately. Client code was blocking waiting for a plaintext greeting/STARTTLS response that the server would never send. Establish TLS upfront (or use WithSSL()) when UseTLS is set and the port is 465.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #385
Problem
Port 465 is implicit TLS (SMTPS): the server expects the TLS handshake immediately after the TCP connection is opened, with no plaintext greeting and no
STARTTLSnegotiation. The SMTP client code always configured aTLSPolicy(which only governsSTARTTLS), so on port 465 it ended up waiting for a plaintext response the server would never send. The connection would hang or fail instead of succeeding.Fix
When TLS is enabled and the port is
465, establish TLS immediately instead of relying onSTARTTLS:internal/service/setup_service.go(TestSMTPConnection): usemail.WithSSL()instead ofmail.WithTLSPolicy(...)whenUseTLS && Port == 465.pkg/mailer/mailer.go(SMTPMailer.createSMTPClient): same change, usingmail.WithSSL()for port 465.internal/service/smtp_service.go(sendRawEmailWithSettings): wrap the raw connection withtls.Client(conn, tlsConfig)and perform the handshake right after connecting whenUseTLS && Port == 465, and skip the subsequentSTARTTLScommand since the connection is already secured.All other ports/configurations (STARTTLS on 587, plaintext on 25, etc.) are unaffected. The existing
TLSPolicy/STARTTLSbehavior is preserved.Testing
TestSMTPConnectionand outgoing mail sending succeed against a port-465/SMTPS SMTP provider.