johncant / mailman-rails

Integrate Mailman with Rails
MIT License
16 stars 6 forks source link

Sample code? #5

Open ays0110 opened 10 years ago

ays0110 commented 10 years ago

Hi! Do you think you could post some sample code of an app that uses this mailman-rails gem? I'm a little confused as to what to put in what files. I installed this gem, along with daemon.

Then put the following into config/initializers/mailman.rb. The file consists of just the code below:

Mailman.config.imap = { server: 'imap.gmail.com', port: 993, # usually 995, 993 for gmail ssl: true, username: 'myemail@gmail.com', password: 'password' }

And then put the following into my email.rb model:

class Email < ActiveRecord::Base

Mailman::Rails.receive do to "myemail@gmail.com" do Email.create(:content=>JSON.parse(message.body)) end end

end

I run rails s, open a separate terminal and run rake mailman:start

I run rake mailman:status, and it says:

mailman_daemon: no instances running

Am I missing something here?

johncant commented 10 years ago

Hello,

I think you're using it correctly. I've only got one app using it so it might need a bit of work to running in various different contexts. Can you check your File.join(Rails.root, 'log') directory and your process list please?

ays0110 commented 10 years ago

Hi John,

Actually, I think I've gotten mailman to start polling and getting messages. However, I just can't seem to trigger the Email Model to create a new email in the DB whenever a message is received. Again, the ONLY code I have in my email.rb model is the following:

class Email < ActiveRecord::Base

Mailman::Rails.receive do to "myemail@gmail.com" do @email = Email.create(:content=>'message received') @email.save end end

end

As you can see, I'm just making Email make a dummy message 'message received', on receipt of a new email. I've tried just putting
@email = Email.create(:content=>'message received') @email.save

into a controller action, and running it, and it created messages just fine, so I know these two lines work. It seems like Mailman is just not calling this code when it gets a new message. Any thoughts on this?

ays0110 commented 10 years ago

Maybe it has something to do with your comment here:

The daemon would be started from the rake task but only after all the code is loaded. You need to enable cache_classes or something to take advantage of this.

I have no idea what you mean by this though, unfortunately.

ays0110 commented 10 years ago

Also, the mailman.log is logging things when emails come in, but the rails log is not noticing/logging anything when emails come in, so I suspect rails is not connected to mailman? Did I miss an extremely crucial link here?

ays0110 commented 10 years ago

Okay, more debugging later, I notice it's because of the to "myemail@gmail.com" line that is causing the problem. I altered the lines to read:

Mailman::Rails.receive do @email = Email.create(:content=>'this is a message to someone') @email.save end

And now it create a new email object every time I get an email. However, I can't use the message, for example:

Mailman::Rails.receive do @email = Email.create(:content=>JSON.parse(message.body)) @email.save end

This code above gives me an error because it says that message is undefined.

undefined local variable or method `message' for #Mailman::Application:0x007fc2ba755088

If I change it back to

Mailman::Rails.receive do to "myemail@gmail.com" do @email = Email.create(:content=>JSON.parse(message.body)) @email.save end end

I get no error, but of course, I get no emails either. I'm 100% certain that myemail@gmail.com is the right email. I've even changed it to myemail@gmail.com@imap.gmail.com, because I saw that on the mailman log, but no good.

johncant commented 10 years ago

Possible solution to main problem:

From memory, putting mailman hooks into your models will only work if the models have actually been loaded. By default, in development mode, your models are autoloaded, meaning that they are loaded on first use. IOW, I think your Mailman::Rails.receive line is not being reached. You need to find a way of conditionally eager loading your rails models from the rake task. You would then need to restart the rake task to see code changes.

How Mailman talks to Rails: Mailman is typically run as a daemon, listening for emails, in a separate process to your rails app, with no built-in communication.

I recommend that you have a look through the code - it really doesn't do very much apart from (cough) add convenience. Putting Mailman hooks in your models is a stylistic choice. I think most people put them in an initializer. I wanted to have them in my models because certain emails were specifically relevant to certain models.

johncant commented 10 years ago

Can you use pry to find the value of self within those blocks?