diff --git a/app/controllers/admin/e_paper_criterias_controller.rb b/app/controllers/admin/e_paper_criterias_controller.rb index f415fc7..d25d0f9 100644 --- a/app/controllers/admin/e_paper_criterias_controller.rb +++ b/app/controllers/admin/e_paper_criterias_controller.rb @@ -56,9 +56,10 @@ def initialize def send_newsletter Orbit::Application.load_tasks + locale = I18n.locale Thread.new do begin - rake = Rake::Task['epaper:send_email'].execute(OpenStruct.new({:paper_criteria_id => params[:id], :domain => request.host_with_port})) + rake = Rake::Task['epaper:send_email'].execute(OpenStruct.new({:paper_criteria_id => params[:id], :domain => request.host_with_port, :locale => locale})) rescue => e puts [e,e.backtrace] end diff --git a/lib/tasks/e_paper_tasks.rake b/lib/tasks/e_paper_tasks.rake index d408351..0328fb5 100644 --- a/lib/tasks/e_paper_tasks.rake +++ b/lib/tasks/e_paper_tasks.rake @@ -1,44 +1,27 @@ # desc "Explaining what the task does" namespace :epaper do - task :send_email, [:paper_criteria_id, :domain] => :environment do |task,args| + 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.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 + 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 - 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