Skip to content

Commit

Permalink
Merge pull request #166 from fewlinesco/release/v3.1.1
Browse files Browse the repository at this point in the history
This release brings back Base64 encoding on headers.
  • Loading branch information
MatheusBueno782 authored Jan 4, 2021
2 parents c6328d5 + bc3fa7e commit a6c9b58
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 87 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/bamboo_smtp.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
name: Bamboo SMTP

on: push
on:
push:
branches: [master, develop]
pull_request:
branches: [master, develop]

jobs:
test:
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## 3.1.1 - 2021-01-04
- Bring back Base64 encoding on headers. Fix issue [#162]

[#162]: https://github.com/fewlinesco/bamboo_smtp/pull/162
## 3.1.0 - 2020-11-23

- Fix for using custom config with `response: true` by bumping `bamboo` version to `~> 1.6` ([#150])
Expand Down
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2016 Fewlines SAS
Copyright (c) 2016-current Fewlines SAS

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
89 changes: 47 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,65 +1,70 @@
[![Hex pm](https://img.shields.io/hexpm/v/bamboo_smtp.svg)](https://hex.pm/packages/bamboo_smtp)
# Bamboo.SMTPAdapter

[![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)
[![Module Version](https://img.shields.io/hexpm/v/bamboo_smtp.svg)](https://hex.pm/packages/bamboo_smtp)
[![Hex Docs](https://img.shields.io/badge/hex-docs-lightgreen.svg)](https://hexdocs.pm/bamboo_smtp/)
[![Total Download](https://img.shields.io/hexpm/dt/bamboo_smtp.svg)](https://hex.pm/packages/bamboo_smtp)
[![License](https://img.shields.io/hexpm/l/bamboo_smtp.svg)](https://github.com/fewlinesco/bamboo_smtp/blob/master/LICENSE)
[![Last Updated](https://img.shields.io/github/last-commit/fewlinesco/bamboo_smtp.svg)](https://github.com/fewlinesco/bamboo_smtp/commits/master)

# Bamboo.SMTPAdapter

An Adapter for the [Bamboo](https://github.com/thoughtbot/bamboo) email app.
An adapter for the [Bamboo](https://github.com/thoughtbot/bamboo) email app.

## Installation

The package can be installed as:

1. Add `bamboo_smtp` to your list of dependencies in `mix.exs`:

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

2. Add `bamboo` and `bamboo_smtp` to your list of applications in `mix.exs`:
2. Add `:bamboo` and `:bamboo_smtp` to your list of applications in `mix.exs`:

```elixir
def application do
[applications: [:bamboo, :bamboo_smtp]]
end
```
```elixir
def application do
[applications: [:bamboo, :bamboo_smtp]]
end
```

3. Setup your SMTP configuration:

```elixir
# In your config/config.exs file
config :my_app, MyApp.Mailer,
adapter: Bamboo.SMTPAdapter,
server: "smtp.domain",
hostname: "your.domain",
port: 1025,
username: "[email protected]", # or {:system, "SMTP_USERNAME"}
password: "pa55word", # or {:system, "SMTP_PASSWORD"}
tls: :if_available, # can be `:always` or `:never`
allowed_tls_versions: [:"tlsv1", :"tlsv1.1", :"tlsv1.2"], # or {:system, "ALLOWED_TLS_VERSIONS"} w/ comma seprated values (e.g. "tlsv1.1,tlsv1.2")
ssl: false, # can be `true`
retries: 1,
no_mx_lookups: false, # can be `true`
auth: :if_available # can be `:always`. If your smtp relay requires authentication set it to `:always`.
```

*Sensitive credentials should not be committed to source control and are best kept in environment variables.
Using `{:system, "ENV_NAME"}` configuration is read from the named environment variable at runtime.*

The *hostname* option sets the FQDN to the header of your emails, its optional, but if you don't set it, the underlying `gen_smtp` module will use the hostname of your machine, like `localhost`.
```elixir
# In your config/config.exs file
config :my_app, MyApp.Mailer,
adapter: Bamboo.SMTPAdapter,
server: "smtp.domain",
hostname: "your.domain",
port: 1025,
username: "[email protected]", # or {:system, "SMTP_USERNAME"}
password: "pa55word", # or {:system, "SMTP_PASSWORD"}
tls: :if_available, # can be `:always` or `:never`
allowed_tls_versions: [:"tlsv1", :"tlsv1.1", :"tlsv1.2"], # or {:system, "ALLOWED_TLS_VERSIONS"} w/ comma seprated values (e.g. "tlsv1.1,tlsv1.2")
ssl: false, # can be `true`
retries: 1,
no_mx_lookups: false, # can be `true`
auth: :if_available # can be `:always`. If your smtp relay requires authentication set it to `:always`.
```

*Sensitive credentials should not be committed to source control and are best kept in environment variables.
Using `{:system, "ENV_NAME"}` configuration is read from the named environment variable at runtime.*

The *hostname* option sets the FQDN to the header of your emails, its optional, but if you don't set it, the underlying `gen_smtp` module will use the hostname of your machine, like `localhost`.

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
```
```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
12 changes: 1 addition & 11 deletions lib/bamboo/adapters/smtp_adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -202,17 +202,7 @@ defmodule Bamboo.SMTPAdapter do
end

defp rfc822_encode(content) do
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?()
"=?UTF-8?B?#{Base.encode64(content)}?="
end

def base64_and_split(data) do
Expand Down
27 changes: 22 additions & 5 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ defmodule BambooSmtp.Mixfile do
use Mix.Project

@project_url "https://github.com/fewlinesco/bamboo_smtp"
@version "3.1.1"

def project do
[
app: :bamboo_smtp,
version: "3.1.0",
version: @version,
elixir: "~> 1.7",
source_url: @project_url,
homepage_url: @project_url,
Expand All @@ -17,7 +18,7 @@ defmodule BambooSmtp.Mixfile do
test_coverage: [tool: ExCoveralls],
package: package(),
deps: deps(),
docs: [main: "readme", extras: ["README.md"]]
docs: docs()
]
end

Expand All @@ -27,12 +28,17 @@ defmodule BambooSmtp.Mixfile do

defp deps do
[
# core
{:bamboo, "~> 1.6"},
{:gen_smtp, "~> 1.0.1"},

# dev / test
{:credo, "~> 1.4.1", only: [:dev, :test]},
{:earmark, ">= 1.3.2", only: :docs},
{:excoveralls, "~> 0.13.3", only: :test},

# doc
{:earmark, ">= 1.3.2", only: :docs},
{:ex_doc, ex_doc_version(), only: :docs},
{:gen_smtp, "~> 1.0.1"},
{:inch_ex, "~> 2.0.0", only: :docs}
]
end
Expand All @@ -49,7 +55,18 @@ defmodule BambooSmtp.Mixfile do
[
maintainers: ["Kevin Disneur", "Thomas Gautier"],
licenses: ["MIT"],
links: %{"GitHub" => @project_url}
links: %{
"Changelog" => "#{@project_url}/blob/master/CHANGELOG.md",
"GitHub" => @project_url
}
]
end

defp docs do
[
main: "readme",
source_ref: "v#{@version}",
extras: ["README.md", "CHANGELOG.md": [title: "Changelog"]]
]
end
end
35 changes: 8 additions & 27 deletions test/lib/bamboo/adapters/smtp_adapter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -434,8 +434,7 @@ defmodule Bamboo.SMTPAdapterTest do
assert format_email_as_string(bamboo_email.from, false) == from
assert format_email(bamboo_email.to ++ bamboo_email.cc ++ bamboo_email.bcc, false) == to

rfc822_subject = "Subject: =?UTF-8?B?Hello from Bamboo?=\r\n"
assert String.contains?(raw_email, rfc822_subject)
assert String.contains?(raw_email, "Subject: #{rfc822_encode("Hello from Bamboo")}")

assert String.contains?(raw_email, "From: #{format_email_as_string(bamboo_email.from)}\r\n")
assert String.contains?(raw_email, "To: #{format_email_as_string(bamboo_email.to)}\r\n")
Expand Down Expand Up @@ -473,8 +472,8 @@ defmodule Bamboo.SMTPAdapterTest do

[{{_from, _to, raw_email}, gen_smtp_config}] = FakeGenSMTP.fetch_sent_emails()

rfc822_subject = "Subject: \r\n"
assert String.contains?(raw_email, rfc822_subject)
plain_text = "Subject: \r\n"
assert String.contains?(raw_email, plain_text)

assert_configuration(bamboo_config, gen_smtp_config)
end
Expand All @@ -499,9 +498,7 @@ defmodule Bamboo.SMTPAdapterTest do
assert format_email_as_string(bamboo_email.from, false) == from
assert format_email(bamboo_email.to ++ bamboo_email.cc ++ bamboo_email.bcc, false) == to

rfc822_subject = "Subject: =?UTF-8?B?Hello from Bamboo?=\r\n"
assert String.contains?(raw_email, rfc822_subject)

assert String.contains?(raw_email, "Subject: #{rfc822_encode("Hello from Bamboo")}")
assert String.contains?(raw_email, "From: #{format_email_as_string(bamboo_email.from)}\r\n")
assert String.contains?(raw_email, "To: #{format_email_as_string(bamboo_email.to)}\r\n")
assert String.contains?(raw_email, "Cc: #{format_email_as_string(bamboo_email.cc)}\r\n")
Expand Down Expand Up @@ -547,9 +544,7 @@ defmodule Bamboo.SMTPAdapterTest do
assert format_email_as_string(bamboo_email.from, false) == from
assert format_email(bamboo_email.to ++ bamboo_email.cc ++ bamboo_email.bcc, false) == to

rfc822_subject = "Subject: =?UTF-8?B?Hello from Bamboo?=\r\n"
assert String.contains?(raw_email, rfc822_subject)

assert String.contains?(raw_email, "Subject: #{rfc822_encode("Hello from Bamboo")}")
assert String.contains?(raw_email, "From: #{format_email_as_string(bamboo_email.from)}\r\n")
assert String.contains?(raw_email, "To: #{format_email_as_string(bamboo_email.to)}\r\n")
assert String.contains?(raw_email, "Cc: #{format_email_as_string(bamboo_email.cc)}\r\n")
Expand Down Expand Up @@ -597,9 +592,7 @@ defmodule Bamboo.SMTPAdapterTest do
assert format_email_as_string(bamboo_email.from, false) == from
assert format_email(bamboo_email.to ++ bamboo_email.cc ++ bamboo_email.bcc, false) == to

rfc822_subject = "Subject: =?UTF-8?B?Hello from Bamboo?=\r\n"
assert String.contains?(raw_email, rfc822_subject)

assert String.contains?(raw_email, "Subject: #{rfc822_encode("Hello from Bamboo")}")
assert String.contains?(raw_email, "From: #{format_email_as_string(bamboo_email.from)}\r\n")
assert String.contains?(raw_email, "To: #{format_email_as_string(bamboo_email.to)}\r\n")
assert String.contains?(raw_email, "Cc: #{format_email_as_string(bamboo_email.cc)}\r\n")
Expand Down Expand Up @@ -646,9 +639,7 @@ defmodule Bamboo.SMTPAdapterTest do
assert format_email_as_string(bamboo_email.from, false) == from
assert format_email(bamboo_email.to ++ bamboo_email.cc ++ bamboo_email.bcc, false) == to

rfc822_subject = "Subject: =?UTF-8?B?Hello from Bamboo?=\r\n"
assert String.contains?(raw_email, rfc822_subject)

assert String.contains?(raw_email, "Subject: #{rfc822_encode("Hello from Bamboo")}")
assert String.contains?(raw_email, "From: #{format_email_as_string(bamboo_email.from)}\r\n")
assert String.contains?(raw_email, "To: #{format_email_as_string(bamboo_email.to)}\r\n")
refute String.contains?(raw_email, "Cc: #{format_email_as_string(bamboo_email.cc)}\r\n")
Expand Down Expand Up @@ -786,17 +777,7 @@ defmodule Bamboo.SMTPAdapterTest do
end

defp rfc822_encode(content) do
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?()
"=?UTF-8?B?#{Base.encode64(content)}?="
end

defp assert_configuration(bamboo_config, gen_smtp_config) do
Expand Down

0 comments on commit a6c9b58

Please sign in to comment.