7

With my rails site, when I try to send mail through GMail, it works perfectly. But when I try to send it through MandrillApp, it gives the following error (RController.create is where the deliver command is called):

Net::SMTPServerBusy in RController#create 
454 4.7.1 <recipient@gmail.com>: Relay access denied

Here's my config/environments/development.rb file:

 # ActionMailer Config
 config.action_mailer.default_url_options = { :host => 'localhost:3000' }
 config.action_mailer.delivery_method = :smtp
config.action_mailer.default :charset => "utf-8"

config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true

config.action_mailer.delivery_method = :smtp
# config.action_mailer.smtp_settings = {
#   :address              => "smtp.gmail.com",
#   :port                 => 587,
#   :domain               => 'gmail.com',
#   :user_name            => 'sender@gmail.com',
#   :password             => 'password',
#   :authentication       => 'plain',
#   :enable_starttls_auto => true  }

config.action_mailer.smtp_settings = {
:address   => "smtp.mandrillapp.com",
:port      => 587,
:user_name => ENV["EMAIL"],
:password  => ENV["PASSWORD"]
}

As it is above, the code doesn't work - I don't get an email and no errors pop up. If I switch to sending it from GMail, I get an email almost instantly. I've never worked with Mandrill before, so any help would be appreciated.

camdroid
  • 524
  • 7
  • 24

5 Answers5

5

I ended up talking to a service rep from Mandrill, and it turns out that I was using the actual strings instead of the environment variables, so I just had to remove "ENV" from the config file.

I ended up going back and making environment variables for security's sake, but I just thought I'd throw that out there in case anyone else has the same problem and finds this question.

camdroid
  • 524
  • 7
  • 24
  • Thanks this worked for me. You should accept your own answer. I had to do get rid of the `ENV` and the `[]` like `"EMAIL"`. – LearningRoR Jan 30 '13 at 01:07
  • Can you clarify your solution? I currently have this line in my smtp_settings - :password => ENV['MANDRILL_API_KEY']. According to your solution, how should this line read? – ajporterfield Apr 05 '13 at 03:48
  • If you are defining MANDRILL_API_KEY as a program variable, rather than an environment variable, then you just need: `password => MANDRILL_API_KEY`, instead of `password => ENV['MANDRILL_API_KEY']`. An environment variable is more secure, since your API key will be stored on your computer/server instead of in your site (basically, someone could get hold of your code, and still not be able to get your key), but a normal variable is easier to implement - depends how concerned you are with security (although, properly, you should do an environment variable). – camdroid Apr 06 '13 at 04:25
  • after plenty of research I finally found your solution <3 :) – davegson Sep 03 '13 at 15:11
2

Have you tried looking at the following set up Mandrill SMTP Integration. Hope it helps

Deej
  • 5,334
  • 12
  • 44
  • 68
  • Thanks for the help (I was missing the authentication line), but it doesn't get rid of the error. When I try to send an email, I still get the Net::SMTPServerBusy error. – camdroid Dec 20 '12 at 18:12
1

You can also check if the ENV variables are set in the first place.

Nesha Zoric
  • 6,218
  • 42
  • 34
1

If you are using Heroku, you need to set the environment variables in the CLI.

cd to your working directory and...

heroku config:set MANDRILL_USERNAME='YOUR USERNAME'
heroku config:set MANDRILL_APIKEY="YOUR API KEY"

You can now access the environment variables. I know you're working with development.rb but this might save you a headache when you get to production.rb. Caused me a headache for a few hours with my application.yml file.

dominathan
  • 61
  • 1
  • 6
0

I have a suspicion that your problem is caused by Phusion Passenger (hence in production). Passenger is notorious for not setting environment variables.

This SO issue suggests 2 solutions : hardcoding values, or overriding the ruby wrapper used by Passenger.

I'd suggest a third option : expanding the environment variables during the deployment. It's pretty much a delayed hardcoding, but leaves your passwords out of your code.

You should execute this bit of bash right after your deploy :

mv config/environments/production.rb config/environments/production.before_sed.rb
env | sed 's/[\%]/\\&/g;s/\([^=]*\)=\(.*\)/s%ENV\\[\\"\1\\"\\]%\2%/' > script/expand_env_vars.sed.script
cat config/environments/production.before_sed.rb | sed -f script/expand_env_vars.sed.script > config/environments/production.rb

Here is the equivalent Capistrano deploy task (lot of escaping ahead !) :

desc "Replace environment variables with hardcoded values in config files"
task :replace_env_vars, roles: :app do
    run "mv #{release_path}/config/environments/production.rb #{release_path}/config/environments/production.before_sed.rb"
    run 'env | sed \'s/[\%]/\\&/g;s/\([^=]*\)=\(.*\)/s%ENV\\\[\\\"\1\\\"\\\]%\\\"\2\\\"%/\' > ' + "#{release_path}/script/expand_env_vars.sed.script"
    run "cat #{release_path}/config/environments/production.before_sed.rb | sed -f #{release_path}/script/expand_env_vars.sed.script > #{release_path}/config/environments/production.rb"
end

after "deploy:update_code", "deploy:replace_env_vars"

Still for Capistrano, you don't have the same environment variables set in the SSH session it uses to deploy (it doesn't execute .bashrc, /etc/profile ...). You have to re-export them in ~/.ssh/environment and append the following option in /etc/ssh/sshd_config :

PermitUserEnvironment yes

These last instructions were found there. I've personally documented a bit further my issue on my new blog.

Hope this can help fix some security issues for you :)

Community
  • 1
  • 1