2015-08-15 113 views
0

我在使用郵箱gem的Heroku上嘗試設置郵件的電子郵件通知。500發送電子郵件通知時發生內部服務器錯誤

郵件已創建,但嘗試通過電子郵件通知聯繫收件人時超時。

消息控制器:

def create 
    @recipient = User.find(params[:user]) 
    current_user.send_message(@recipient, params[:body], params[:subject]) 
    flash[:notice] = "Message has been sent!" 
    if request.xhr? 
     render :json => {:notice => flash[:notice]} 
    else 
     redirect_to :conversations 
    end 
    end 

Mailboxer.rb:

Mailboxer.setup do |config| 

    #Configures if you applications uses or no the email sending for Notifications and Messages 
    config.uses_emails = true 

    #Configures the default from for the email sent for Messages and Notifications of Mailboxer 
    config.default_from = "[email protected]" 

    #Configures the methods needed by mailboxer 
    config.email_method = :mailboxer_email 
    config.name_method = :name 

    #Configures if you use or not a search engine and wich one are you using 
    #Supported enignes: [:solr,:sphinx] 
    config.search_enabled = false 
    config.search_engine = :sphinx 
end 

用戶模型:

def mailboxer_email(object) 
    if self.no_email 
     email 
    else 
     nil 
    end 
end 

Production.rb:

config.action_mailer.delivery_method = :smtp 
    config.action_mailer.default_url_options = { :host => 'domain.herokuapp.com' } 
    config.action_mailer.smtp_settings = { 
    address:    'smtp.gmail.com', 
    port:     587, 
    domain:    'gmail.com', 
    user_name:   '[email protected]', 
    password:    'password', 
    authentication:  'plain', 
    enable_starttls_auto: false } 

    config.action_mailer.default_url_options = { :host => "domain.herokuapp.com" } 

    config.action_mailer.perform_deliveries = true 

日誌:

Aug 14 12:11:23 domain app/web.1: Parameters: {"utf8"=>"✓", "user"=>"7", "subject"=>"inhereyes messaged you", "body"=>"Will the email send this time as notification?", "commit"=>"Send"} 
Aug 14 12:11:23 domain app/web.1: Rendered vendor/bundle/ruby/2.0.0/gems/mailboxer-0.11.0/app/views/message_mailer/new_message_email.html.erb (0.6ms) 
Aug 14 12:11:23 domain app/web.1: Rendered vendor/bundle/ruby/2.0.0/gems/mailboxer-0.11.0/app/views/message_mailer/new_message_email.html.erb (0.6ms) 
Aug 14 12:11:23 domain app/web.1: Rendered vendor/bundle/ruby/2.0.0/gems/mailboxer-0.11.0/app/views/message_mailer/new_message_email.text.erb (0.2ms) 
Aug 14 12:11:23 domain app/web.1: Rendered vendor/bundle/ruby/2.0.0/gems/mailboxer-0.11.0/app/views/message_mailer/new_message_email.text.erb (0.2ms) 
Aug 14 12:11:52 domain heroku/router: at=error code=H12 desc="Request timeout" method=POST path="/messages" host=domain.com request_id=b00e5acf-3342-4e80-913e-610c5be6d83c fwd="73.54.214.248,108.162.237.126" dyno=web.1 connect=0ms service=30001ms status=503 bytes=0 
Aug 14 12:12:23 domain app/web.1: Sent mail to [email protected] (60151.4ms) 
Aug 14 12:12:23 domain app/web.1: Sent mail to [email protected] (60151.4ms) 
Aug 14 12:12:23 domain app/web.1: Completed 500 Internal Server Error in 60210ms 
Aug 14 12:12:23 domain app/web.1: Completed 500 Internal Server Error in 60210ms 
Aug 14 12:12:23 domain app/web.1: Net::ReadTimeout (Net::ReadTimeout): 
Aug 14 12:12:23 domain app/web.1: app/controllers/messages_controller.rb:15:in `create' 
Aug 14 12:12:23 domain app/web.1: app/controllers/application_controller.rb:27:in `user_time_zone' 
Aug 14 12:12:23 domain app/web.1: Net::ReadTimeout (Net::ReadTimeout): 
Aug 14 12:12:23 domain app/web.1: app/controllers/messages_controller.rb:15:in `create' 
Aug 14 12:12:23 domain app/web.1: app/controllers/application_controller.rb:27:in `user_time_zone' 
+0

我認爲這只是Heroku超時30秒後,您的請求持續多久? – 2015-08-15 22:00:23

回答

1

這指出您的電子郵件生成大約需要60秒,如果我沒有弄錯Heroku在30秒後超時。

Aug 14 12:12:23 domain app/web.1: Sent mail to [email protected] (60151.4ms) 
Aug 14 12:12:23 domain app/web.1: Sent mail to [email protected] (60151.4ms) 
+0

所以唯一的解決方法是運行一個後臺任務,以便它不超時heroku? – pwz2000

+0

對,或者讓你的電子郵件更快。我不知道爲什麼需要這麼長時間。 – 2015-08-16 01:06:26

+0

在Rails 4中,使用deliver_later發送帶有後臺作業的郵件要容易得多。儘管在Heroku中它意味着你必須支付dynos。 – 2015-08-16 01:08:47

0

它說的你有messages_controller.rb行號錯誤15

應用程序/控制器/ messages_controller.rb:15

+0

'current_user.send_message(@recipient,params [:body],params [:subject])' – pwz2000

1

要使用Gmail,您需要確保它能夠繞過Google的現代安全協議。

雖然我可以解釋如何做,但我建議使用Mandrill,Sendgrid,Mailjet或其他電子郵件程序,因爲Gmail只允許您每天最多可收到500個收件人的電子郵件。

個人而言,我使用Mandrill,並且喜歡在RoR之外設置電子郵件模板,運行A-B測試以及跟蹤誰打開/查看電子郵件。

+0

感謝您的諮詢。我實際上首先使用我的服務器託管,但我只是切換到gmail,因爲都沒有工作。這個問題似乎是控制者。當應用程序變大時,我會記住mandrill。 – pwz2000

相關問題