chrisdel101
Sending email 404 not found - nothing is happening, no debug messages
Edit: according to chatGTP the error is likely be on the mailgun connection, so therefore it’s the syntax I’m using, but not the mailgun creds. i.e Phoenix is calling the mailgun URL properly.
I’m trying to send an email but nothing happens: errors, logs, anything. Email is not received. I’ve confirmed that the Mailgun process does work with curl.
I can’t find a step-by-step tutorial on how to do this. So far I’ve cobbled together only this.
So I am trying it from iex
#mailer.ex
defmodule TurnStile.Mailer do
use Swoosh.Mailer, otp_app: :turnStile
end
#conig.exs
config :turnStile, TurnStile.Mailer,
adapter: Swoosh.Adapters.Mailgun,
domain: System.get_env("MAILGUN_DOMAIN"),
api_key: System.get_env("MAILGUN_API_KEY")
config :swoosh, :api_client, Swoosh.ApiClient.Hackney
There is also some mailer stuff in runtime.exs, so the above also exist there too. I also don’t know what hackney is but I think it’s needed?
#notifier (take from swoosh docs)
defmodule TurnStile.Staff.EmployeeNotifier do
import Swoosh.Email
alias TurnStile.Mailer
def welcome(user) do
new()
|> to({user.name, user.email})
|> from({"Dr B Banner", "#{System.get_env("MAILGUN_DOMAIN")}/messages"})
|> subject("Hello, Avengers!")
|> html_body("<h1>Hello #{user.name}</h1>")
|> text_body("Hello #{user.name}\n")
end
Not sure what goes in the from section. Above is one version of what I’ve tried.
In iex I try
email = TurnStile.Staff.EmployeeNotifier.welcome(%{name: "Tony Stark", email: "working@email.com"})
TurnStile.Mailer.deliver(email)
I get:
{:error, {404, "404 page not found\n"}}
EDIT: in case it helps, this is how it works from curl
curl -s --user 'API_KEY' \
https://api.mailgun.net/v3/sandbox123456.mailgun.org/messages \
-F from='Excited User <mailgun@sandbox123456.mailgun.org>' \
-F to=working@email.com \
-F subject='Hello' \
-F text='Testing some Mailgun awesomeness!'
Marked As Solved
chrisdel101
The issue was with the domain and with the from field, and error was occuring in Swoosh. To many, these might be super obvious items, but maybe pointing them out will help other email novices. They were not obv to me,
-
If full email URL is
https://api.mailgun.net/v3/sandbox123456.mailgun.org -
Then domain in the config is:
domain: sandbox123456.mailgun.org. It is not the full URL. -
In the
fromfield for thenew()when making a new email (seewelcomefunc above), this is also different from the full domain. It needs to include themailpart:
from({"Dr B Banner", "mailgun@sandbox123456.mailgun.org"})
Feedback to Swoosh: Better documentation would have made such obvious mistakes easier to correct. Also better error messages than just 404 not found. ![]()








