RuNpiXelruN
RuNpiXelruN

Reputation: 1920

Heroku/Rails App Error

I'm really stuck here and am worried my mailer config files are all messed up. My app seems to work fine locally, I'm on Nitrous. I can git push heroku master fine and when opened can view the home page, which is just a form(https://nameless-taiga-1654.herokuapp.com/), however once I hit submit I get an error message. I believe all my mailer actions on nitrous are setup correctly, I was planning on using sendgrid via heroku to run the mailing side of things. Basically I'm just lost and need help. Attached are the heroku logs of the form error.

heroku logs -t

2015-08-07T11:00:54.501756+00:00 heroku[router]: at=info method=POST path="/contacts" host=nameless-taiga-1654.herokuapp.com request_id=452df9b2-aef8-4a23-9ffb-3ba6781d80
75 fwd="124.149.33.78" dyno=web.1 connect=1ms service=72ms status=500 bytes=1754
2015-08-07T11:00:54.433019+00:00 app[web.1]: Started POST "/contacts" for 124.149.33.78 at 2015-08-07 11:00:54 +0000
2015-08-07T11:00:54.437794+00:00 app[web.1]:   Parameters: {"utf8"=>"✓", "authenticity_token"=>"Z2Qh/GDIycId0uJk2evhblbKisGXNlIzjtz+xfJoyac=", "contact"=>{"name"=>"John D
oe", "phone"=>"040000000", "email"=>"[email protected]", "event_type"=>"Corporate", "comments"=>"Test Comments"}, "commit"=>"Submit"}
2015-08-07T11:00:54.491841+00:00 app[web.1]:
2015-08-07T11:00:54.491846+00:00 app[web.1]: Sent mail to [email protected] (21.8ms)
2015-08-07T11:00:54.491848+00:00 app[web.1]: Completed 500 Internal Server Error in 53ms
2015-08-07T11:00:54.492821+00:00 app[web.1]:
2015-08-07T11:00:54.492824+00:00 app[web.1]: Errno::ECONNREFUSED (Connection refused - connect(2)):
2015-08-07T11:00:54.436340+00:00 app[web.1]: Processing by ContactsController#create as HTML
2015-08-07T11:00:54.492826+00:00 app[web.1]:   app/mailers/contact_mailer.rb:8:in `contact_email'
2015-08-07T11:00:54.463333+00:00 app[web.1]:   Rendered contact_mailer/contact_email.html.erb (0.3ms)
2015-08-07T11:00:54.492828+00:00 app[web.1]:   app/controllers/contacts_controller.rb:9:in `create'
2015-08-07T11:00:54.492831+00:00 app[web.1]:
2015-08-07T11:00:54.492830+00:00 app[web.1]:
2015-08-07T11:00:54.492827+00:00 app[web.1]:   app/models/contact.rb:7:in `send_email'

contacts_controller.rb

class ContactsController < ApplicationController
  def new
    @contact = Contact.new
  end

  def create
    @contact = Contact.new(contact_params)

    if @contact.save
      flash[:success] = 'Message Sent.'
      redirect_to new_contact_path
    else
      flash[:danger] = 'Error occurred, messgage not sent.'
      redirect_to new_contact_path
    end
  end
end

models/contact.rb

class Contact < ActiveRecord::Base
  validates :name, :email, presence: true
  after_create :send_email

  private
  def send_email
    ContactMailer.contact_email(self).deliver
  end
end

mailers/contact_mailer.rb

class ContactMailer < ActionMailer::Base
  default to: '[email protected]'


  def contact_email(contact)
   @contact = contact

    mail(from: @contact.email, subject: 'Contact Form Message').deliver
  end
end

views/contact_mailer/contact_email.html.erb

<!DOCTYPE html>
<html>
  <head>
    <title></title>
  </head>
  <body>
    <p>New Message from Hoot and Holla's Contact form, from <%= "#{@contact.name}, #{@contact.email}" %></p>
    <p><%= @contact.phone %></p>
    <p><%= @contact.event_type %></p>
    <p><%= @contact.comments %></p>
  </body>
</html>

application.html.erb

<!DOCTYPE html>
<html>
<head>
  <title>HootandhollaLp</title>
  <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
  <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
  <%= csrf_meta_tags %>
</head>
<body>

  <div class="container">

    <% flash.each do |key, value| %>
      <div class="alert alert-<%= key %> alert-dismissible">
    <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <%= value %>
     </div>
    <% end %>

    <%= yield %>
  </div>

</body>
</html>

@Pavan @alexsmn I hope this helps?


config/environments/production.rb

Rails.application.configure do
  # Settings specified here will take precedence over those in config/application.rb.

  # Code is not reloaded between requests.
  config.cache_classes = true

  # Eager load code on boot. This eager loads most of Rails and
  # your application in memory, allowing both threaded web servers
  # and those relying on copy on write to perform better.
  # Rake tasks automatically ignore this option for performance.
  config.eager_load = true

  # Full error reports are disabled and caching is turned on.
  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true

  # Enable Rack::Cache to put a simple HTTP cache in front of your application
  # Add `rack-cache` to your Gemfile before enabling this.
  # For large-scale production use, consider using a caching reverse proxy like nginx, varnish or squid.
  # config.action_dispatch.rack_cache = true

  # Disable Rails's static asset server (Apache or nginx will already do this).
  config.serve_static_assets = false

  # Compress JavaScripts and CSS.
  config.assets.js_compressor = :uglifier
  # config.assets.css_compressor = :sass

  # Do not fallback to assets pipeline if a precompiled asset is missed.
  config.assets.compile = false

  # Generate digests for assets URLs.
  config.assets.digest = true

  # Version of your assets, change this if you want to expire all your assets.
  config.assets.version = '1.0'

  # Specifies the header that your server uses for sending files.
  # config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
  # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx

  # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
  # config.force_ssl = true

  # Set to :debug to see everything in the log.
  config.log_level = :info

  # Prepend all log lines with the following tags.
  # config.log_tags = [ :subdomain, :uuid ]

  # Use a different logger for distributed setups.
  # config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)

  # Use a different cache store in production.
  # config.cache_store = :mem_cache_store

  # Enable serving of images, stylesheets, and JavaScripts from an asset server.
  # config.action_controller.asset_host = "http://assets.example.com"

  # Precompile additional assets.
  # application.js, application.css, and all non-JS/CSS in app/assets folder are already added.
  # config.assets.precompile += %w( search.js )

  # Ignore bad email addresses and do not raise email delivery errors.
  # Set this to true and configure the email server for immediate delivery to raise delivery errors.
  # config.action_mailer.raise_delivery_errors = false

  # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
  # the I18n.default_locale when a translation cannot be found).
  config.i18n.fallbacks = true

  # Send deprecation notices to registered listeners.
  config.active_support.deprecation = :notify

  # Disable automatic flushing of the log to improve performance.
  # config.autoflush_log = false

  # Use default logging formatter so that PID and timestamp are not suppressed.
  config.log_formatter = ::Logger::Formatter.new

  # Do not dump schema after migrations.
  config.active_record.dump_schema_after_migration = false
end

config/environment.rb

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

# Initialize the Rails application.
Rails.application.initialize!

ActionMailer::Base.smtp_settings = {
  :address => 'smtp.sendgrid.net',
  :port => '587',
  :authentication => :plain,
  :user_name => ENV['SENDGRID_USERNAME'],
  :password => ENV['SENDGRID_PASSWORD'],
  :domain => 'heroku.com',
  :enable_startstls_auto => true 
  }

Upvotes: 0

Views: 398

Answers (2)

Olena Chernilevska
Olena Chernilevska

Reputation: 11

I have had the same problem working with my current project. Actually, as I found out, this is a Gmail authentication problem. Your app is trying to login into Gmail account and cannot do it because Gmail does not see or recognize encrypted email and password. I temporarily solved this problem by writing necessary values into my smtp_settings directly, like:

    smtp_settings = {
      ...
      :user_name => "...your gmail email...",
      :password => "...your gmail password...",
      ...
    }

Also, I changed Gmail account security settings: turned off double authentication and allowed my account to be accessed by less secure apps. So, currently the app is deployed on Heroku and working. However, from cyber security point of view my solution is rather dangerous, so I am still looking for better ones.

EDITED!!! There is a better way to encrypt gmail account settings. In my .bashrc file (is hidden in computer root directory - press Ctrl+H to reveal) I wrote the following lines of code:

    export GMAIL_NAME=necessary value here
    export GMAIL_PASSWORD=necessary value here

After this I also changed my smtp_settings one more time, like:

    smtp_settings = {
      ...
      user_name: ENV['GMAIL_NAME'],
      password: ENV['GMAIL_PASSWORD'],
      ...
    } 

Finally, I configured repo setting on Heroku: Heroku/Personal apps/myApp/Settings, section "Config variables" via adding two new variables GMAIL_NAME and GMAIL_PASSWORD with necessary values.

It worked.

Upvotes: 1

Jerry
Jerry

Reputation: 884

Try setting the following in your production.rb file:

config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.default_url_options = { host: 'nameless-taiga-1654.herokuapp.com' }

Also, to verify the configuration is correct, you can run heroku config.

Upvotes: 0

Related Questions