-
Notifications
You must be signed in to change notification settings - Fork 1
/
adapter.ex
78 lines (64 loc) · 2.2 KB
/
adapter.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
68
69
70
71
72
73
74
75
76
77
78
defmodule Resend.Swoosh.Adapter do
@moduledoc """
Adapter module to configure Swoosh to send emails via Resend.
Using this adapter, we can configure a new Phoenix application to send mail
via Resend by default. If the project generated authentication with `phx.gen.auth`,
then all auth communication will work with Resend out of the box.
To configure your Mailer, specify the adapter and a Resend API key:
```ex
config :my_app, MyApp.Mailer,
adapter: Resend.Swoosh.Adapter,
api_key: "re_1234567"
```
If you're configuring your app for production, configure your adapter in `prod.exs`, and
your API key from the environment in `runtime.exs`:
```ex
# prod.exs
config :my_app, MyApp.Mailer, adapter: Resend.Swoosh.Adapter
```
```ex
# runtime.exs
config :my_app, MyApp.Mailer, api_key: "re_1234567"
```
And just like that, you should be all set to send emails with Resend!
"""
@behaviour Swoosh.Adapter
@impl true
def deliver(%Swoosh.Email{} = email, config) do
Resend.Emails.send(Resend.client(config), %{
subject: email.subject,
from: format_sender(email.from),
to: format_recipients(email.to),
bcc: format_recipients(email.bcc),
cc: format_recipients(email.cc),
reply_to: format_recipients(email.reply_to),
headers: email.headers,
html: email.html_body,
text: email.text_body
})
end
@impl true
def deliver_many(list, config) do
Enum.reduce_while(list, {:ok, []}, fn email, {:ok, acc} ->
case deliver(email, config) do
{:ok, email} ->
{:cont, {:ok, acc ++ [email]}}
{:error, _reason} = error ->
{:halt, error}
end
end)
end
@impl true
def validate_config(config) do
Resend.validate_config!(config)
:ok
end
defp format_sender(nil), do: nil
defp format_sender(from) when is_binary(from), do: from
defp format_sender({"", from}), do: from
defp format_sender({from_name, from}), do: "#{from_name} <#{from}>"
defp format_recipients(nil), do: nil
defp format_recipients(to) when is_binary(to), do: to
defp format_recipients({_ignore, to}), do: to
defp format_recipients(xs) when is_list(xs), do: Enum.map(xs, &format_recipients/1)
end