1
- defmodule Support.SwooshFakeMailer do
2
- @ moduledoc false
3
-
4
- # Overrides the Swoosh `deliver!/1` function.
5
- #
6
- # Instead of sending an email it puts the fields in a mailbox so they can be
7
- # received in the test.
8
- #
9
- # Swoosh does include Swoosh.Adapters.Test, but its behaviour seems unreliable
10
- # in our case because we are catching then sending mail, meaning tests need
11
- # a lot of Process.sleep/1 calls to wait for mailboxes.
12
- #
13
- # It's also possible to use Swoosh.Adapters.Local and
14
- # Swoosh.Adapter.Local.Storage.Memory which you can directly inspect. This
15
- # could be used to check for specific mail.
1
+ defmodule BoomNotifier.FakeMailer do
2
+ @ moduledoc """
3
+ Fake mailer for Swoosh and Bamboo
4
+ """
5
+
6
+ @ doc """
7
+ Bamboo's email deliver that just sends message back
8
+ """
9
+ def deliver_later! ( email ) do
10
+ # Use email.to to specify test target pid
11
+ email . to
12
+ |> to_pid ( )
13
+ |> send_back ( email . to , email . from , email )
14
+ end
16
15
16
+ @ doc """
17
+ Swoosh's email deliver that just sends message back
18
+ """
17
19
def deliver! ( email ) do
18
20
# Swoosh uses a {name, address} tuple for to and from.
19
21
# The `name` will default to `""` when not specified in the email creation,
@@ -23,25 +25,20 @@ defmodule Support.SwooshFakeMailer do
23
25
# this crashes OTP 21, specifically getting the from_addr via tuple match,
24
26
# so get from_addr via elem/2 separately...
25
27
# %{to: [{_name, email_to}], from: {_, from_addr}} = email
26
- % { to: [ { _name , email_to } ] } = email
28
+ % { to: [ { _name , email_to } ] , from: from } = email
29
+ email_from = elem ( from , 1 )
27
30
28
31
# Use email.to to specify test target pid
29
- pid =
30
- email_to
31
- |> String . to_atom ( )
32
- |> Process . whereis ( )
33
-
34
- send_back ( pid , email )
32
+ email_to
33
+ |> to_pid ( )
34
+ |> send_back ( email_to , email_from , email )
35
35
end
36
36
37
- def send_back ( nil , _ ) , do: nil
38
-
39
- def send_back ( pid , email ) do
40
- % { to: [ { _name , email_to } ] , from: from } = email
41
- from_addr = elem ( from , 1 )
37
+ defp send_back ( nil , _ , _ , _ ) , do: nil
42
38
39
+ defp send_back ( pid , email_to , email_from , email ) do
43
40
send ( pid , { :email_subject , email . subject } )
44
- send ( pid , { :email_from , from_addr } )
41
+ send ( pid , { :email_from , email_from } )
45
42
send ( pid , { :email_to , email_to } )
46
43
send ( pid , { :email_text_body , text_body_lines ( email . text_body ) } )
47
44
send ( pid , { :email_html_body , email . html_body } )
@@ -52,4 +49,13 @@ defmodule Support.SwooshFakeMailer do
52
49
|> Enum . map ( & String . trim / 1 )
53
50
|> Enum . reject ( & ( String . length ( & 1 ) == 0 ) )
54
51
end
52
+
53
+ defp to_pid ( value ) do
54
+ value
55
+ |> String . to_existing_atom ( )
56
+ |> Process . whereis ( )
57
+ rescue
58
+ # raised by to_existing_atom
59
+ ArgumentError -> nil
60
+ end
55
61
end
0 commit comments