3

I am trying to create a message plugin for Redmine. I have couple of doubts regarding this

  1. How could I send the email in Redmine plugin? Is it possible to create the mailer inside the plugin? If so what is the command for creating mailer?

  2. I can able to see this(call_hook) method almost all the controller files. What is the use of this method?

Thanks in Advance

Karthi Ponnusamy
  • 2,031
  • 2
  • 25
  • 41

1 Answers1

6

There are two ways how to do it:

  1. Create new Mailer and inherit it from redmine mailer and just add new methods as you want
  2. Patch redmine mailer and add method for sending your mail

I used second one in my plugin RedmineCRM, you could download it and check in lib/redmine_contacts/patches/mailer_patch.rb

require 'dispatcher'   

module RedmineContacts
  module Patches    

    module MailerPatch
      module InstanceMethods
        def contacts_note_added(note, parent) 
          redmine_headers 'X-Project' => note.source.project.identifier, 
          'X-Notable-Id' => note.source.id,
          'X-Note-Id' => note.id
          message_id note
          if parent
            recipients (note.source.watcher_recipients + parent.watcher_recipients).uniq
          else
            recipients note.source.watcher_recipients
          end

          subject "[#{note.source.project.name}] - #{parent.name + ' - ' if parent}#{l(:label_note_for)} #{note.source.name}"  

          body :note => note,   
          :note_url => url_for(:controller => 'notes', :action => 'show', :note_id => note.id)
          render_multipart('note_added', body)
        end

        def contacts_issue_connected(issue, contact)
          redmine_headers 'X-Project' => contact.project.identifier, 
          'X-Issue-Id' => issue.id,
          'X-Contact-Id' => contact.id
          message_id contact
          recipients contact.watcher_recipients 
          subject "[#{contact.projects.first.name}] - #{l(:label_issue_for)} #{contact.name}"  

          body :contact => contact,
          :issue => issue,
          :contact_url => url_for(:controller => contact.class.name.pluralize.downcase, :action => 'show', :project_id => contact.project, :id => contact.id),
          :issue_url => url_for(:controller => "issues", :action => "show", :id => issue)
          render_multipart('issue_connected', body)
        end

      end  

      def self.included(receiver)
        receiver.send :include, InstanceMethods
        receiver.class_eval do 
          unloadable   
          self.instance_variable_get("@inheritable_attributes")[:view_paths] << RAILS_ROOT + "/vendor/plugins/redmine_contacts/app/views"
        end  
      end

    end

  end
end

Dispatcher.to_prepare do  

  unless Mailer.included_modules.include?(RedmineContacts::Patches::MailerPatch)
    Mailer.send(:include, RedmineContacts::Patches::MailerPatch)
  end   

end
Kirill Bezrukov
  • 592
  • 4
  • 6
  • I have tried to creating the mailer using below command but it is not working "ruby script/generate redmine_plugin_mailer communication notifier", I got below error "Couldn't find 'redmine_plugin_mailer' generator" – Karthi Ponnusamy Apr 30 '12 at 07:41
  • 3
    Just create new file in models folder yourplugin_mailer.rb. These is no plugin generator for mailer – Kirill Bezrukov May 03 '12 at 05:59