Unable to send email using Action Mailer in production

1k views Asked by At

I'm having some trouble sending email using ActionMailer with Gmail. I've attempted to use Mandrill but it's still not sending. When testing ActionMailer with gmail it works in development only but in production there seems to be an issue.

I've tried multiple solutions but nothing seems to be working. I've tried setting up action mailer settings in environment.rb, swapped from gmail to mandrill. I can't seem to figure out what I'm missing.

Let me know what you think. I've provided the code snippets below

production.rb:

config.action_mailer.default_url_options = { :host => ENV["HEROKU_DOMAIN"] }
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = false
config.action_mailer.default :charset => "utf-8"
config.action_mailer.smtp_settings = {
  address: "smtp.mandrillapp.com",
  port: 587,
  domain: ENV["HEROKU_DOMAIN"],
  authentication: "plain",
  enable_starttls_auto: true,
  user_name: ENV["HEROKU_USERNAME"],
  password: ENV["MANDRILL_KEY"]
}

email_signup.rb:

class EmailSignup < ApplicationMailer
  default from: "[email protected]"

  def sample_email(user)
    @user = user

    mail subject: "test", to: user.email
  end
end

users_controller.rb:

def create
  @user = User.new(user_params)

  if @user.save
    @user.save
    EmailSignup.sample_email(@user).deliver
    redirect_to thanks_path
  else
    redirect_to root_path
  end
end
2

There are 2 answers

0
Kerrison Garcia On BEST ANSWER

Neo's solution solved my problem.

All I did was set the environment variables on Heroku.

$ heroku config:set HEROKU_USERNAME = ...

and all the other variables and that fixed it.

0
Ojash On

Try this config instead, i think there is a problem with default_url_options and smtp domain name. Also you can omit other configs you have.

config.action_mailer.default_url_options = { host: ENV['DEFAULT_URL_OPTIONS'] }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  address: 'smtp.mandrillapp.com',
  port: 587,
  domain: 'heroku.com',
  user_name: ENV['MANDRILL_USERNAME'],
  password: ENV['MANDRILL_APIKEY'],
  authentication: 'plain'
}

Your default url options should be the full domain name of your server eg. mytestapp.herokuapp.com