Heyho everybody!
I am building a web application in and recently wanted to start implementing features that involve sending mails (password-reset functionality).
Nim has std/smtp for this with really neat examples for using both starttls and ssl, however I can't seem to get them to work despite sticking to them as closely as I could. For that purpose I created a dummy mail address on a local mail service provider (https://hilfe.web.de/pop-imap/pop3/serverdaten.html - Careful, loads of spam) to play around a bit and can't get it to work, despite following the examples as closely as I could.
Here the code I used:
import std/[smtp]
let smtpServerName = "smtp.web.de"
let startTlsportNumber = 587
let un = "dummymail" # replace with actual username
let pw = "dummypw" # replace with actual pw
let target = "[email protected]"
#StartTLS code block - Have either this or the SSL code block commented in, not both
let smtpConn = newSmtp(debug=true)
smtpConn.connect(smtpServerName, Port startTlsportNumber)
smtpConn.startTls()
#SSL code block - Have either this or the startTLS code block commented in, not both
# let sslPortNumber = 465
# let smtpConn = newSmtp(useSsl = true, debug=true)
# smtpConn.connect(smtpServerName, Port sslPortNumber)
var msg = createMessage(mSubject = "Hello from Nim's SMTP",
mBody = "Hello!.\n Is this awesome or what?",
mTo = @[target])
smtpConn.auth(un, pw)
smtpConn.sendmail(un, @[target], $msg)
With startTLS this causes a runtime error as the web.de server sends back a 503 response on smtpConn.startTls:
Error: unhandled exception: Expected 220 reply, got: 503 Bad sequence of commands [ReplyError]
With SSL this causes a runtime error as the web.de server sends back a 503 response on smtpConn.connect: > Error: unhandled exception: Expected 250 reply, got: 503 Bad sequence of commands [ReplyError]
I've triple checked the ports, the service provider states explicitly that 587 is the port for startTls and 465 for SSL. I compile on Arch Linux with -d:ssl in both cases, so that shouldn't be an issue either (and also that would be a compile-time error in that scenario, wouldn't it?).
Googling the error for a bit and looking at other questions of other programming languages, the error seems to be related to authentication? Which is weird, because I thought authentication starts with my login credentials after I made the connection secure with startTls. This looks to me like I'm using the std/smtp library wrong, but I don't quite see where. Does anyone here see the issue?
Ah, naturally!
#With startTLS codeblock
S:220 web.de (mrweb005) Nemesis ESMTP Service ready
C:HELO smtp.web.de
S:503 Bad sequence of commands
C:QUIT
/home/philipp/dev/playground/src/playground.nim(15) playground
/usr/lib/nim/pure/smtp.nim(240) connect
/usr/lib/nim/pure/smtp.nim(231) helo
/usr/lib/nim/pure/smtp.nim(226) checkReply
/usr/lib/nim/pure/smtp.nim(110) quitExcpt
Error: unhandled exception: Expected 250 reply, got: 503 Bad sequence of commands [ReplyError]
# With SSL codeblock
S:220 web.de (mrweb005) Nemesis ESMTP Service ready
C:HELO smtp.web.de
S:503 Bad sequence of commands
C:QUIT
/home/philipp/dev/playground/src/playground.nim(15) playground
/usr/lib/nim/pure/smtp.nim(240) connect
/usr/lib/nim/pure/smtp.nim(231) helo
/usr/lib/nim/pure/smtp.nim(226) checkReply
/usr/lib/nim/pure/smtp.nim(110) quitExcpt
Error: unhandled exception: Expected 250 reply, got: 503 Bad sequence of commands [ReplyError]
Alright so I tried around with the above just now with a dummy google mail account. With that one the startTls variant worked flawlessly (after putting an "application"-password in place).
Thus I'm getting the idea that the local mailprovider might be the issue behind this. Why that is the case though I'm not sure.
so check if you can get an application key from your mail provider and check if your mail provider supports smtp
It's a little strange that it opens with smtp, it should not after my pr: https://github.com/nim-lang/Nim/pull/19077
Can you check if it works with this changed version?