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

Add starttls for smtp #11500

Merged
merged 2 commits into from
Jun 16, 2019
Merged
Changes from 1 commit
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
25 changes: 25 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,17 @@ 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.} =
NamPNQ marked this conversation as resolved.
Show resolved Hide resolved
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