title: Cloudmailin gets 500 from Heroku when delivering e-mails
body: <p>I'm using the Cloudmailin addon to receive e-mail from my Heroku app. However, Cloudmailin has not been able to deliver - or, rather, it gets 500 from Heroku every time (so the address is correct).</p>
  
  <p>The error in Heroku logs is</p>
  
  <pre><code>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'
  </code></pre>
  
  <p>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.</p>
  
  <pre><code># routes.rb
  post '/incoming_mails' =&gt; 'incoming_mails#create'
  </code></pre>
  
  <p>The file itself looks like this:</p>
  
  <pre><code># /app/controllers/incoming_mails_controller.rb
  class IncomingMailsController &lt; 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(&amp;: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 =&gt; params[:envelope][:from])
        @thought.date = DateTime.now
  
        if @thought.save
          render :text =&gt; 'Success', :status =&gt; 200
        else
          render :text =&gt; 'Internal failure', :status =&gt; 501
        end
      else
        render :text =&gt; 'Unknown user', :status =&gt; 404 # 404 would reject the mail
      end
    end
  end
  </code></pre>
  
  <p>User and Thought are database resources used elsewhere without a problem. The saving procedure is the same that works in scaffolding-generated Thought controller. The <code>params</code> and <code>Rails.logger</code> logic I copied from a <a href="https://devcenter.heroku.com/articles/cloudmailin#receiving-email-in-ruby" rel="nofollow">Cloudmailin Rails 3 example</a>.</p>
  
  <p>I'm really confused - where am I going wrong? I'd really appreciate any pointers.</p>