-
Notifications
You must be signed in to change notification settings - Fork 209
/
sendmail.ex
67 lines (56 loc) · 1.45 KB
/
sendmail.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
defmodule Swoosh.Adapters.Sendmail do
@moduledoc ~S"""
An adapter that sends email using the sendmail binary.
## Example
# config/config.exs
config :sample, Sample.Mailer,
adapter: Swoosh.Adapters.Sendmail,
cmd_path: "/usr/bin/sendmail",
cmd_args: "-N delay,failure,success",
qmail: true # Default false
# lib/sample/mailer.ex
defmodule Sample.Mailer do
use Swoosh.Mailer, otp_app: :sample
end
"""
use Swoosh.Adapter, required_deps: [gen_smtp: :mimemail]
alias Swoosh.Email
alias Swoosh.Adapters.SMTP.Helpers
def deliver(%Email{} = email, config) do
body = Helpers.body(email, config)
port = Port.open({:spawn, cmd(email, config)}, [:binary])
Port.command(port, body)
Port.close(port)
:ok
end
@doc false
def cmd(email, config) do
sender = Helpers.sender(email) |> shell_escape()
"#{cmd_path(config)} -f#{sender}#{cmd_args(config)}"
end
@doc false
def cmd_path(config) do
default =
case config[:qmail] do
true -> "/var/qmail/bin/qmail-inject"
_ -> "/usr/sbin/sendmail"
end
config[:cmd_path] || default
end
@doc false
def cmd_args(config) do
case config[:qmail] do
true -> ""
_ -> " -oi -t"
end
<>
case config[:cmd_args] do
nil -> ""
args -> " #{args}"
end
end
@doc false
def shell_escape(s) do
"'" <> String.replace(s, "'", "'\\''") <> "'"
end
end