6
3

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.

Network.Mail.Mime でのメール送信方法

Last updated at Posted at 2016-01-14

Network.Mail.Mime を使えば UTF-8 ですごく簡単にメール送信ができる。sendmail コマンドが呼び出せることが前提。

simple.hs
{-# LANGUAGE OverloadedStrings #-}

import Network.Mail.Mime

main :: IO ()
main =
  renderSendMail $ simpleMail'
    (Address Nothing "to@example.com")
    (Address Nothing "from@example.com")
    "件名"
    "本文"

この方法だと to に複数メールアドレスを設定することができない。複数アドレスに同時にメールを送信したい場合、少しだけ複雑になる。

complex.hs
{-# LANGUAGE OverloadedStrings #-}

import Network.Mail.Mime

main :: IO ()
main = renderSendMail mail'
 where
  from = Address Nothing "from@example.com"
  mail = addPart [plainPart "本文"] $ emptyMail from
  mail' = mail { mailHeaders = [ ("to", "to1@example.com, to2@example.com")
                               , ("subject", "件名")
                               ]   
               }   

ポイントは emptyMail で from アドレスだけ持つ Mail インスタンスを作成し、mailHeaders で自由にヘッダを設定するところ。今回のサンプルでは addPart で text/plain の本文をひとつだけ追加しているが、HTML パートも添付ファイルも簡単にできる。

Network.Mail.Client.Gmail でのメール送信方法もご参考ください。

6
3
2

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
6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?