500发送电子邮件通知时发生内部服务器错误
我在使用邮箱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'
这指出您的电子邮件生成大约需要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)
所以唯一的解决方法是运行一个后台任务,以便它不超时heroku? – pwz2000
对,或者让你的电子邮件更快。我不知道为什么需要这么长时间。 – 2015-08-16 01:06:26
在Rails 4中,使用deliver_later发送带有后台作业的邮件要容易得多。尽管在Heroku中它意味着你必须支付dynos。 – 2015-08-16 01:08:47
它说的你有messages_controller.rb行号错误15
应用程序/控制器/ messages_controller.rb:15
'current_user.send_message(@recipient,params [:body],params [:subject])' – pwz2000
要使用Gmail,您需要确保它能够绕过Google的现代安全协议。
虽然我可以解释如何做,但我建议使用Mandrill,Sendgrid,Mailjet或其他电子邮件程序,因为Gmail只允许您每天最多可收到500个收件人的电子邮件。
个人而言,我使用Mandrill,并且喜欢在RoR之外设置电子邮件模板,运行A-B测试以及跟踪谁打开/查看电子邮件。
感谢您的咨询。我实际上首先使用我的服务器托管,但我只是切换到gmail,因为都没有工作。这个问题似乎是控制者。当应用程序变大时,我会记住mandrill。 – pwz2000
我认为这只是Heroku超时30秒后,您的请求持续多久? – 2015-08-15 22:00:23