summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorPhạm Ngọc Quang Nam <NamPNQ@users.noreply.github.com>2019-06-17 05:50:56 +0700
committerDominik Picheta <dominikpicheta@googlemail.com>2019-06-16 23:50:56 +0100
commit9f6187b46caf28eab83c6817306cbf813b063a03 (patch)
treec3811691cf4c63bd74763983cc713c132f844563 /lib
parent71829226229287f4e1d38c9e4e91fb3d8b8fb648 (diff)
downloadNim-9f6187b46caf28eab83c6817306cbf813b063a03.tar.gz
Add starttls for smtp (#11500)
* Add starttls for smtp

* Update smtp.nim
Diffstat (limited to 'lib')
-rw-r--r--lib/pure/smtp.nim27
1 files changed, 27 insertions, 0 deletions
diff --git a/lib/pure/smtp.nim b/lib/pure/smtp.nim
index 5f4b09f80..1e621a9e2 100644
--- a/lib/pure/smtp.nim
+++ b/lib/pure/smtp.nim
@@ -26,6 +26,20 @@
 ##   smtpConn.sendmail("username@gmail.com", @["foo@gmail.com"], $msg)
 ##
 ##
+## Example for starttls use:
+##
+##
+## .. code-block:: Nim
+##   var msg = createMessage("Hello from Nim's SMTP",
+##                           "Hello!.\n Is this awesome or what?",
+##                           @["foo@gmail.com"])
+##   let smtpConn = newSmtp(debug=true)
+##   smtpConn.connect("smtp.mailtrap.io", Port 2525)
+##   smtpConn.starttls()
+##   smtpConn.auth("username", "password")
+##   smtpConn.sendmail("username@gmail.com", @["foo@gmail.com"], $msg)
+##
+##
 ## For SSL support this module relies on OpenSSL. If you want to
 ## enable SSL, compile with ``-d:ssl``.
 
@@ -167,6 +181,19 @@ proc connect*(smtp: Smtp | AsyncSmtp,
   await smtp.checkReply("220")
   await smtp.debugSend("HELO " & address & "\c\L")
   await smtp.checkReply("250")
+  
+proc starttls*(smtp: Smtp | AsyncSmtp, sslContext: SSLContext = nil) {.multisync.} =
+  ## Put the SMTP connection in TLS (Transport Layer Security) mode.
+  ## May fail with ReplyError
+  await smtp.debugSend("STARTTLS\c\L")
+  await smtp.checkReply("220")
+  when compiledWithSsl:
+    if sslContext == nil:
+      getSSLContext().wrapConnectedSocket(smtp.sock, handshakeAsClient)
+    else:
+      sslContext.wrapConnectedSocket(smtp.sock, handshakeAsClient)
+  else:
+    {.error: "SMTP module compiled without SSL support".}
 
 proc auth*(smtp: Smtp | AsyncSmtp, username, password: string) {.multisync.} =
   ## Sends an AUTH command to the server to login as the `username`