1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Amazon SESとNetwork.Mail.Mimeでメールを送信してみた

Posted at

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
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?