简体   繁体   English

传递电子邮件时,Cloudmailin从Heroku获得500

[英]Cloudmailin gets 500 from Heroku when delivering e-mails

I'm using the Cloudmailin addon to receive e-mail from my Heroku app. 我正在使用Cloudmailin插件从我的Heroku应用程序接收电子邮件。 However, Cloudmailin has not been able to deliver - or, rather, it gets 500 from Heroku every time (so the address is correct). 但是,Cloudmailin无法交付-而是每次都会从Heroku获取500(因此地址正确)。

The error in Heroku logs is Heroku日志中的错误是

Started POST "/incoming_mails" for 109.107.35.53 at 2013-02-27 08:54:22 +0000
2013-02-27T08:54:23+00:00 app[web.1]: Entering the controller! Controlling the e-mail!
2013-02-27T08:54:23+00:00 app[web.1]: 
2013-02-27T08:54:23+00:00 app[web.1]: NoMethodError (undefined method `[]' for nil:NilClass):
2013-02-27T08:54:23+00:00 app[web.1]:   app/controllers/incoming_mails_controller.rb:7:in `create'

My routing is correct; 我的路线是正确的; the "Entering the controller! Controlling the e-mail!" “进入控制器!控制电子邮件!” comes from the puts at the beginning of the class, so the class definitely gets entered. 来自班级开始时的推杆,因此肯定会进入班级。

# routes.rb
post '/incoming_mails' => 'incoming_mails#create'

The file itself looks like this: 文件本身如下所示:

# /app/controllers/incoming_mails_controller.rb
class IncomingMailsController < ApplicationController
  skip_before_filter :verify_authenticity_token

  def create
    puts "Entering the controller! Controlling the e-mail!"
    Rails.logger.info params[:headers][:subject]
    Rails.logger.info params[:plain]
    Rails.logger.info params[:html]

    if User.all.map(&:email).include? params[:envelope][:from] # check if user is registered
      @thought = Thought.new
      @thought.body = params[:plain].split("\n").first
      @thought.user = User.where(:email => params[:envelope][:from])
      @thought.date = DateTime.now

      if @thought.save
        render :text => 'Success', :status => 200
      else
        render :text => 'Internal failure', :status => 501
      end
    else
      render :text => 'Unknown user', :status => 404 # 404 would reject the mail
    end
  end
end

User and Thought are database resources used elsewhere without a problem. 用户和思想是在其他地方正常使用的数据库资源。 The saving procedure is the same that works in scaffolding-generated Thought controller. 保存过程与在脚手架生成的Thought控制器中工作的过程相同。 The params and Rails.logger logic I copied from a Cloudmailin Rails 3 example . 我从Cloudmailin Rails 3示例中复制了paramsRails.logger逻辑。

I'm really confused - where am I going wrong? 我真的很困惑-我要去哪里错了? I'd really appreciate any pointers. 我真的很感谢任何指示。

It turns out, this is why you shouldn't code while sleep-deprived. 事实证明,这就是为什么您不应该在睡眠不足时进行编码。 The problem was simple: there is no such thing as params[:envelope][:from] , there is only params[:from]) . 问题很简单:没有params[:envelope][:from]这样的东西,只有params[:from]) My assumption that :from would be a sub-element of :envelope was probably formed by looking at the pattern in the second "Cloudmailin in Rails on Heroku" example , where a code used to log subject is Rails.logger.log params[:envelope][:subject] . 我的假设:from可能是:envelope的子元素,可能是通过查看第二个“ Heroku中Rails中的Cloudmailin”示例中的模式形成的,其中用于记录主题的代码是Rails.logger.log params[:envelope][:subject]

I realized this was the error after reading the API documentation for 'original' Cloudmailin format . 阅读“原始” Cloudmailin格式的API文档后,我意识到这是错误。 It was exceptionally silly of me not to have found / looked for this resource in the first place. 最初没有找到/寻找这个资源对我来说是非常愚蠢的。

After fixing this, the code still didn't work, because User.where(:email => params[:from]) only returned a Relation object, while User object was expected. 修复此问题后,该代码仍然无法正常工作,因为User.where(:email => params[:from])仅返回了一个Relation对象,而预期User对象。 The error in Heroku logs was the following: Heroku日志中的错误如下:

 ActiveRecord::AssociationTypeMismatch (User(#29025160) expected,
 got ActiveRecord::Relation(#12334440)):

Since there can only be one user with some e-mail, the fix User.where(:email => params[:from]).first has no side-effects and results in correct behavior. 由于只能有一个用户接收某些电子邮件,因此修复程序User.where(:email => params[:from]).first没有副作用,并且可以导致正确的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM