From 8ff8b541ad67e88b3b17e6c43604e180a29765dd Mon Sep 17 00:00:00 2001 From: Alexander Vershilov Date: Tue, 23 Nov 2021 18:06:23 +0300 Subject: [PATCH] Ignore 502 error on helo - fixes communication with some servers Fixes #31. --- src/Network/HaskellNet/SMTP/SSL.hs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Network/HaskellNet/SMTP/SSL.hs b/src/Network/HaskellNet/SMTP/SSL.hs index b3dd3ba..2ed35de 100644 --- a/src/Network/HaskellNet/SMTP/SSL.hs +++ b/src/Network/HaskellNet/SMTP/SSL.hs @@ -52,7 +52,7 @@ connectSTARTTLS hostname cfg = do hn <- getHostName bsPut bs $ B.pack ("HELO " ++ hn ++ "\r\n") - getResponse bs >>= failIfNot bs 250 + getResponse bs >>= failIfNotEx bs (`elem` [250, 502]) bsPut bs $ B.pack ("EHLO " ++ hn ++ "\r\n") getResponse bs >>= failIfNot bs 250 bsPut bs $ B.pack "STARTTLS\r\n" @@ -73,6 +73,11 @@ failIfNot :: BSStream -> Integer -> (Integer, String) -> IO () failIfNot bs code (rc, rs) = when (code /= rc) closeAndFail where closeAndFail = bsClose bs >> fail ("cannot connect to server: " ++ rs) +-- | Extended version of fail if, can support multiple statuses +failIfNotEx :: BSStream -> (Integer -> Bool) -> (Integer, String) -> IO () +failIfNotEx bs f (rc, rs) = unless (f rc) closeAndFail + where closeAndFail = bsClose bs >> fail ("cannot connect to server: " ++ rs) + -- This is a bit of a nasty hack. Network.HaskellNet.SMTP.connectStream -- expects to receive a status 220 from the server as soon as it connects, -- but we've intercepted it in order to establish a STARTTLS connection.