I have been struggling to send emails form within a Haskell program for a while now, tried to use HaskellMime library or something like that, but failed.
I installed HaskellNet recently and try using Haskellnet.SMTP module.
I tried sending an email with 'sendMail' command and get "user error(sendMail error)". I suppose it was because the SMTP server I used needed authentication.
I took a look at 'sendMail' source code and ended up writing this simple main: http://hpaste.org/47841
I checked each 'sendCommand' command and after the AUTH command I get a "Auth success" from the SMTP server, and 250 code from the other commands, as expected in the 'sendMail' source code.
The problem is I don't have any mails in my mailbox, so what am I doing wrong? The only thing I can think of, is that the mail is somewhere queued in the SMTP outgoing list and I need to flush the SMTP server, but that is not part of the 'sendMail' code, so I wonder...
Any help would be greatly appreciated, because I never thought it would be so hard to send an email :/
P.S. I use the exact same settings on my phone to send email with this SMTP server, same "smtp.sfr.fr", same ID (entire address), same password; and it works: I can send mails from my phone.
Thanks in advance for the help.
While I can't comment on your use of HaskellNet, I've had great success using SMTPClient which you can grab from hackage with cabal install SMTPClient
.
I've included the package's example to give you a sense of what using the library is like:
import Network.SMTP.ClientSession
import Network.SMTP.Client
import Network.Socket
import System.Time
import System.IO
import Data.Bits
import Data.IORef
myDomain = "example.com"
smtpHost = "hubert.blacksapphire.com" -- <-- Your SMTP server here
-- This will send the author an email. I don't mind!
main = do
now <- getClockTime
nowCT <- toCalendarTime now
let message = Message [
From [NameAddr (Just "Mr. Nobody") "[email protected]"],
To [NameAddr (Just "Stephen Blackheath") "[email protected]"],
Subject "I'm using SMTPClient!",
Date nowCT
]
("Dear Sir,\n"++
"It has come to my attention that this is an email.\n"++
"Yours sincerely,\n"++
"Mr. Nobody\n")
addrs <- getAddrInfo Nothing (Just smtpHost) Nothing
let SockAddrInet _ hostAddr = addrAddress (addrs !! 0)
sockAddr = SockAddrInet (fromIntegral 25) hostAddr
putStrLn $ "connecting to "++show sockAddr
sentRef <- newIORef []
sendSMTP' (hPutStrLn stderr) (Just sentRef) myDomain
sockAddr [message]
statuses <- readIORef sentRef
-- If no exception was caught, statuses is guaranteed to be
-- the same length as the list of input messages, therefore head won't fail here.
case head statuses of
Nothing -> putStrLn "Message successfully sent"
Just status -> putStrLn $ "Message send failed with status "++show status
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With