0
votes

I am newbie in Rails, therefore sorry for silly question. For sending emails I use ActionMailer with Rails 2.3.5. The syntax is following

deliver_maintest1

deliver_maintest2

in model of instance of ActionMailer I have

def maintest1

end

def maintest2

end

Inside definitions I set recipient, subject, headers,...As I understand there is no any explicity defined method mail which is actually sent email. Emails are sent from def maintest1 and maintest2. The problem is before sending email I need to define few counters how many emails were sent thought maintest1 and maintest2. Now take into account I have tens defs like maintest. So I need a common place for all those defs. In your opinion what's the best solution?

Thanks!

2

2 Answers

2
votes

On rails 3 and above you could use an observer. These get called after every mail delivery, passing through the message object. You just need to implement a delivered_email class method and register it.

class EmailObserver
  def self.delivered_email(message)
    # do something with message
  end
end

Then, hook it into mail with

Mail.register_observer(EmailObserver)

This doesn't work on rails 2.x, which doesn't use the mail gem (it uses tmail from the ruby standard library.)

On 2.3.x I would try something like

class MyMailer < ActionMailer::Base
  def deliver!(mail=@mail)
    super
    # do your logging here
  end
end 
1
votes

You would be calling "Mailer.deliver_maintest" to send the mails out to anyone, to count the nos of times you sent a particular email you just need to keep track of it each time you call "Mailer.deliver_maintest" .

You can store that counter either the database or somewhere. something like this.

  // Some lines of code to Update the counter for the mailer
  Mailer.deliver_maintest

You can also use a third party email tool like PostMark to send your email ( with them you can associate each email with tags, and I just generally use those tags to keep track of emails sent out ).