Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send email from Redmine Plugin

I am writing a Redmine plugin. I already have the model, view and controller in place. Whenever someone creates, updates or deletes my model I want to send an email to people in a certain group. (Similar to emails sent out by Redmine when someone creates or updates an Issue) Could someone please let me know what would be the best way to go about it? Thanks!

like image 500
AAK Avatar asked May 08 '12 20:05

AAK


1 Answers

I know it's been 2 years since you asked but I had the same issue and I figured out how to send an email with my plugin.

What you have to do for a plugin named my_plugin is :

1. Create a Model which inherits from Mailer.

So if I want a mailer named MyPluginMailer :

  • I create redmine_folder/plugins/my_plugin/app/models/my_plugin_mailer.rb
  • I create the MyPluginMailer class which inherits from the redmine Mailer

Like that:

class MyPluginMailer < Mailer
end

2. Create a method to call on the mailer.

Say I am writing a news plugin for redmine. I want to send an email which summarizes the article I submitted so that users do not have to poll the plugin each time they want to know if there is something new.

I create a method in my mailer class :

class MyPluginMailer < Mailer
  def on_new_article(user_to_warn, article)
    mail to: user_to_warn.email, subject: "New article: #{article.title}"
    @article = article #So that @article will be available in the views.
  end
end

You can call this method in your Article class in an after_create callback for example.

3. Create some views for the method.

I have to create 2 differents files :

  1. my_method.html.erb
  2. my_method.text.erb

or else redmine is going to crash with a "template not found" exception.

So in my redmine_folder/plugins/my_plugin/app/views/my_plugin_mailer/ I create

  1. on_new_article.html.erb
  2. on_new_article.text.erb

In my on_new_article.html.erb I write something like :

<h1><%= @article.title %></h1>
<p><%= @article.summary %></p>
like image 138
Michel Antoine Avatar answered Oct 06 '22 22:10

Michel Antoine