wingrunr21 / griddler-mandrill

Mandrill adapter for Griddler
MIT License
16 stars 49 forks source link

Unable to receive attachments #2

Closed matteodesimone closed 9 years ago

matteodesimone commented 10 years ago

Hi,

I'm trying to handle inbound email form Mandrill. My code extracts form the mandril email address the user id, it checks if the user is present in my db and, in this case, it forwards the email to the user. Everythings works except if I try to send attachments.

My EmailProcessor class is very simple:

class EmailProcessor 
  def self.process(email)

    receiver = User.where(slug:email.to[0][:token]).first  
    sender = User.where(email: email.from).first

    # if sender or receiver are missing
    # send back a warning email
    if sender.nil?
      UserMailer.delay.forward_from_incoming_email_wrong_sender(email, email.from)
    elsif receiver.nil? 
      UserMailer.delay.forward_from_incoming_email_wrong_receiver(email, email.from)
    else
      # Sender and receiver are Networkmamas' clients so send the email

      # THIS IS ROW 30 WHERE EXCEPTION RAISES
      UserMailer.delay.forward_from_incoming_email(email, sender, receiver)
    end
  end
end

The UserMailer::forward_from_incoming_email() is:

  def forward_from_incoming_email(email, sender, receiver) 
    email.attachments.each do |attachment|
      attachments[attachment.original_filename] = File.read(attachment.tempfile)
    end

    @sender = sender
    @receiver = receiver
    @email = email

    mail(:from =>  sender.internal_email, to: receiver.email, :subject => email.subject)
  end

I've tested several situations and I noticed that:

...attachments array seems to be empty because the .each loop is not executed. Indeed this is the content of 'email' object in this case:

params:
  :to:
  - XXXXXXXXXXXXXXXXXXXXX
  :cc: []
  :from: XXXXXXXXXXXXXXXXXXXX
  :subject: test allegati
  :text: |+
    allego un allegato

  :html: |
    XXXXXXXXXXXXXXXX
    CUT
    XXXXXXXXXXXXXXXX
  :attachments: &1 []
config: !ruby/object:Griddler::Configuration
  processor_class: !ruby/class 'EmailProcessor'
  to: :hash
  reply_delimiter: "-- PER RISPONDERE SCRIVI SOPRA A QUESTA RIGA --"
  email_service_adapter: !ruby/class 'Griddler::Mandrill::Adapter'
  from: :email
  processor_method: :process
to:
- :token: gino-ginetto
  :host: testemail.XXXXXXXXXX
  :email: XXXXXXXXXXXXXX
  :full: XXXXXXXXXXXXXXXX
  :name:
from: XXXXXXXX
subject: test allegati
body: allego un allegato
raw_text: |+
  allego un allegato

raw_html: |
  <html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;">allego un allegato<br><br><img name="logo_gaminaction_white.jpg" apple-inline="yes" id="C66786B0-EDA6-4F49-BF5A-3A4DCF257DD3" height="400" width="400" apple-width="yes" apple-height="yes" src="cid:2D631A06-FF95-43EF-AAC0-14326760B75D@lan"></body></html>
raw_body: |+
  allego un allegato

headers: {}
cc: []
raw_headers:
attachments: *1

... it raises an exception:

Completed 500 Internal Server Error in 9ms

NoMethodError (undefined method `name' for nil:NilClass):
  app/models/email_processor.rb:30:in `process'

  Rendered /.rvm/gems/ruby-2.1.1/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.7ms)
  Rendered /.rvm/gems/ruby-2.1.1/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.3ms)
  Rendered /.rvm/gems/ruby-2.1.1/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (4.7ms)
  Rendered /.rvm/gems/ruby-2.1.1/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (19.6ms)

and forward_from_incoming_email() is not executed.

Any suggestions?

wingrunr21 commented 10 years ago

Can you post the full stack trace for the exception that is raised? I'll have to test multiple attachments manually.

matteodesimone commented 10 years ago

I've moved to a different gem so I'm not able to replicate the problem now. Anyway I can share with you the complete log related to the problem: https://www.dropbox.com/s/znjyfpfwg7nft1u/log-mandrill.txt.zip