Allow sending emails with custom subject via Notifier.admin
This commit is contained in:
parent
6dd1e31337
commit
a4eafe8a87
2 changed files with 22 additions and 21 deletions
|
|
@ -3,16 +3,16 @@ class Notifier < ActionMailer::Base
|
||||||
helper :notifier
|
helper :notifier
|
||||||
helper :people
|
helper :people
|
||||||
|
|
||||||
def self.admin(string, recipients, opts = {})
|
def self.admin(string, recipients, opts = {}, subject=nil)
|
||||||
mails = []
|
mails = []
|
||||||
recipients.each do |rec|
|
recipients.each do |rec|
|
||||||
mail = single_admin(string, rec, opts.dup)
|
mail = single_admin(string, rec, opts.dup, subject)
|
||||||
mails << mail
|
mails << mail
|
||||||
end
|
end
|
||||||
mails
|
mails
|
||||||
end
|
end
|
||||||
|
|
||||||
def single_admin(string, recipient, opts={})
|
def single_admin(string, recipient, opts={}, subject=nil)
|
||||||
@receiver = recipient
|
@receiver = recipient
|
||||||
@string = string.html_safe
|
@string = string.html_safe
|
||||||
|
|
||||||
|
|
@ -22,13 +22,15 @@ class Notifier < ActionMailer::Base
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
unless subject
|
||||||
|
subject = I18n.t('notifier.single_admin.subject')
|
||||||
|
end
|
||||||
|
|
||||||
default_opts = {:to => @receiver.email,
|
default_opts = {:to => @receiver.email,
|
||||||
:from => AppConfig.mail.sender_address,
|
:from => AppConfig.mail.sender_address,
|
||||||
:subject => I18n.t('notifier.single_admin.subject'), :host => AppConfig.pod_uri.host}
|
:subject => subject, :host => AppConfig.pod_uri.host}
|
||||||
default_opts.merge!(opts)
|
default_opts.merge!(opts)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
mail(default_opts) do |format|
|
mail(default_opts) do |format|
|
||||||
format.text
|
format.text
|
||||||
format.html
|
format.html
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,10 @@
|
||||||
namespace :podmin do
|
namespace :podmin do
|
||||||
|
|
||||||
desc <<DESC
|
desc "Send an email to users as admin"
|
||||||
Send an email to users as admin.
|
task :admin_mail, [:users_def, :msg_path, :subject] => :environment do |t, args|
|
||||||
Parameters
|
|
||||||
users def
|
|
||||||
"all", "active_yearly", "active_monthly" or "active_halfyear"
|
|
||||||
msg_path
|
|
||||||
path to a file that contains the HTML message to send
|
|
||||||
DESC
|
|
||||||
task :admin_mail, [:users_def, :msg_path] => :environment do |t, args|
|
|
||||||
if args[:users_def] == 'all'
|
if args[:users_def] == 'all'
|
||||||
# to all except deleted and deactivated, of course
|
# to all except deleted and deactivated, of course
|
||||||
users = User.where(locked_at: nil)
|
users = User.where("locked_at is null and username is not null")
|
||||||
elsif args[:users_def] == 'active_yearly'
|
elsif args[:users_def] == 'active_yearly'
|
||||||
users = User.yearly_actives
|
users = User.yearly_actives
|
||||||
elsif args[:users_def] == 'active_monthly'
|
elsif args[:users_def] == 'active_monthly'
|
||||||
|
|
@ -19,11 +12,17 @@ DESC
|
||||||
elsif args[:users_def] == 'active_halfyear'
|
elsif args[:users_def] == 'active_halfyear'
|
||||||
users = User.halfyear_actives
|
users = User.halfyear_actives
|
||||||
end
|
end
|
||||||
file = File.open(args[:msg_path])
|
msg = File.read(args[:msg_path])
|
||||||
msg = file.read
|
mails = Notifier.admin(msg.html_safe, users, :subject => args[:subject])
|
||||||
file.close
|
count = 0
|
||||||
mails = Notifier.admin(msg.html_safe, users)
|
mails.each do |mail|
|
||||||
mails.each(&:deliver)
|
mail.deliver
|
||||||
|
count += 1
|
||||||
|
if count % 100 == 0
|
||||||
|
puts "#{count} out of #{mails.count} delivered"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
puts "#{count} out of #{mails.count} delivered"
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
Loading…
Reference in a new issue