Merge pull request #5111 from jaywink/user-mailer

Rake task to send mails to users
This commit is contained in:
Jonne Haß 2014-08-18 11:17:53 +02:00
commit d3b901801c
3 changed files with 41 additions and 6 deletions

View file

@ -31,6 +31,7 @@
* Add help section about keyboard shortcuts [#5100](https://github.com/diaspora/diaspora/pull/5100) * Add help section about keyboard shortcuts [#5100](https://github.com/diaspora/diaspora/pull/5100)
* Automatically add poll answers as needed [#5109](https://github.com/diaspora/diaspora/pull/5109) * Automatically add poll answers as needed [#5109](https://github.com/diaspora/diaspora/pull/5109)
* Add Terms of Service as an option for podmins, includes base template [#5104](https://github.com/diaspora/diaspora/pull/5104) * Add Terms of Service as an option for podmins, includes base template [#5104](https://github.com/diaspora/diaspora/pull/5104)
* Add rake task to send a mail to all users [#5111](https://github.com/diaspora/diaspora/pull/5111)
# 0.4.0.1 # 0.4.0.1

View file

@ -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

32
lib/tasks/podmin.rake Normal file
View file

@ -0,0 +1,32 @@
namespace :podmin do
desc "Send an email to users as admin"
task :admin_mail, [:users_def, :msg_path, :subject] => :environment do |t, args|
if args[:users_def] == 'all'
# to all except deleted and deactivated, of course
users = User.where("locked_at is null and username is not null")
elsif args[:users_def] == 'active_yearly'
users = User.yearly_actives
elsif args[:users_def] == 'active_monthly'
users = User.monthly_actives
elsif args[:users_def] == 'active_halfyear'
users = User.halfyear_actives
end
msg = File.read(args[:msg_path])
mails = Notifier.admin(msg.html_safe, users, :subject => args[:subject])
count = 0
mails.each do |mail|
begin
mail.deliver
count += 1
if count % 100 == 0
puts "#{count} out of #{mails.count} delivered"
end
rescue
puts $!, $@
end
end
puts "#{count} out of #{mails.count} delivered"
end
end