Skip to content

Commit

Permalink
Merge pull request #158 from fewlinesco/release/v3.1.0
Browse files Browse the repository at this point in the history
Bump to v3.1.0

This release contains:

Fix for using custom config with response: true by bumping bamboo version to ~> 1.6 (#150)
Implement our custom test adapter ([#151])
Fix CI random failure by attaching FakeGenSMTP Server process to Test supervision tree.(#153)
Add Content-ID header when needed(#154)
Base 64 encode the headers only when the content contains non-ASCII characters.(#155)
Handle :permanent_failure exception and re-raising it as a SMTPError.(#156)
After bumping the dependencies, the project requires(#149):
credo ~> 1.4.1
bamboo ~> 1.6
excoveralls ~> 0.13.3
gen_smtp ~> 1.0.1
  • Loading branch information
MatheusBueno782 committed Nov 24, 2020
2 parents 1e29509 + dc46246 commit c6328d5
Show file tree
Hide file tree
Showing 11 changed files with 313 additions and 84 deletions.
59 changes: 59 additions & 0 deletions .github/workflows/bamboo_smtp.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: Bamboo SMTP

on: push

jobs:
test:
name: Test on Elixir ${{ matrix.elixir }} / OTP ${{ matrix.otp }}
runs-on: ubuntu-latest
strategy:
matrix:
elixir: [1.7.4, 1.8.2, 1.9.1, 1.10.3]
otp: [19.3, 20.3, 21.3, 22.0]
exclude:
- elixir: 1.8.2
otp: 19.3
- elixir: 1.9.1
otp: 19.3
- elixir: 1.10.3
otp: 19.3
- elixir: 1.10.3
otp: 20.3
steps:
- name: Checkout code
uses: actions/checkout@v2

- uses: actions/cache@v2
with:
path: deps
key: ${{ runner.os }}-mix-${{ hashFiles(format('{0}{1}', github.workspace, '/mix.lock')) }}-elixir-${{ matrix.elixir }}-otp-${{ matrix.otp }}

- uses: actions/setup-elixir@v1
with:
otp-version: ${{ matrix.otp }}
elixir-version: ${{ matrix.elixir }}

- run: mix deps.get
- run: mix compile --warnings-as-errors
- run: mix credo --strict
- run: mix test

doc:
name: Generate inch report
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2

- uses: actions/cache@v2
with:
path: deps
key: ${{ runner.os }}-mix-${{ hashFiles(format('{0}{1}', github.workspace, '/mix.lock')) }}

- uses: actions/setup-elixir@v1
with:
otp-version: 22.0
elixir-version: 1.10.3

- run: mix deps.get --only docs
- run: MIX_ENV=docs mix inch.report
37 changes: 0 additions & 37 deletions .travis.yml

This file was deleted.

22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
# CHANGELOG

## 3.1.0 - 2020-11-23

- Fix for using custom config with `response: true` by bumping `bamboo` version to `~> 1.6` ([#150])
- Implement our custom test adapter ([#151])
- Fix CI random failure by attaching FakeGenSMTP Server process to Test supervision tree.([#153])
- Add Content-ID header when needed([#154])
- Base 64 encode the headers only when the content contains non-ASCII characters.([#155])
- Handle `:permanent_failure` exception and re-raising it as a `SMTPError`.([#156])
- After bumping the dependencies, the project requires([#149]):
- credo `~> 1.4.1`
- bamboo `~> 1.6`
- excoveralls `~> 0.13.3`
- gen_smtp `~> 1.0.1`

[#149]: https://github.com/fewlinesco/bamboo_smtp/pull/149
[#150]: https://github.com/fewlinesco/bamboo_smtp/pull/150
[#151]: https://github.com/fewlinesco/bamboo_smtp/pull/151
[#153]: https://github.com/fewlinesco/bamboo_smtp/pull/153
[#154]: https://github.com/fewlinesco/bamboo_smtp/pull/154
[#155]: https://github.com/fewlinesco/bamboo_smtp/pull/155
[#156]: https://github.com/fewlinesco/bamboo_smtp/pull/156

## 3.0.0 - 2020-09-10

- Fix eml attachment ([#137]).
Expand Down
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[![Hex pm](https://img.shields.io/hexpm/v/bamboo_smtp.svg)](https://hex.pm/packages/bamboo_smtp)
[![Build Status](https://travis-ci.org/fewlinesco/bamboo_smtp.svg?branch=master)](https://travis-ci.org/fewlinesco/bamboo_smtp)
[![Build Status](https://github.com/fewlinesco/bamboo_smtp/workflows/Bamboo%20SMTP/badge.svg)](https://github.com/fewlinesco/bamboo_smtp/actions)
[![Inline docs](http://inch-ci.org/github/fewlinesco/bamboo_smtp.svg)](http://inch-ci.org/github/fewlinesco/bamboo_smtp)

# Bamboo.SMTPAdapter
Expand All @@ -14,7 +14,7 @@ The package can be installed as:

```elixir
def deps do
[{:bamboo_smtp, "~> 3.0.0"}]
[{:bamboo_smtp, "~> 3.1.0"}]
end
```

Expand Down 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
49 changes: 41 additions & 8 deletions lib/bamboo/adapters/smtp_adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,18 @@ defmodule Bamboo.SMTPAdapter do
config
|> to_gen_smtp_server_config

email
|> Bamboo.Mailer.normalize_addresses()
|> to_gen_smtp_message
|> config[:transport].send_blocking(gen_smtp_config)
|> handle_response
response =
try do
email
|> Bamboo.Mailer.normalize_addresses()
|> to_gen_smtp_message
|> config[:transport].send_blocking(gen_smtp_config)
catch
e ->
raise SMTPError, {:not_specified, e}
end

handle_response(response)
end

@doc false
Expand All @@ -96,6 +103,10 @@ defmodule Bamboo.SMTPAdapter do
raise SMTPError, {reason, detail}
end

defp handle_response({:error, detail}) do
raise SMTPError, {:not_specified, detail}
end

defp handle_response(response) do
{:ok, response}
end
Expand Down Expand Up @@ -191,7 +202,17 @@ defmodule Bamboo.SMTPAdapter do
end

defp rfc822_encode(content) do
"=?UTF-8?B?#{Base.encode64(content)}?="
if contains_only_ascii_characters?(content) do
"=?UTF-8?B?#{content}?="
else
"=?UTF-8?B?#{Base.encode64(content)}?="
end
end

defp contains_only_ascii_characters?(content) do
content
|> String.to_charlist()
|> List.ascii_printable?()
end

def base64_and_split(data) do
Expand All @@ -215,7 +236,19 @@ defmodule Bamboo.SMTPAdapter do
|> add_smtp_line(text_body)
end

defp add_attachment_header(body, %{content_type: content_type} = attachment)
defp add_attachment_header(body, attachment) do
case attachment.content_id do
nil ->
add_common_attachment_header(body, attachment)

cid ->
body
|> add_common_attachment_header(attachment)
|> add_smtp_line("Content-ID: <#{cid}>")
end
end

defp add_common_attachment_header(body, %{content_type: content_type} = attachment)
when content_type == "message/rfc822" do
<<random::size(32)>> = :crypto.strong_rand_bytes(4)

Expand All @@ -225,7 +258,7 @@ defmodule Bamboo.SMTPAdapter do
|> add_smtp_line("X-Attachment-Id: #{random}")
end

defp add_attachment_header(body, attachment) do
defp add_common_attachment_header(body, attachment) do
<<random::size(32)>> = :crypto.strong_rand_bytes(4)

body
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
12 changes: 6 additions & 6 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule BambooSmtp.Mixfile do
def project do
[
app: :bamboo_smtp,
version: "3.0.0",
version: "3.1.0",
elixir: "~> 1.7",
source_url: @project_url,
homepage_url: @project_url,
Expand All @@ -27,19 +27,19 @@ defmodule BambooSmtp.Mixfile do

defp deps do
[
{:bamboo, "~> 1.2"},
{:credo, "~> 1.4.0", only: [:dev, :test]},
{:bamboo, "~> 1.6"},
{:credo, "~> 1.4.1", only: [:dev, :test]},
{:earmark, ">= 1.3.2", only: :docs},
{:excoveralls, "~> 0.12.0", only: :test},
{:excoveralls, "~> 0.13.3", only: :test},
{:ex_doc, ex_doc_version(), only: :docs},
{:gen_smtp, "~> 0.15.0"},
{:gen_smtp, "~> 1.0.1"},
{:inch_ex, "~> 2.0.0", only: :docs}
]
end

defp ex_doc_version do
if Version.match?(System.version(), "~> 1.7") do
"~> 0.21.2"
"~> 0.23.0"
else
"~> 0.18.4"
end
Expand Down

0 comments on commit c6328d5

Please sign in to comment.