summary refs log tree commit diff stats
path: root/lib/pure/smtp.nim
diff options
context:
space:
mode:
authorMildred Ki'Lya <mildred@users.noreply.github.com>2020-07-22 06:30:33 +0200
committerGitHub <noreply@github.com>2020-07-22 00:30:33 -0400
commit503c5e0520779af87dc6c42da7c9d1dbd2e8f8e1 (patch)
tree87074d54d2fa4f7ec13f81253f0b5885800a0b9a /lib/pure/smtp.nim
parent911e39351e34c1022b586fe9bfc3a67b2ce3bf44 (diff)
downloadNim-503c5e0520779af87dc6c42da7c9d1dbd2e8f8e1.tar.gz
smtp: Fix STARTTLS, request HELO once TLS is established (#15032)
Diffstat (limited to 'lib/pure/smtp.nim')
-rw-r--r--lib/pure/smtp.nim12
1 files changed, 9 insertions, 3 deletions
diff --git a/lib/pure/smtp.nim b/lib/pure/smtp.nim
index 7d426041d..b42664029 100644
--- a/lib/pure/smtp.nim
+++ b/lib/pure/smtp.nim
@@ -60,6 +60,7 @@ type
 
   SmtpBase[SocketType] = ref object
     sock: SocketType
+    address: string
     debug: bool
 
   Smtp* = SmtpBase[Socket]
@@ -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.
@@ -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".}