Amazon SESをSMTPサーバとして使いHaskellからメールを送るサンプル。
要はSSLで包んでSMTPを使えばいい。
以下、サンプルコード。
import GHC.Base
import Data.String (IsString)
import Network.HaskellNet.SMTP.SSL
import Network.Socket (PortNumber(..))
import Network.Mail.Mime (Mail(..), Address(..), plainPart)
_port :: PortNumber
_port = 465
_host :: IsString a => a
_host = "email-smtp.us-east-1.amazonaws.com"
_user :: IsString a => a
_user = "SMTP-ACCESS-KEY"
_pass :: IsString a => a
_pass = "SMTP-SECRET-KEY"
sendMailWithSES :: IO ()
sendMailWithSES = doSMTPSSLWithSettings
_host
defaultSettingsSMTPSSL { sslPort = _port
, sslLogToConsole = True
}
(
\conn -> do
auth <- authenticate LOGIN _user _pass conn
_ <- if auth then
do
return ()
else
error "authenticate failed."
mail <- return $ Mail {
mailFrom = address "jabara ses" "ah+ses@jabara.info"
, mailTo = [address "jabara" "ah@jabara.info"]
, mailCc = []
, mailBcc = []
, mailHeaders = [("Subject", "test mail")]
, mailParts = [[plainPart "This is Haskell send mail via Amazon SES."]]
}
sendMimeMail2 mail conn
-- sendPlainTextMail
-- "ah@jabara.info" -- receiver
-- "ah+ses@jabara.info" -- sender
-- "test" -- subject
-- "this is test mail." -- body
-- conn
)
where
address name email = Address (Just name) email