Search code examples
phoenix-frameworkmailgunmailer

'to' parameter not valid error using Mailgun in Phoenix app via Heroku


I'm trying to follow a tut to create an mailer in my phoenix app. I've got pretty far but now I'm trying to send a test email from the iex console I get the following error:

iex(2)> Radios.Mailer.my_first_email("myemail@domain.com")
{:error, 400,
 "{\n  \"message\": \"'to' parameter is not a valid address. please check documentation\"\n}"}

mailer.ex

defmodule Radios.Mailer do
  use Mailgun.Client,
      domain: Application.get_env(:radios, :mailgun_domain),
      key: Application.get_env(:radios, :mailgun_key)

  def my_first_email(email_address) do
                send_email to: "email_address", #<= this appears to be the issue
                from: "test@example.com",
                subject: "My first email",
                text: "This is an email send with Phoenix and Mailgun"
  end
end

config.exs

config :radios,
  ecto_repos: [Radios.Repo],
  mailgun_domain: "https://api.mailgun.net/v3/XXXXXXXXX.mailgun.org",
  mailgun_key: "pubkey-XXXXXXXX"

I've been messing around changing double quotes to single, and send_email(to: :email_address) and have also added my own actual address in there as well.

All with no joy.

What am I missing?

Update Have changed config.exs to the below:

config :radios,  Radios.Mailer,
  mailgun_domain: "https://api.mailgun.net/v3/XXXXXXXX.mailgun.org",
  mailgun_key: "pubkey-XXXXXX"

But now receive:

iex(1)> Radios.Mailer.my_first_email("myemail@domain.com")
** (FunctionClauseError) no function clause matching in IO.chardata_to_string/1
    (elixir) lib/io.ex:445: IO.chardata_to_string(nil)
    (elixir) lib/path.ex:468: Path.join/2
    (elixir) lib/path.ex:450: Path.join/1
             lib/client.ex:44: Mailgun.Client.send_without_attachments/2

Solution

  • There are two mistakes:

    1. email_address is a variable but you're passing the string "email_address" to the to argument of send_email. This:

      send_email to: "email_address",
      

      should be:

      send_email to: email_address,
      
    2. You're fetching the wrong settings from env. The settings are stored in the keyword list Application.get_env(:radios, Radios.Mailer).

      This:

      use Mailgun.Client,
          domain: Application.get_env(:radios, :mailgun_domain),
          key: Application.get_env(:radios, :mailgun_key)
      

      Should be:

      use Mailgun.Client,
          domain: Application.get_env(:radios, Radios.Mailer)[:mailgun_domain],
          key: Application.get_env(:radios, Radios.Mailer)[:mailgun_key]