# desc "Explaining what the task does" namespace :epaper do task :send_email, [:paper_criteria_id, :domain, :locale] => :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.where(:language.in=>paper.language_to_send,:email.nin=>['',nil]) subscribers.update_all(:last_paper_sent => paper.id.to_s) emails = subscribers.collect(&:email) site = Site.first email_limit_reciever = site[:site_settings][:email_limit_reciever].to_i rescue 0 mail_limit = email_limit_reciever==0 ? 25 : email_limit_reciever emails += paper.other_emails_ids emails.each_slice(mail_limit).each do |slice_emails| email = Email.new(:mail_subject => paper.sending_title, :mail_lang => args[:locale], :template => "admin/e_paper_criterias/emailer", :template_data => {"paper" => paper, "page" => "http://" + domain + page, "siteurl" => "http://" + domain, "papers_data" => papers_data }, :mail_to => slice_emails) begin email.deliver puts "Sending #{slice_emails.count} emails" rescue => e puts [slice_emails,e,e.backtrace] end end paper.save end end end