2019-05-28 15:54:32 +00:00
|
|
|
# desc "Explaining what the task does"
|
2019-07-01 12:06:33 +00:00
|
|
|
namespace :epaper do
|
|
|
|
task :send_email, [:paper_criteria_id, :domain] => :environment do |task,args|
|
|
|
|
paper = PaperCriteria.find(args[:paper_criteria_id]) rescue nil
|
2020-08-26 09:57:30 +00:00
|
|
|
page = Page.where(:module => "e_paper").first.url rescue ""
|
|
|
|
papers_data = Admin::EpaperHelper.get_paper_data(paper)
|
2019-07-01 12:06:33 +00:00
|
|
|
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"
|
2020-08-26 09:57:30 +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, "papers_data" => papers_data }, :mail_to => emails)
|
2019-07-01 12:06:33 +00:00
|
|
|
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)
|
2019-07-01 12:06:33 +00:00
|
|
|
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)
|
2019-07-01 12:06:33 +00:00
|
|
|
email.deliver
|
|
|
|
end
|
|
|
|
paper.save
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|