1

这是我的代码:

require 'mms2r'

class IncomingMailHandler < ActionMailer::Base

  ##
  # Receives email(s) from MMS-Email or regular email and 
  # uploads that content the user's photos.
  # TODO: Use beanstalkd for background queueing and processing.
  def receive(email)    
    begin
      mms = MMS2R::Media.new(email)

      ## 
      # Ok to find user by email as long as activate upon registration.
      # Remember to make UI option that users can opt out of registration 
      # and either not send emails or send them to a username+32523@example.com
      # type address.
      ##
      # Remember to get SpamAssasin
      if (@user = User.find_by_email(email.from) && email.has_attachments?)
        mms.media.each do |key, value|
          if key.include?('image')
            value.each do |file| 
              @user.photos.push Photo.create!(:uploaded_data => File.open(file), :title => email.subject.empty? ? "Untitled" : email.subject)
            end
          end
        end
      end

    ensure

      mms.purge

    end

  end

end

这是我的错误:

/usr/local/lib/ruby/gems/1.8/gems/rails-2.3.4/lib/commands/runner.rb:48: undefined method `photos' for true:TrueClass (NoMethodError)
  from /usr/home/xxx/app/models/incoming_mail_handler.rb:23:in `each'
  from /usr/home/xxx/app/models/incoming_mail_handler.rb:23:in `receive'
  from /usr/home/xxx/app/models/incoming_mail_handler.rb:21:in `each'
  from /usr/home/xxx/app/models/incoming_mail_handler.rb:21:in `receive'
  from /usr/local/lib/ruby/gems/1.8/gems/actionmailer-2.3.4/lib/action_mailer/base.rb:419:in `receive'
  from (eval):1
  from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `eval'
  from /usr/local/lib/ruby/gems/1.8/gems/rails-2.3.4/lib/commands/runner.rb:48
  from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
  from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
  from /home/xxx/script/runner:3

我向服务器发送了一封带有两个图像附件的电子邮件。收到电子邮件后,服务器运行“| ruby​​ /xxx/script/runner 'IncomingMailHandler.receive STDIN.read'”

到底是怎么回事?我究竟做错了什么?

MMS2R 文档

4

1 回答 1

1

请更换

 if (@user = User.find_by_email(email.from) && email.has_attachments?)

 if ((@user = User.find_by_email(email.from)) && email.has_attachments?)
于 2010-05-04T06:31:40.627 回答