问题
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.
回答1:
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.
回答2:
Have you tried looking at the following set up Mandrill SMTP Integration. Hope it helps
回答3:
You can also check if the ENV variables are set in the first place.
回答4:
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.
回答5:
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 :)
来源:https://stackoverflow.com/questions/13963795/rails-mailer-netsmtpserverbusy