Skip to content

Commit

Permalink
Add support for replyto parameter with SendGrid (#254)
Browse files Browse the repository at this point in the history
* add reply_to param for sendgrid

* add tests for replyto

* better documentation

* remove unecessary line

* minor cleanup in sendgrid  adapter and test

* change SendgridAdapter to SendGridAdapter

* improve module doc for SendGridAdapter
  • Loading branch information
cjpwrs authored and paulcsmith committed Mar 8, 2017
1 parent 9dfff3e commit 3701099
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
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
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

0 comments on commit 3701099

Please sign in to comment.