import Network.SMTP.ClientSession
import Network.SMTP.Client
import Network.Socket
import System.Time
import System.IO
import Data.IORef
myDomain = "example.com"
smtpHost = "mail.example.com" -- <-- Your SMTP server here
main = do
now <- getClockTime
nowCT <- toCalendarTime now
let message = Message [
From [NameAddr (Just "Mr. Nobody") "nobody@example.com"],
To [NameAddr (Just "Mr. Somebody") "somebody@example.com"],
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) (Just "25")
putStrLn $ "connecting to "++show (map addrAddress addrs)
sentRef <- newIORef []
sendSMTP' (hPutStrLn stderr) (Just sentRef) myDomain addrs [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