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

Implement dual-stack IPv4/IPv6 #7

Merged
merged 1 commit into from
Aug 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions src/smtp.nim
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type
sock: SocketType
address: string
debug: bool
sslContext: SslContext

Smtp* = SmtpBase[Socket]
AsyncSmtp* = SmtpBase[AsyncSocket]
Expand Down Expand Up @@ -179,27 +180,29 @@ proc newSmtp*(useSsl = false, debug = false, sslContext: SslContext = nil): Smtp
## Creates a new `Smtp` instance.
new result
result.debug = debug
result.sock = newSocket()
result.sock = nil
result.sslContext = nil
if useSsl:
when compiledWithSsl:
if sslContext == nil:
getSSLContext().wrapSocket(result.sock)
result.sslContext = getSSLContext()
else:
sslContext.wrapSocket(result.sock)
result.sslContext = sslContext
else:
{.error: "SMTP module compiled without SSL support".}

proc newAsyncSmtp*(useSsl = false, debug = false, sslContext: SslContext = nil): AsyncSmtp =
## Creates a new `AsyncSmtp` instance.
new result
result.debug = debug
result.sock = newAsyncSocket()
result.sock = nil
result.sslContext = nil
if useSsl:
when compiledWithSsl:
if sslContext == nil:
getSSLContext().wrapSocket(result.sock)
result.sslContext = getSSLContext()
else:
sslContext.wrapSocket(result.sock)
result.sslContext = sslContext
else:
{.error: "SMTP module compiled without SSL support".}

Expand Down Expand Up @@ -251,12 +254,25 @@ proc connect*(smtp: Smtp | AsyncSmtp,
## 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)
when smtp is Smtp:
smtp.sock = net.dial(address, port)
else:
smtp.sock = await asyncnet.dial(address, port)
if smtp.sslContext != nil:
smtp.sslContext.wrapSocket(smtp.sock)
await smtp.checkReply("220")
let speaksEsmtp = await smtp.ehlo()
if not speaksEsmtp:
await smtp.helo()

proc dial*(address: string, port: Port, useSsl = false, debug = false, sslContext: SslContext = nil): Smtp =
result = newSmtp(useSsl, debug, sslContext)
result.connect(address, port)

proc dialAsync*(address: string, port: Port, useSsl = false, debug = false, sslContext: SslContext = nil): Future[AsyncSmtp] {.async.} =
result = newAsyncSmtp(useSsl, debug, sslContext)
await result.connect(address, port)

proc startTls*(smtp: Smtp | AsyncSmtp, sslContext: SslContext = nil) {.multisync.} =
## Put the SMTP connection in TLS (Transport Layer Security) mode.
## May fail with ReplyError
Expand Down Expand Up @@ -314,4 +330,4 @@ proc sendMail*(smtp: Smtp | AsyncSmtp, fromAddr: string,
proc close*(smtp: Smtp | AsyncSmtp) {.multisync.} =
## Disconnects from the SMTP server and closes the socket.
await smtp.debugSend("QUIT\c\L")
smtp.sock.close()
if smtp.sock != nil: smtp.sock.close()