Reputation: 1
Thank you for your help. I'm new to development and Phoenix.
Recently I developed a website that sends an email when the form is submitted.
I tested the application in the iex enviroment and works just fine, it sends just one email with the information from the form. But when I deploy the application to Heroku, the email gets sent twice and I can't figure out why.
I am using SMTP with Bamboo dependencies in my mix.exs file, and I am using Bluehost SMTP configuration.
Here's what the code looks like:
mix.exs
defp deps do
[
{:phoenix, "~> 1.3.2"},
{:phoenix_pubsub, "~> 1.0"},
{:phoenix_ecto, "~> 3.2"},
{:postgrex, ">= 0.0.0"},
{:phoenix_html, "~> 2.10"},
{:phoenix_live_reload, "~> 1.0", only: :dev},
{:gettext, "~> 0.11"},
{:bamboo, "~> 1.0"},
{:cowboy, "~> 1.0"},
{:bamboo_smtp, "~> 1.5.0"}
]
end
config.exs
config :app, App.Mailer,
adapter: Bamboo.SMTPAdapter,
server: "mail.app.com",
hostname: "app.com",
port: 26,
username: System.get_env("USER_ID"),
password: System.get_env("USER_PASS"),
tls: :if_available,
allowed_tls_versions: [:"tlsv1", :"tlsv1.1", :"tlsv1.2"],
ssl: false,
retries: 1,
no_mx_lookups: false,
auth: :always
email.ex
defmodule App.Email do
use Bamboo.Phoenix, view: App.EmailView
def contact_email(message) do
new_email()
|> from("[email protected]")
|> to("[email protected]")
|> put_text_layout({AppWeb.LayoutView, "email.html"})
|> subject("New lead in App")
|> assign(:message, message)
|> render("send_email.html")
|> App.Mailer.deliver_later
end
mailer.exs
defmodule App.Mailer do
use Bamboo.Mailer, otp_app: :app
end
Also, in my .gitignore file I'm ignoring my .env file that contains my environment variables, and I already set them in Heroku with:
heroku config:set VARIABLE_NAME=variable
I have no idea what could be happening and appreciate all the answers I can get. Also, if you need further information of the code, let me know.
Upvotes: 0
Views: 89
Reputation: 15535
This is just a guess, but you are probably calling App.Mailer.deliver_later
(or App.Mailer.deliver_now
) multiple times. Once from App.Email.contact_email
and once from the controller action that receives the posted form data.
If that is the case, removing App.Mailer.deliver_later
from the contact_email
function is probably the solution.
Upvotes: 1