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