diff --git a/lib/bamboo/adapters/send_grid_adapter.ex b/lib/bamboo/adapters/send_grid_adapter.ex index d72c3ca9..813ca20d 100644 --- a/lib/bamboo/adapters/send_grid_adapter.ex +++ b/lib/bamboo/adapters/send_grid_adapter.ex @@ -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. @@ -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) @@ -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, diff --git a/lib/bamboo/email.ex b/lib/bamboo/email.ex index 51eee937..faf22771 100644 --- a/lib/bamboo/email.ex +++ b/lib/bamboo/email.ex @@ -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", @@ -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, @@ -48,7 +48,7 @@ defmodule Bamboo.Email do text_body: "Welcome to the app", html_body: "Welcome to the app" ) - + # or you can use functions to build it up step by step base_email |> to(user) @@ -56,7 +56,7 @@ defmodule Bamboo.Email do |> text_body("Welcome to the app") |> html_body("Welcome to the app") end - + def base_email do new_email(from: "me@app.com") end diff --git a/test/lib/bamboo/adapters/send_grid_adapter_test.exs b/test/lib/bamboo/adapters/send_grid_adapter_test.exs index 1c675f21..49ad328e 100644 --- a/test/lib/bamboo/adapters/send_grid_adapter_test.exs +++ b/test/lib/bamboo/adapters/send_grid_adapter_test.exs @@ -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")