import Network.SMTP.ClientSession
import Network.SMTP.Client
import Network.Socket
import Text.ParserCombinators.Parsec.Rfc2822
import System.Time
import System.IO
import Data.Bits
import Data.IORef
-- To do: Find out the proper way of doing this.
intToPortNumber :: Int -> PortNumber
intToPortNumber v = PortNum $ fromIntegral $ ((v `shiftR` 8) .|. (v `shiftL` 8)) .&. 0xffff
myDomain = "example.com"
smtpHost = "smtp.example.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") "nobody@example.com"],
To [NameAddr (Just "Stephen Blackheath") "unprintable.distances.stephen@blacksapphire.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) Nothing
let SockAddrInet _ hostAddr = addrAddress $ (addrs !! 0)
let sockAddr = SockAddrInet (intToPortNumber 25) hostAddr
putStrLn $ "connecting to "++show sockAddr
sentRef <- newIORef 0
sendSMTP_ (hPutStrLn stderr) (Just sentRef) myDomain
sockAddr [message]
sent <- readIORef sentRef
putStrLn $ "no of emails sent=" ++ show sent