From 6dd1e313374983d0936d8964a6ac9c7a5c2139d9 Mon Sep 17 00:00:00 2001 From: Jason Robinson Date: Sun, 10 Aug 2014 00:11:17 +0300 Subject: [PATCH 1/3] Rake task for Notifier.admin to send emails to users --- lib/tasks/podmin.rake | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 lib/tasks/podmin.rake diff --git a/lib/tasks/podmin.rake b/lib/tasks/podmin.rake new file mode 100644 index 000000000..f48f042a3 --- /dev/null +++ b/lib/tasks/podmin.rake @@ -0,0 +1,29 @@ +namespace :podmin do + + desc < :environment do |t, args| + if args[:users_def] == 'all' + # to all except deleted and deactivated, of course + users = User.where(locked_at: nil) + 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 + file = File.open(args[:msg_path]) + msg = file.read + file.close + mails = Notifier.admin(msg.html_safe, users) + mails.each(&:deliver) + end + +end \ No newline at end of file From a4eafe8a8797397e7404ba4207449d3e13525c9e Mon Sep 17 00:00:00 2001 From: Jason Robinson Date: Sun, 10 Aug 2014 22:38:44 +0300 Subject: [PATCH 2/3] Allow sending emails with custom subject via Notifier.admin --- app/mailers/notifier.rb | 14 ++++++++------ lib/tasks/podmin.rake | 29 ++++++++++++++--------------- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/app/mailers/notifier.rb b/app/mailers/notifier.rb index 5abce9ee0..00dd5e109 100644 --- a/app/mailers/notifier.rb +++ b/app/mailers/notifier.rb @@ -3,16 +3,16 @@ class Notifier < ActionMailer::Base helper :notifier helper :people - def self.admin(string, recipients, opts = {}) + def self.admin(string, recipients, opts = {}, subject=nil) mails = [] recipients.each do |rec| - mail = single_admin(string, rec, opts.dup) + mail = single_admin(string, rec, opts.dup, subject) mails << mail end mails end - def single_admin(string, recipient, opts={}) + def single_admin(string, recipient, opts={}, subject=nil) @receiver = recipient @string = string.html_safe @@ -22,13 +22,15 @@ class Notifier < ActionMailer::Base } end + unless subject + subject = I18n.t('notifier.single_admin.subject') + end + default_opts = {:to => @receiver.email, :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) - - mail(default_opts) do |format| format.text format.html diff --git a/lib/tasks/podmin.rake b/lib/tasks/podmin.rake index f48f042a3..547db8722 100644 --- a/lib/tasks/podmin.rake +++ b/lib/tasks/podmin.rake @@ -1,17 +1,10 @@ namespace :podmin do - desc < :environment do |t, args| + 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: nil) + 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' @@ -19,11 +12,17 @@ DESC elsif args[:users_def] == 'active_halfyear' users = User.halfyear_actives end - file = File.open(args[:msg_path]) - msg = file.read - file.close - mails = Notifier.admin(msg.html_safe, users) - mails.each(&:deliver) + msg = File.read(args[:msg_path]) + mails = Notifier.admin(msg.html_safe, users, :subject => args[:subject]) + count = 0 + mails.each do |mail| + 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 \ No newline at end of file From d0b37fa7ee0b8f39f75465d5f477d5c5b93d62a9 Mon Sep 17 00:00:00 2001 From: Jason Robinson Date: Sun, 17 Aug 2014 18:14:36 +0300 Subject: [PATCH 3/3] Rescue from an exception when sending emails in podmin:admin_email --- lib/tasks/podmin.rake | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/tasks/podmin.rake b/lib/tasks/podmin.rake index 547db8722..dc5289cbd 100644 --- a/lib/tasks/podmin.rake +++ b/lib/tasks/podmin.rake @@ -16,10 +16,14 @@ namespace :podmin do mails = Notifier.admin(msg.html_safe, users, :subject => args[:subject]) count = 0 mails.each do |mail| - mail.deliver - count += 1 - if count % 100 == 0 - puts "#{count} out of #{mails.count} delivered" + 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"