Actionmailer woes

hi Guys

I have now spent hours on this and turn to you in desperation.

I am using Actionmailer to fetch emails, but I need to pass a userid to
the receive method, so that the receive method knows what user to pass
the email to. I tried:
(ATTEMPT 1)
class PokeMailer < ActionMailer::Base
  def receive(email,userid)
    ...
  end

I then invoke the receipt of email with

task = PokeMailer.receive(m.pop,userid)

But I get the error 'wrong number of arguments (2 for 1)'

So I though - ok - the actionmailer receive method must be expecting one
argument. Lets create another method to accept the userid and call the
receive method with only the raw email. So I wrote as second PokeMailer
method:
(ATTEMPT 2)
class PokeMailer < ActionMailer::Base
  def getemail(email,userid)
    @userid=userid
    PokeMailer.receive(email)
  end
  def receive(email)
    ..
  end
  ..
end

and call it with

task = PokeMailer.getemail(m.pop,userid)

Now I get the error 'undefined method `getemail' for PokeMailer:Class'

(kindof figured this out - changed def getemail to def self.getemail

My third attempt was to try and instantiate an instance of PokeMailer
with a initialize method:
(ATTEMPT 3)
class PokeMailer < ActionMailer::Base
  def intitalize (userid)
    @userid = userid
    super #not sure if this is needed?
  end
  def receive(email)
    ...
  end
  ...
end

and then call this with something like:

pm = PokeMailer.new(userid)
task = pm.receive(m.pop)

This fails because it seems that pm == nil - it doesn't event get
created?!!!

I am at whits' end. Please save me!

Thanks for anything - even sympathy would help :wink:

Pieter Hugo

hi Guys

I have now spent hours on this and turn to you in desperation.

I am using Actionmailer to fetch emails, but I need to pass a userid to
the receive method, so that the receive method knows what user to pass
the email to. I tried:
(ATTEMPT 1)
class PokeMailer < ActionMailer::Base
def receive(email,userid)

If you are trying to define a class method (rather than an instance
method) you need
def self.receive(..)
I think this may be the root of all the problems below also.

Colin

Hi Colin

Thanks for the feedback. I kinda grokked that eventually. But I can't
just turn my receive method into a class level method - because then the
raw email string never gets turned into a tmail object. I any case the
original code - which works, but doesnt pass my userid, is

class PokeMailer < ActionMailer::Base
  def receive(email)
    ...
  end

I then invoke the receipt of email with

task = PokeMailer.receive(m.pop)

Question - is this now a class method or an instance method? It gets
called on the PokeMailer class, and responds, but its not defined with
def self.receive.

I suspect my ruby skills are woefully inadequate.... :frowning:

Any other suggestions?

Pieter

Hi Colin

Thanks for the feedback. I kinda grokked that eventually. But I can't
just turn my receive method into a class level method - because then the
raw email string never gets turned into a tmail object. I any case the
original code - which works, but doesnt pass my userid, is

class PokeMailer < ActionMailer::Base
def receive(email)

That is an instance method. To call it you would need a PokeMailer
object and then call it via
my_poke_mailer.receive(..)

...
end

I then invoke the receipt of email with

task = PokeMailer.receive(m.pop)

You are asking here for a class method, so it will look for something
defined as
def self.receive()
Since you have not overridden that then presumably it will call an
existing method (I don't know PokeMailer so I can't comment on the
details). It will not call your method. Take your method out and see
if it still finds a method.

Question - is this now a class method or an instance method? It gets
called on the PokeMailer class, and responds, but its not defined with
def self.receive.

not sure what you are asking here. If you want to find out which
method it is calling have a look at the Rails Guide on debugging, it
will show you how to use ruby-debug to break into your code, you can
then step into the function and see where it goes.

I suspect my ruby skills are woefully inadequate.... :frowning:

So are mine, just possibly slightly less so. The problem is that the
problem one is tackling often seems to require just a little more
knowledge than one's current state. :slight_smile:

Colin

Hi Colin

Thanks for bearing with me. Your thinking is spot on in agreement with
how I understand ruby. And yet

class PokeMailer < ActionMailer::Base
def receive(email)
...
end

I then invoke the receipt of email with

task = PokeMailer.receive(m.pop)

works 100%

I tried creating an instance of PokeMailer (see first post)

pm = PokeMailer.new(userid)

But it just returns nil, and pm.receive says nil doesn't have a receive
method.

I think its something to do with Actionmailer - but it definitely doesnt
behave like a normal class.

:frowning:

Hi Colin

Thanks for bearing with me. Your thinking is spot on in agreement with
how I understand ruby. And yet

class PokeMailer < ActionMailer::Base
def receive(email)
...
end

I then invoke the receipt of email with

task = PokeMailer.receive(m.pop)

works 100%

What happens if you comment out your receive method and then run the code?

Colin

Hi Colin

Thanks for bearing with me. Your thinking is spot on in agreement with
how I understand ruby. And yet

class PokeMailer < ActionMailer::Base
def receive(email)
...
end

I then invoke the receipt of email with

task = PokeMailer.receive(m.pop)

works 100%

I tried creating an instance of PokeMailer (see first post)

pm = PokeMailer.new(userid)

But it just returns nil, and pm.receive says nil doesn't have a receive
method.

I think its something to do with Actionmailer - but it definitely doesnt
behave like a normal class.

There's a method_missing handler which handles calls to class methods that aren't there by creating new instances and dispatching the method to the newly created instance

Fred

Thanks for all the help guys. I got a solution in the end. I created a
class level variable called @@userid which I then set in my
getemail(email,userid) method before calling receive(email). Nasty, and
not what I'd have expected, but it works.

Regards

Pieter