Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

smtp: Fix STARTTLS, request HELO once TLS is established #15032

Merged
merged 1 commit into from Jul 22, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 9 additions & 3 deletions lib/pure/smtp.nim
Expand Up @@ -60,6 +60,7 @@ type

SmtpBase[SocketType] = ref object
sock: SocketType
address: string
debug: bool

Smtp* = SmtpBase[Socket]
Expand Down Expand Up @@ -225,15 +226,19 @@ proc checkReply*(smtp: Smtp | AsyncSmtp, reply: string) {.multisync.} =
if not line.startswith(reply):
await quitExcpt(smtp, "Expected " & reply & " reply, got: " & line)

proc helo*(smtp: Smtp | AsyncSmtp) {.multisync.} =
# Sends the HELO request
await smtp.debugSend("HELO " & smtp.address & "\c\L")
await smtp.checkReply("250")

proc connect*(smtp: Smtp | AsyncSmtp,
address: string, port: Port) {.multisync.} =
## Establishes a connection with a SMTP server.
## May fail with ReplyError or with a socket error.
smtp.address = address
await smtp.sock.connect(address, port)

await smtp.checkReply("220")
await smtp.debugSend("HELO " & address & "\c\L")
await smtp.checkReply("250")
await smtp.helo()

proc startTls*(smtp: Smtp | AsyncSmtp, sslContext: SSLContext = nil) {.multisync.} =
## Put the SMTP connection in TLS (Transport Layer Security) mode.
Expand All @@ -245,6 +250,7 @@ proc startTls*(smtp: Smtp | AsyncSmtp, sslContext: SSLContext = nil) {.multisync
getSSLContext().wrapConnectedSocket(smtp.sock, handshakeAsClient)
else:
sslContext.wrapConnectedSocket(smtp.sock, handshakeAsClient)
await smtp.helo()
else:
{.error: "SMTP module compiled without SSL support".}

Expand Down