Send a private message to Redmine Plugin

I am writing a Redmine plugin. I already have a model, view and controller in place. Whenever someone creates, updates, or deletes my model, I want to send an email to people from a particular group. (Like emails sent by Redmine when someone creates or updates a problem) Can someone please tell me what would be the best way? Thank you

+6
source share
1 answer

I know that 2 years have passed since you asked, but I had the same problem and I understood how to send an email with my plugin.

What you should do for a plugin named my_plugin:

1. Create a model that inherits from Mailer.

So, if I need a mailer named MyPluginMailer:

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

Like:

class MyPluginMailer < Mailer end 

2. Create a method to invoke the mailer.

Let's say I'm writing a news plugin for redmine. I want to send an email summarizing the article I submitted so that users cannot poll the plugin every time they want to find out 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 the after_create callback, for example.

3. Create several views for this method.

I have to create 2 different files:

  • my_method.html.erb
  • my_method.text.erb

otherwise redmine will fail with the exception "pattern not found".

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

  • on_new_article.html.erb
  • on_new_article.text.erb

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

 <h1><%= @article.title %></h1> <p><%= @article.summary %></p> 
+10
source

Source: https://habr.com/ru/post/915229/


All Articles