# 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 "" papers_data = Admin::EpaperHelper.get_paper_data(paper) 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.email.blank? subscriber.last_paper_sent = paper.id.to_s subscriber.save if emails.count == mail_limit puts "Sending #{mail_limit} mails" 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) 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" 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) email.deliver emails = [] sleep 0.5 end end end if !emails.empty? puts "Sending last emails" 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) email.deliver end paper.save end end end