Skip to content

Commit

Permalink
Add starttls for smtp (#11500)
Browse files Browse the repository at this point in the history
* Add starttls for smtp

* Update smtp.nim
  • Loading branch information
NamPNQ authored and dom96 committed Jun 16, 2019
1 parent 7182922 commit 9f6187b
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions lib/pure/smtp.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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``.

Expand Down Expand Up @@ -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`
Expand Down

0 comments on commit 9f6187b

Please sign in to comment.