Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for replyto parameter with SendGrid #254

Merged
merged 8 commits into from
Mar 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/bamboo/adapters/send_grid_adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ defmodule Bamboo.SendGridAdapter do
Use this adapter to send emails through SendGrid's API. Requires that an API
key is set in the config.

If you would like to add a replyto header to your email, then simply pass it in
using the header property or put_header function like so:

put_header("reply-to", "foo@bar.com")

## Example config

# In config/config.exs, or config.prod.exs, etc.
Expand Down Expand Up @@ -110,6 +115,7 @@ defmodule Bamboo.SendGridAdapter do
%{}
|> put_from(email)
|> put_to(email)
|> put_reply_to(email)
|> put_cc(email)
|> put_bcc(email)
|> put_subject(email)
Expand Down Expand Up @@ -156,6 +162,11 @@ defmodule Bamboo.SendGridAdapter do
defp put_text_body(body, %Email{text_body: nil}), do: body
defp put_text_body(body, %Email{text_body: text_body}), do: Map.put(body, :text, text_body)

defp put_reply_to(body, %Email{headers: %{"reply-to" => reply_to}} = email) do
Map.put(body, :replyto, reply_to)
end
defp put_reply_to(body, _), do: body

defp maybe_put_x_smtp_api(body, %Email{private: %{"x-smtpapi" => fields}} = email) do
# SendGrid will error with empty bodies, even while using templates.
# Sets a default `text_body` and 'html_body' if either are not specified,
Expand Down
8 changes: 4 additions & 4 deletions lib/bamboo/email.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ defmodule Bamboo.Email do

defmodule MyApp.Email do
import Bamboo.Email

def welcome_email(user) do
new_email(
from: "me@app.com",
Expand All @@ -39,7 +39,7 @@ defmodule Bamboo.Email do

defmodule MyApp.Email do
import Bamboo.Email

def welcome_email(user) do
# Since new_email/1 returns a struct you can update it with Kernel.struct!/2
struct!(base_email,
Expand All @@ -48,15 +48,15 @@ defmodule Bamboo.Email do
text_body: "Welcome to the app",
html_body: "<strong>Welcome to the app</strong>"
)

# or you can use functions to build it up step by step
base_email
|> to(user)
|> subject("Welcome!")
|> text_body("Welcome to the app")
|> html_body("<strong>Welcome to the app</strong>")
end

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catches :)

def base_email do
new_email(from: "me@app.com")
end
Expand Down
9 changes: 9 additions & 0 deletions test/lib/bamboo/adapters/send_grid_adapter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,15 @@ defmodule Bamboo.SendGridAdapterTest do
}}
end

test "deliver/2 correctly formats reply-to from headers" do
email = new_email(headers: %{"reply-to" => "foo@bar.com"})

email |> SendGridAdapter.deliver(@config)

assert_receive {:fake_sendgrid, %{params: params}}
assert params["replyto"] == "foo@bar.com"
end

test "raises if the response is not a success" do
email = new_email(from: "INVALID_EMAIL")

Expand Down