Skip to content

Commit

Permalink
Merge branch 'develop' into CU-9muftt-update-deps
Browse files Browse the repository at this point in the history
  • Loading branch information
babariviere committed Nov 3, 2020
2 parents 4ec3e9b + 4209633 commit f79ff13
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ The *hostname* option sets the FQDN to the header of your emails, its optional,

4. Follow Bamboo [Getting Started Guide](https://github.com/thoughtbot/bamboo#getting-started)

5. **Optional** Set `BambooSMTP.TestAdapter` as your test adapter:

```elixir
# In your config/config.exs file
if Mix.env() == :test do
config :my_app, MyApp.Mailer, adapter: MyApp.SMTPTestAdapter
end
```
## Usage

You can find more information about advanced features in the [Wiki](https://github.com/fewlinesco/bamboo_smtp/wiki).
Expand Down
56 changes: 56 additions & 0 deletions lib/bamboo/adapters/test_adapter.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
defmodule BambooSMTP.TestAdapter do
@moduledoc """
Based on `Bamboo.TestAdapter`, this module can be used for testing email delivery.
The `deliver/2` function will provide a response that follow the format of a SMTP server raw response.
No emails are sent, instead it sends back `{%Bamboo.Email{...}, {:ok,"<raw_smtp_response>"}}`
for success and raise an exception on error.
## Example config
# Typically done in config/test.exs
config :my_app, MyApp.Mailer,
adapter: BambooSMTP.TestAdapter
# Define a Mailer. Typically in lib/my_app/mailer.ex
defmodule MyApp.Mailer do
use Bamboo.Mailer, otp_app: :my_app
end
"""

@behaviour Bamboo.Adapter

@doc false
def deliver(_email, _config) do
send(test_process(), {:ok, "Ok #{Enum.random(100_000_000..999_999_999)}"})
end

defp test_process do
Application.get_env(:bamboo, :shared_test_process) || self()
end

def handle_config(config) do
case config[:deliver_later_strategy] do
nil ->
Map.put(config, :deliver_later_strategy, Bamboo.ImmediateDeliveryStrategy)

Bamboo.ImmediateDeliveryStrategy ->
config

_ ->
raise ArgumentError, """
BambooSMTP.TestAdapter requires that the deliver_later_strategy is
Bamboo.ImmediateDeliveryStrategy
Instead it got: #{inspect(config[:deliver_later_strategy])}
Please remove the deliver_later_strategy from your config options, or
set it to Bamboo.ImmediateDeliveryStrategy.
"""
end
end

@doc false
def supports_attachments?, do: true
end

0 comments on commit f79ff13

Please sign in to comment.