mailer error in production only

2019-06-05 09:09发布

问题:

Setup: VPS with Ubuntu 12.04, Apache, PhusionPassenger, Rail 3.2.12, Postgresql

I want to send a confirmation mail with my app. In development mode everything works fine, the user receives a mail but in production I get this error (log):

Started POST "/newsletters" for 1XX.16X.30.XX at 2013-02-26 09:22:47 +0000
Processing by NewslettersController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"XXXXXXXXXXXXX=",
"newsletter"=>{"name"=>"Test", "email"=>"test@example.com"}, "commit"=>"Submit"}

  Rendered newsletter_mailer/confirmation.text.erb (0.4ms)

Sent mail to test@example.com (44ms)
Completed 500 Internal Server Error in 134ms

Errno::ECONNREFUSED (Connection refused - connect(2)):
  app/controllers/newsletters_controller.rb:45:in `create'

So I guess, error should be in newsletters_controller.rb (line 45 is marked):

def create
    @newsletter = Newsletter.new(params[:newsletter])

    if @newsletter.save
      NewsletterMailer.confirmation(@newsletter.email).deliver ### line 45
      flash[:success] = 'It works!'
      redirect_to root_path
    else
      flash[:error] = 'Error!'
      render action: "new"
    end
end

NewsletterMailer.rb

class NewsletterMailer < ActionMailer::Base
  default from: "some@email.com"

  def confirmation(email)
    mail to: email,
         subject: "Welcome"
  end
end

Again, it works only in development but not in production. I also tried to change the database to Mysql2 but the same error occurs.

My database.yml:

production:
  adapter: postgresql
  encoding: unicode
  reconnect: false
  database: app_production
  pool: 5
  username: user
  password: secret
  host: localhost

For mailing I use smtp with mandrill or gmail. Gmail-Settings work well in another app...

My environment.rb

# Load the rails application
require File.expand_path('../application', __FILE__)

# Initialize the rails application
LandingPage::Application.initialize!

LandingPage::Application.configure do 
  config.action_mailer.delivery_method = :smtp

  config.action_mailer.smtp_settings = {
    :address   => "smtp.mandrillapp.com",
    :port      => 587, # or 25
    :enable_starttls_auto => true, # detects and uses STARTTLS
    :user_name => "user",
    :password  => "password",
    :authentication => 'login' # Mandrill supports 'plain' or 'login'
}
end

UPDATE: I found out that the submits get saved in my index. So I guess the DB works.

Any advice? Thanks

回答1:

I solved my problem with a change to a new passwort (api-key) from mandrill. Still don't know what the problem was, because with the old one it worked in development mode...

The working setting:

YourApp::Application.configure do
  config.action_mailer.smtp_settings = {
    :address   => "smtp.mandrillapp.com",
    :port      => 587,
    :enable_starttls_auto => true,
    :user_name => "MANDRILL_USERNAME",
    :password  => "MANDRILL_PASSWORD", # SMTP password is any valid API key
    :authentication => 'login'
  }