Question

I have a rails app that has email authorisation issues in production. The error is:

Net::SMTPAuthenticationError (530-5.5.1 Authentication Required. Learn more at ):
app/models/user.rb:53:in `send_password_reset'
app/controllers/password_resets_controller.rb:15:in `create'

My email settings are correct and they work correctly, both in staging and development, in addition, running the commands that are executed from the production server in the console works correctly.

It is only when running it through the web interface of my production server that the problem appears, is there any way Phussion Passenger or nginx can cause this type of error?

I have tried restarting the server multiple times and cannot figure out why it doesn't work. As far as I am aware there should be no difference between the commands executed in a production webserver (e.g. phusion passenger) and those executed within a production console (e.g. RAILS_ENV=production rails c) am I mistaken in that assumption?

My current thoughts are which user is running the commands as ENV['EMAIL_USERNAME'] e.t.c. is defined in my .zshrc, but I assumed that was getting sourced by phussion passenger as I have a password: <%= ENV['POSTGRES_PASSWORD'] %> in my database.yml file...

any ideas greatly appreciated.

More details follow below:

production.rb configuration:

  config.action_mailer.default_url_options = {host: 'myhost.com' }

  ActionMailer::Base.smtp_settings = {
  :address              => "smtp.gmail.com",
  :port                 => 587,
  :domain               => "myhost.com",
  :user_name            => ENV["EMAIL_USERNAME"],
  :password             => ENV["EMAIL_PWD"],
  :authentication       => "plain",
  :enable_starttls_auto => true
}

password_resets_controller.rb:

  ##
  # Sends password reset email
  def create
    user =User.find_by_email(params[:email])
    user.send_password_reset if user
    redirect_to root_url, notice: "Email sent with password reset instructions."
  end

result of running those lines in a terminal on the production server with RAILS_ENV=production rails c:

[4] pry(main)> user =User.find_by_email(params[:email]); user.send_password_reset if user
  Rendered user_mailer/password_reset.html.erb (1.5ms)
  Rendered user_mailer/password_reset.html.erb (1.5ms)
  Rendered user_mailer/password_reset.text.erb (2.3ms)
  Rendered user_mailer/password_reset.text.erb (2.3ms)

Sent mail to email@address.net (1984.9ms)

Sent mail to email@address.net (1984.9ms)
Date: Mon, 03 Mar 2014 12:34:32 +0000
<snipped message content>

result of running it by clicking the button on the web interface (through the password_reset_controller):

App 1395 stdout:   Rendered user_mailer/password_reset.html.erb (0.5ms)
App 1395 stdout:   Rendered user_mailer/password_reset.text.erb (0.5ms)
App 1395 stdout: 
App 1395 stdout: Sent mail to email@address.net (188.8ms)
App 1395 stdout: Completed 500 Internal Server Error in 231ms
App 1395 stdout: 
App 1395 stdout: Net::SMTPAuthenticationError (530-5.5.1 Authentication Required. Learn more at
App 1395 stdout: ):
App 1395 stdout:   app/models/user.rb:53:in `send_password_reset'
App 1395 stdout:   app/controllers/password_resets_controller.rb:15:in `create'
Was it helpful?

Solution

Found the solution to this problem, I had forgotten that the environment variables were being read from /etc/default/nginx not my .zshrc and it was there that I needed to add them to.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top