Project

General

Profile

How to send an email with attachment on a plugin?

Added by Miguel Akira about 12 years ago

Hello.

I developed a plugin that generates a PDF report (I used prawn to generate the PDF). Now I need to email this PDF to the user's email. Is there a way to do it?

Thanks!


Replies (1)

RE: How to send an email with attachment on a plugin? - Added by Miguel Akira about 12 years ago

Well I found a way, I had to extend the core Mailer method to add my mailer method.

In order to do that, I edited the init.rb of my plugin:

##init.rb

# regular init.rb stuff....

require_dependency 'mailer'

module MailerPatch
  def self.included(base) # :nodoc:
    base.extend(ClassMethods)
    base.send(:include, InstanceMethods)

    # Same as typing in the class 
    base.class_eval do

    end

  end

  module ClassMethods

  end

  module InstanceMethods
    #the actual method
    def report(user, date)
      set_language_if_valid(user.language)
      recipients user.mail
      subject 'Report date #{date.month}/#{date.year}'
      body 'just testing'
      render_multipart('report', body)
    end
  end    
end

# Add module to Issue
Mailer.send(:include, MailerPatch)

Then in my plugin controller, I called the method:

    @test = Mailer.deliver_report(@user, @date)

As any mailer method, I have to put 'deliver_' before the method name. So the method name is 'report', but I call it as 'deliver_method' on the controller!

That's it, I don't know if there's an easier or better way (probably yes)!

    (1-1/1)