2015-08-08 113 views
0

我真的被困在這裏,擔心我的郵件配置文件都搞砸了。我的應用似乎在本地很好地工作,我在Nitrous上。我可以推動heroku大師罰款,當打開可以查看主頁,這只是一種形式(https://nameless-taiga-1654.herokuapp.com/),但是一旦我點擊提交,我收到一條錯誤消息。我相信我的所有郵件行爲都是正確設置的,我打算通過heroku使用sendgrid來運行郵件的事情。基本上我只是迷路了,需要幫助。附件是表單錯誤的heroku日誌。Heroku/Rails應用程序錯誤

Heroku的日誌-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 

型號/ 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 

郵寄/ 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 

的意見/ 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我希望這可以幫助?


配置/環境/ 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 
    } 
+0

[Errno :: ECONNREFUSED:連接被拒絕 - 連接(2)用於動作郵件程序](http://stackoverflow.com/questions/17141004/errnoeconnrefused-connection-refused-connect2-for-action-mailer) –

+0

檢查你的'config.action_mailer.smtp_settings'生產。 – alexsmn

+0

@alexsmn我剛剛將設置文件複製到原始問題中。我真的迷失在這裏,太令人沮喪了! – RuNpiXelruN

回答

0

嘗試設置在production.rb文件中的以下內容:

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

另外,要驗證配置是否正確,可以運行heroku config

1

我和我目前的項目有同樣的問題。事實上,正如我發現的那樣,這是一個Gmail身份驗證問題。您的應用正嘗試登錄Gmail帳戶,但無法執行此操作,因爲Gmail未查看或識別加密的電子郵件和密碼。我通過編寫必要的值到我smtp_settings直接,就像暫時解決了這個問題:

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

另外,我改變了Gmail帳戶的安全設置:關閉雙重認證,並允許通過安全性較低的應用程序來訪問我的帳戶。所以,目前該應用程序已部署在Heroku上並正在運行。然而,從網絡安全的角度來看,我的解決方案相當危險,所以我仍然在尋找更好的解決方案。

EDITED !!! 有一個更好的方法來加密Gmail帳戶設置。在我的 。在.bashrc文件(隱藏在電腦的根目錄 - 按Ctrl + H透露)我寫了下面的代碼行:

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

這個我也改變了我的smtp_settings一個更多的時間,像後:

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

最後,我通過添加兩個新變量GMAIL_NAME和GMAIL_PASSWORD以及必要的值,在Heroku:Heroku/Personal apps/myApp/Settings配置回購設置,「配置變量」部分。

它工作。