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
|
2020-08-28 09:09:45 +00:00
|
|
|
task :send_email, [:paper_criteria_id, :domain, :locale] => :environment do |task,args|
|
2019-07-01 12:06:33 +00:00
|
|
|
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?
|
2020-08-28 12:49:47 +00:00
|
|
|
email_format = /^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+$/
|
2020-08-28 09:09:45 +00:00
|
|
|
subscribers = EPaperSubscriber.where(:language.in=>paper.language_to_send,:email.nin=>['',nil])
|
|
|
|
subscribers.update_all(:last_paper_sent => paper.id.to_s)
|
2020-08-28 09:12:28 +00:00
|
|
|
emails = subscribers.collect(&:email)
|
2020-08-28 09:09:45 +00:00
|
|
|
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
|
2020-08-28 12:49:47 +00:00
|
|
|
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|
|
2020-11-30 15:59:51 +00:00
|
|
|
email = Email.new(:mail_subject => paper.sending_title, :mail_lang => args[:locale], :template => "admin/e_paper_criterias/emailer", :template_data => {"paper_id" => paper.id, "page" => "http://" + domain + page, "siteurl" => "http://" + domain, "papers_data" => papers_data }, :mail_to => slice_emails.join(","))
|
2020-08-28 12:49:47 +00:00
|
|
|
begin
|
2020-11-30 13:27:49 +00:00
|
|
|
email.save
|
2020-08-28 12:49:47 +00:00
|
|
|
email.deliver
|
|
|
|
puts "Sending #{slice_emails.count} emails"
|
|
|
|
rescue => e
|
|
|
|
puts [slice_emails.inspect,e,e.backtrace]
|
|
|
|
tmp += slice_emails
|
|
|
|
end
|
2019-07-01 12:06:33 +00:00
|
|
|
end
|
2020-08-28 12:49:47 +00:00
|
|
|
paper.update_attributes(:send_failed_emails => tmp)
|
2019-07-01 12:06:33 +00:00
|
|
|
end
|
2020-08-28 12:49:47 +00:00
|
|
|
paper.invalid_emails = emails_invalid
|
2019-07-01 12:06:33 +00:00
|
|
|
paper.save
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|