SMTP sync/async deduplication
Deduplicate synchronous and asynchronous code with the multisync pragma. Pass address and port at connect rather than ``new(Async)Smtp``.
This commit is contained in:
parent
7dc8dcb581
commit
fecad72e02
1 changed files with 58 additions and 114 deletions
|
|
@ -20,7 +20,8 @@
|
||||||
## var msg = createMessage("Hello from Nim's SMTP",
|
## var msg = createMessage("Hello from Nim's SMTP",
|
||||||
## "Hello!.\n Is this awesome or what?",
|
## "Hello!.\n Is this awesome or what?",
|
||||||
## @["foo@gmail.com"])
|
## @["foo@gmail.com"])
|
||||||
## var smtpConn = connect("smtp.gmail.com", Port 465, true, true)
|
## let smtpConn = newSmtp(useSsl = true, debug=true)
|
||||||
|
## smtpConn.connect("smtp.gmail.com", Port 465)
|
||||||
## smtpConn.auth("username", "password")
|
## smtpConn.auth("username", "password")
|
||||||
## smtpConn.sendmail("username@gmail.com", @["foo@gmail.com"], $msg)
|
## smtpConn.sendmail("username@gmail.com", @["foo@gmail.com"], $msg)
|
||||||
##
|
##
|
||||||
|
|
@ -34,10 +35,6 @@ import asyncnet, asyncdispatch
|
||||||
export Port
|
export Port
|
||||||
|
|
||||||
type
|
type
|
||||||
Smtp* = object
|
|
||||||
sock: Socket
|
|
||||||
debug: bool
|
|
||||||
|
|
||||||
Message* = object
|
Message* = object
|
||||||
msgTo: seq[string]
|
msgTo: seq[string]
|
||||||
msgCc: seq[string]
|
msgCc: seq[string]
|
||||||
|
|
@ -47,37 +44,29 @@ type
|
||||||
|
|
||||||
ReplyError* = object of IOError
|
ReplyError* = object of IOError
|
||||||
|
|
||||||
AsyncSmtp* = ref object
|
SmtpBase[SocketType] = ref object
|
||||||
sock: AsyncSocket
|
sock: SocketType
|
||||||
address: string
|
|
||||||
port: Port
|
|
||||||
useSsl: bool
|
|
||||||
debug: bool
|
debug: bool
|
||||||
|
|
||||||
|
Smtp* = SmtpBase[Socket]
|
||||||
|
AsyncSmtp* = SmtpBase[AsyncSocket]
|
||||||
|
|
||||||
{.deprecated: [EInvalidReply: ReplyError, TMessage: Message, TSMTP: Smtp].}
|
{.deprecated: [EInvalidReply: ReplyError, TMessage: Message, TSMTP: Smtp].}
|
||||||
|
|
||||||
proc debugSend(smtp: Smtp, cmd: string) =
|
proc debugSend(smtp: Smtp | AsyncSmtp, cmd: string) {.multisync.} =
|
||||||
if smtp.debug:
|
if smtp.debug:
|
||||||
echo("C:" & cmd)
|
echo("C:" & cmd)
|
||||||
smtp.sock.send(cmd)
|
await smtp.sock.send(cmd)
|
||||||
|
|
||||||
proc debugRecv(smtp: var Smtp): TaintedString =
|
|
||||||
var line = TaintedString""
|
|
||||||
smtp.sock.readLine(line)
|
|
||||||
|
|
||||||
|
proc debugRecv(smtp: Smtp | AsyncSmtp): Future[TaintedString] {.multisync.} =
|
||||||
|
result = await smtp.sock.recvLine()
|
||||||
if smtp.debug:
|
if smtp.debug:
|
||||||
echo("S:" & line.string)
|
echo("S:" & result.string)
|
||||||
return line
|
|
||||||
|
|
||||||
proc quitExcpt(smtp: Smtp, msg: string) =
|
proc quitExcpt(smtp: Smtp, msg: string) =
|
||||||
smtp.debugSend("QUIT")
|
smtp.debugSend("QUIT")
|
||||||
raise newException(ReplyError, msg)
|
raise newException(ReplyError, msg)
|
||||||
|
|
||||||
proc checkReply(smtp: var Smtp, reply: string) =
|
|
||||||
var line = smtp.debugRecv()
|
|
||||||
if not line.string.startswith(reply):
|
|
||||||
quitExcpt(smtp, "Expected " & reply & " reply, got: " & line.string)
|
|
||||||
|
|
||||||
const compiledWithSsl = defined(ssl)
|
const compiledWithSsl = defined(ssl)
|
||||||
|
|
||||||
when not defined(ssl):
|
when not defined(ssl):
|
||||||
|
|
@ -86,63 +75,6 @@ when not defined(ssl):
|
||||||
else:
|
else:
|
||||||
let defaultSSLContext = newContext(verifyMode = CVerifyNone)
|
let defaultSSLContext = newContext(verifyMode = CVerifyNone)
|
||||||
|
|
||||||
proc connect*(address: string, port = Port(25),
|
|
||||||
ssl = false, debug = false,
|
|
||||||
sslContext = defaultSSLContext): Smtp =
|
|
||||||
## Establishes a connection with a SMTP server.
|
|
||||||
## May fail with ReplyError or with a socket error.
|
|
||||||
result.sock = newSocket()
|
|
||||||
if ssl:
|
|
||||||
when compiledWithSsl:
|
|
||||||
sslContext.wrapSocket(result.sock)
|
|
||||||
else:
|
|
||||||
raise newException(ESystem,
|
|
||||||
"SMTP module compiled without SSL support")
|
|
||||||
result.sock.connect(address, port)
|
|
||||||
result.debug = debug
|
|
||||||
|
|
||||||
result.checkReply("220")
|
|
||||||
result.debugSend("HELO " & address & "\c\L")
|
|
||||||
result.checkReply("250")
|
|
||||||
|
|
||||||
proc auth*(smtp: var Smtp, username, password: string) =
|
|
||||||
## Sends an AUTH command to the server to login as the `username`
|
|
||||||
## using `password`.
|
|
||||||
## May fail with ReplyError.
|
|
||||||
|
|
||||||
smtp.debugSend("AUTH LOGIN\c\L")
|
|
||||||
smtp.checkReply("334") # TODO: Check whether it's asking for the "Username:"
|
|
||||||
# i.e "334 VXNlcm5hbWU6"
|
|
||||||
smtp.debugSend(encode(username) & "\c\L")
|
|
||||||
smtp.checkReply("334") # TODO: Same as above, only "Password:" (I think?)
|
|
||||||
|
|
||||||
smtp.debugSend(encode(password) & "\c\L")
|
|
||||||
smtp.checkReply("235") # Check whether the authentification was successful.
|
|
||||||
|
|
||||||
proc sendmail*(smtp: var Smtp, fromaddr: string,
|
|
||||||
toaddrs: seq[string], msg: string) =
|
|
||||||
## Sends `msg` from `fromaddr` to `toaddr`.
|
|
||||||
## Messages may be formed using ``createMessage`` by converting the
|
|
||||||
## Message into a string.
|
|
||||||
|
|
||||||
smtp.debugSend("MAIL FROM:<" & fromaddr & ">\c\L")
|
|
||||||
smtp.checkReply("250")
|
|
||||||
for address in items(toaddrs):
|
|
||||||
smtp.debugSend("RCPT TO:<" & address & ">\c\L")
|
|
||||||
smtp.checkReply("250")
|
|
||||||
|
|
||||||
# Send the message
|
|
||||||
smtp.debugSend("DATA " & "\c\L")
|
|
||||||
smtp.checkReply("354")
|
|
||||||
smtp.debugSend(msg & "\c\L")
|
|
||||||
smtp.debugSend(".\c\L")
|
|
||||||
smtp.checkReply("250")
|
|
||||||
|
|
||||||
proc close*(smtp: Smtp) =
|
|
||||||
## Disconnects from the SMTP server and closes the socket.
|
|
||||||
smtp.debugSend("QUIT\c\L")
|
|
||||||
smtp.sock.close()
|
|
||||||
|
|
||||||
proc createMessage*(mSubject, mBody: string, mTo, mCc: seq[string],
|
proc createMessage*(mSubject, mBody: string, mTo, mCc: seq[string],
|
||||||
otherHeaders: openarray[tuple[name, value: string]]): Message =
|
otherHeaders: openarray[tuple[name, value: string]]): Message =
|
||||||
## Creates a new MIME compliant message.
|
## Creates a new MIME compliant message.
|
||||||
|
|
@ -178,81 +110,94 @@ proc `$`*(msg: Message): string =
|
||||||
result.add("\c\L")
|
result.add("\c\L")
|
||||||
result.add(msg.msgBody)
|
result.add(msg.msgBody)
|
||||||
|
|
||||||
proc newAsyncSmtp*(address: string, port: Port, useSsl = false,
|
proc newSmtp*(useSsl = false, debug=false,
|
||||||
|
sslContext = defaultSslContext): Smtp =
|
||||||
|
## Creates a new ``Smtp`` instance.
|
||||||
|
new result
|
||||||
|
result.debug = debug
|
||||||
|
|
||||||
|
result.sock = newSocket()
|
||||||
|
if useSsl:
|
||||||
|
when compiledWithSsl:
|
||||||
|
sslContext.wrapSocket(result.sock)
|
||||||
|
else:
|
||||||
|
raise newException(SystemError,
|
||||||
|
"SMTP module compiled without SSL support")
|
||||||
|
|
||||||
|
proc newAsyncSmtp*(useSsl = false, debug=false,
|
||||||
sslContext = defaultSslContext): AsyncSmtp =
|
sslContext = defaultSslContext): AsyncSmtp =
|
||||||
## Creates a new ``AsyncSmtp`` instance.
|
## Creates a new ``AsyncSmtp`` instance.
|
||||||
new result
|
new result
|
||||||
result.address = address
|
result.debug = debug
|
||||||
result.port = port
|
|
||||||
result.useSsl = useSsl
|
|
||||||
|
|
||||||
result.sock = newAsyncSocket()
|
result.sock = newAsyncSocket()
|
||||||
if useSsl:
|
if useSsl:
|
||||||
when compiledWithSsl:
|
when compiledWithSsl:
|
||||||
sslContext.wrapSocket(result.sock)
|
sslContext.wrapSocket(result.sock)
|
||||||
else:
|
else:
|
||||||
raise newException(ESystem,
|
raise newException(SystemError,
|
||||||
"SMTP module compiled without SSL support")
|
"SMTP module compiled without SSL support")
|
||||||
|
|
||||||
proc quitExcpt(smtp: AsyncSmtp, msg: string): Future[void] =
|
proc quitExcpt(smtp: AsyncSmtp, msg: string): Future[void] =
|
||||||
var retFuture = newFuture[void]()
|
var retFuture = newFuture[void]()
|
||||||
var sendFut = smtp.sock.send("QUIT")
|
var sendFut = smtp.debugSend("QUIT")
|
||||||
sendFut.callback =
|
sendFut.callback =
|
||||||
proc () =
|
proc () =
|
||||||
# TODO: Fix this in async procs.
|
# TODO: Fix this in async procs.
|
||||||
raise newException(ReplyError, msg)
|
raise newException(ReplyError, msg)
|
||||||
return retFuture
|
return retFuture
|
||||||
|
|
||||||
proc checkReply(smtp: AsyncSmtp, reply: string) {.async.} =
|
proc checkReply(smtp: Smtp | AsyncSmtp, reply: string) {.multisync.} =
|
||||||
var line = await smtp.sock.recvLine()
|
var line = await smtp.debugRecv()
|
||||||
if not line.string.startswith(reply):
|
if not line.startswith(reply):
|
||||||
await quitExcpt(smtp, "Expected " & reply & " reply, got: " & line.string)
|
await quitExcpt(smtp, "Expected " & reply & " reply, got: " & line)
|
||||||
|
|
||||||
proc connect*(smtp: AsyncSmtp) {.async.} =
|
proc connect*(smtp: Smtp | AsyncSmtp,
|
||||||
|
address: string, port: Port) {.multisync.} =
|
||||||
## Establishes a connection with a SMTP server.
|
## Establishes a connection with a SMTP server.
|
||||||
## May fail with ReplyError or with a socket error.
|
## May fail with ReplyError or with a socket error.
|
||||||
await smtp.sock.connect(smtp.address, smtp.port)
|
await smtp.sock.connect(address, port)
|
||||||
|
|
||||||
await smtp.checkReply("220")
|
await smtp.checkReply("220")
|
||||||
await smtp.sock.send("HELO " & smtp.address & "\c\L")
|
await smtp.debugSend("HELO " & address & "\c\L")
|
||||||
await smtp.checkReply("250")
|
await smtp.checkReply("250")
|
||||||
|
|
||||||
proc auth*(smtp: AsyncSmtp, username, password: string) {.async.} =
|
proc auth*(smtp: Smtp | AsyncSmtp, username, password: string) {.multisync.} =
|
||||||
## Sends an AUTH command to the server to login as the `username`
|
## Sends an AUTH command to the server to login as the `username`
|
||||||
## using `password`.
|
## using `password`.
|
||||||
## May fail with ReplyError.
|
## May fail with ReplyError.
|
||||||
|
|
||||||
await smtp.sock.send("AUTH LOGIN\c\L")
|
await smtp.debugSend("AUTH LOGIN\c\L")
|
||||||
await smtp.checkReply("334") # TODO: Check whether it's asking for the "Username:"
|
await smtp.checkReply("334") # TODO: Check whether it's asking for the "Username:"
|
||||||
# i.e "334 VXNlcm5hbWU6"
|
# i.e "334 VXNlcm5hbWU6"
|
||||||
await smtp.sock.send(encode(username) & "\c\L")
|
await smtp.debugSend(encode(username) & "\c\L")
|
||||||
await smtp.checkReply("334") # TODO: Same as above, only "Password:" (I think?)
|
await smtp.checkReply("334") # TODO: Same as above, only "Password:" (I think?)
|
||||||
|
|
||||||
await smtp.sock.send(encode(password) & "\c\L")
|
await smtp.debugSend(encode(password) & "\c\L")
|
||||||
await smtp.checkReply("235") # Check whether the authentification was successful.
|
await smtp.checkReply("235") # Check whether the authentification was successful.
|
||||||
|
|
||||||
proc sendMail*(smtp: AsyncSmtp, fromAddr: string,
|
proc sendMail*(smtp: Smtp | AsyncSmtp, fromAddr: string,
|
||||||
toAddrs: seq[string], msg: string) {.async.} =
|
toAddrs: seq[string], msg: string) {.multisync.} =
|
||||||
## Sends ``msg`` from ``fromAddr`` to the addresses specified in ``toAddrs``.
|
## Sends ``msg`` from ``fromAddr`` to the addresses specified in ``toAddrs``.
|
||||||
## Messages may be formed using ``createMessage`` by converting the
|
## Messages may be formed using ``createMessage`` by converting the
|
||||||
## Message into a string.
|
## Message into a string.
|
||||||
|
|
||||||
await smtp.sock.send("MAIL FROM:<" & fromAddr & ">\c\L")
|
await smtp.debugSend("MAIL FROM:<" & fromAddr & ">\c\L")
|
||||||
await smtp.checkReply("250")
|
await smtp.checkReply("250")
|
||||||
for address in items(toAddrs):
|
for address in items(toAddrs):
|
||||||
await smtp.sock.send("RCPT TO:<" & address & ">\c\L")
|
await smtp.debugSend("RCPT TO:<" & address & ">\c\L")
|
||||||
await smtp.checkReply("250")
|
await smtp.checkReply("250")
|
||||||
|
|
||||||
# Send the message
|
# Send the message
|
||||||
await smtp.sock.send("DATA " & "\c\L")
|
await smtp.debugSend("DATA " & "\c\L")
|
||||||
await smtp.checkReply("354")
|
await smtp.checkReply("354")
|
||||||
await smtp.sock.send(msg & "\c\L")
|
await smtp.sock.send(msg & "\c\L")
|
||||||
await smtp.sock.send(".\c\L")
|
await smtp.debugSend(".\c\L")
|
||||||
await smtp.checkReply("250")
|
await smtp.checkReply("250")
|
||||||
|
|
||||||
proc close*(smtp: AsyncSmtp) {.async.} =
|
proc close*(smtp: Smtp | AsyncSmtp) {.multisync.} =
|
||||||
## Disconnects from the SMTP server and closes the socket.
|
## Disconnects from the SMTP server and closes the socket.
|
||||||
await smtp.sock.send("QUIT\c\L")
|
await smtp.debugSend("QUIT\c\L")
|
||||||
smtp.sock.close()
|
smtp.sock.close()
|
||||||
|
|
||||||
when not defined(testing) and isMainModule:
|
when not defined(testing) and isMainModule:
|
||||||
|
|
@ -278,25 +223,24 @@ when not defined(testing) and isMainModule:
|
||||||
|
|
||||||
proc async_test() {.async.} =
|
proc async_test() {.async.} =
|
||||||
let client = newAsyncSmtp(
|
let client = newAsyncSmtp(
|
||||||
conf["smtphost"],
|
conf["use_tls"].parseBool,
|
||||||
conf["port"].parseInt.Port,
|
debug=true
|
||||||
conf["use_tls"].parseBool
|
|
||||||
)
|
)
|
||||||
await client.connect()
|
await client.connect(conf["smtphost"], conf["port"].parseInt.Port)
|
||||||
await client.auth(conf["username"], conf["password"])
|
await client.auth(conf["username"], conf["password"])
|
||||||
await client.sendMail(conf["sender"], @[conf["recipient"]], $msg)
|
await client.sendMail(conf["sender"], @[conf["recipient"]], $msg)
|
||||||
await client.close()
|
await client.close()
|
||||||
echo "async email sent"
|
echo "async email sent"
|
||||||
|
|
||||||
proc sync_test() =
|
proc sync_test() =
|
||||||
var smtpConn = connect(
|
var smtpConn = newSmtp(
|
||||||
conf["smtphost"],
|
|
||||||
conf["port"].parseInt.Port,
|
|
||||||
conf["use_tls"].parseBool,
|
conf["use_tls"].parseBool,
|
||||||
true, # debug
|
debug=true
|
||||||
)
|
)
|
||||||
|
smtpConn.connect(conf["smtphost"], conf["port"].parseInt.Port)
|
||||||
smtpConn.auth(conf["username"], conf["password"])
|
smtpConn.auth(conf["username"], conf["password"])
|
||||||
smtpConn.sendmail(conf["sender"], @[conf["recipient"]], $msg)
|
smtpConn.sendMail(conf["sender"], @[conf["recipient"]], $msg)
|
||||||
|
smtpConn.close()
|
||||||
echo "sync email sent"
|
echo "sync email sent"
|
||||||
|
|
||||||
waitFor async_test()
|
waitFor async_test()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue