epaper/lib/tasks/e_paper_tasks.rake

45 lines
1.9 KiB
Ruby
Raw Normal View History

2019-05-28 15:54:32 +00:00
# desc "Explaining what the task does"
namespace :epaper do
task :send_email, [:paper_criteria_id, :domain] => :environment do |task,args|
paper = PaperCriteria.find(args[:paper_criteria_id]) rescue nil
page = Page.where(:module => "e_paper").first.url rescue ""
domain = args[:domain]
if !paper.nil?
subscribers = EPaperSubscriber.all
emails = []
mail_limit = 25
subscribers.each do |subscriber|
emails << subscriber.email if paper.language_to_send.include? subscriber.language
subscriber.last_paper_sent = paper.id.to_s
subscriber.save
if emails.count == mail_limit
puts "Sending #{mail_limit} mails"
2019-09-01 20:31:45 +00:00
email = Email.new(:mail_subject => paper.sending_title, :mail_lang => :zh_tw, :template => "admin/e_paper_criterias/emailer", :template_data => {"paper" => paper, "page" => "http://" + domain + page, "siteurl" => "http://" + domain }, :mail_to => emails)
email.deliver
emails = []
sleep 0.5
end
end
other_emails = paper.other_emails_ids
if !other_emails.blank?
other_emails.each do |email|
emails << email
if emails.count == mail_limit
puts "Sending #{mail_limit} mails"
2019-09-01 20:31:45 +00:00
email = Email.new(:mail_subject => paper.sending_title, :mail_lang => :zh_tw, :template => "admin/e_paper_criterias/emailer", :template_data => {"paper" => paper, "page" => "http://" + domain + page, "siteurl" => "http://" + domain }, :mail_to => emails)
email.deliver
emails = []
sleep 0.5
end
end
end
if !emails.empty?
puts "Sending last emails"
2019-09-01 20:31:45 +00:00
email = Email.new(:mail_subject => paper.sending_title, :mail_lang => :zh_tw, :template => "admin/e_paper_criterias/emailer", :template_data => {"paper" => paper, "page" => "http://" + domain + page, "siteurl" => "http://" + domain }, :mail_to => emails)
email.deliver
end
paper.save
end
end
end