Send email from Redmine Plugin
Asked Answered
A

1

7

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!

Ama answered 8/5, 2012 at 20:20 Comment(2)
No answer for this post? I am also wanting to implement the same. If anybody knows the answer, please update. Thanks in advance.Earwig
Did this ever get solved?Confront
S
14

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>
Shrewsbury answered 23/10, 2014 at 17:2 Comment(1)
In on_new_article I had to define the instance variable before calling mail. Thanks for this answer.Kalimantan

© 2022 - 2024 — McMap. All rights reserved.