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!
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 :
- my_method.html.erb
- 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
- 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>
© 2022 - 2024 — McMap. All rights reserved.