LoginSignup
11
9

More than 5 years have passed since last update.

Elixirでメールを送信

Last updated at Posted at 2016-06-03

kamilc/mailmanを利用します。

Githubにサンプルプロジェクト

ソースコード

lib/mailer.ex
defmodule Mailer do
  def deliver(email) do
    # CC or BCCを利用する場合、:send_cc_and_bccが必要
    Mailman.deliver(email, config, :send_cc_and_bcc)
  end

  def config do
    %Mailman.Context{
      config: %Mailman.SmtpConfig{
        relay: "smtp.gmail.com",
        username: "MAILADDRESS",
        password: "PASSWORD",
        port: 587,
        auth: :always,
        tls: :always
      },
      composer: %Mailman.EexComposeConfig{
        html_file: false,
        text_file: true,
        text_file_path: "lib/templates/"
      }
    }
  end
end

defmodule Mail do
  def run do
    send
  end

  def send do
    Mailer.deliver(contents)
  end

  def contents do
    %Mailman.Email{
      subject: "subject",
      from: "FROM",
      to: ["TO"],
      bcc: ["BCC"],
      data: [], # テンプレートを使う場合、必須
      text: "mailer.txt.eex"
    }
  end
end

lib/templates/mailer.txt.eex
Hoge
mix.exs
  # 省略
  defp deps do
    [{:mailman, "~> 0.2.2"}]
  end
  # 省略

実行

$ mix run -e Mail.run
11
9
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
11
9