43 lines
1.7 KiB
Ruby
43 lines
1.7 KiB
Ruby
# 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?
|
|
email_format = /^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+$/
|
|
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_valid = emails.select do |email|
|
|
tmp = (email_format=~email)
|
|
tmp==0 ? true : false
|
|
end
|
|
emails_invalid = emails - emails_valid
|
|
paper.send_failed_emails = []
|
|
Thread.new do
|
|
tmp = []
|
|
emails_valid.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.save
|
|
email.deliver
|
|
puts "Sending #{slice_emails.count} emails"
|
|
rescue => e
|
|
puts [slice_emails.inspect,e,e.backtrace]
|
|
tmp += slice_emails
|
|
end
|
|
end
|
|
paper.update_attributes(:send_failed_emails => tmp)
|
|
end
|
|
paper.invalid_emails = emails_invalid
|
|
paper.save
|
|
end
|
|
end
|
|
end
|