From f64b643c996187f9301ee2ae06fecbb5f0aa66d7 Mon Sep 17 00:00:00 2001 From: chiu Date: Fri, 28 Aug 2020 20:49:47 +0800 Subject: [PATCH] fix error --- .../admin/e_paper_criterias_controller.rb | 26 +++++---- app/models/paper_criteria.rb | 4 +- .../admin/e_paper_criterias/_index.html.erb | 56 ++++++++++++++++++- config/locales/en.yml | 4 +- config/locales/zh_tw.yml | 4 +- config/routes.rb | 2 + lib/tasks/e_paper_tasks.rake | 29 +++++++--- 7 files changed, 100 insertions(+), 25 deletions(-) diff --git a/app/controllers/admin/e_paper_criterias_controller.rb b/app/controllers/admin/e_paper_criterias_controller.rb index d25d0f9..de4ac13 100644 --- a/app/controllers/admin/e_paper_criterias_controller.rb +++ b/app/controllers/admin/e_paper_criterias_controller.rb @@ -56,18 +56,24 @@ 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, :locale => locale})) - rescue => e - puts [e,e.backtrace] - end - end + rake = Rake::Task['epaper:send_email'].execute(OpenStruct.new({:paper_criteria_id => params[:id], :domain => request.host_with_port, :locale => I18n.locale})) # rake = Rake::Task['epaper:send_email'].invoke([params[:id], request.host_with_port]) - render :json => {"success" => true}.to_json + paper = PaperCriteria.find(params[:id]) rescue nil + if !paper.nil? && paper.invalid_emails.blank? + render :json => {"success" => true}.to_json + else + invalid_email_list = paper.invalid_emails rescue 'can not find paper' + render :json => {"success" => false,"invalid_emails"=>invalid_email_list}.to_json + end + end + def get_failed_emails + send_failed_emails = PaperCriteria.find(params[:id]).send_failed_emails rescue nil + render :json => {'data' => (send_failed_emails.blank? ? 'None' : send_failed_emails)} + end + def get_invalid_emails + invalid_emails = PaperCriteria.find(params[:id]).invalid_emails rescue nil + render :json => {'data' => (invalid_emails.blank? ? 'None' : invalid_emails)} end - private def paper_criteria_params diff --git a/app/models/paper_criteria.rb b/app/models/paper_criteria.rb index a927f1e..cb83668 100644 --- a/app/models/paper_criteria.rb +++ b/app/models/paper_criteria.rb @@ -13,8 +13,8 @@ class PaperCriteria field :language_to_send, type: Array, :default => ["en","zh_tw"] field :receivers, type: Array, :default => ["subscriber"] field :other_emails - - + field :invalid_emails, type: Array, :default => [] + field :send_failed_emails, type: Array, :default => [] def epaper_topics tmp = EPaperTopic.where(:period.gte => self.start_date.in_time_zone(Time.zone.utc_offset / 3600), :period.lte => self.end_date.in_time_zone(Time.zone.utc_offset / 3600)).can_display tmp = EPaperTopic.where(:period.gte => self.start_date.in_time_zone(Time.zone.utc_offset / 3600), :period.lte => self.end_date.in_time_zone(Time.zone.utc_offset / 3600)).limit(1) if tmp.first.nil? diff --git a/app/views/admin/e_paper_criterias/_index.html.erb b/app/views/admin/e_paper_criterias/_index.html.erb index 514e631..8de8b73 100644 --- a/app/views/admin/e_paper_criterias/_index.html.erb +++ b/app/views/admin/e_paper_criterias/_index.html.erb @@ -16,6 +16,8 @@
  • <%= t(:edit) %>
  • <%= t('e_paper.test_mail') %>
  • <%= t('e_paper.send_newsletter') %>
  • +
  • <%= t('e_paper.get_failed_emails') %>
  • +
  • <%= t('e_paper.get_invalid_emails') %>
  • <%= t(:delete_) %>
  • @@ -25,6 +27,7 @@ <% end %> +
    <%= content_tag :div, paginate(@criterias), class: "pagination pagination-centered" %>
    @@ -33,7 +36,16 @@
    + \ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index 69681c4..9c9fef0 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1,7 +1,9 @@ en: module_name: e_paper: "ePaper" - e_paper: + e_paper: + get_invalid_emails: get invalid email + get_failed_emails: get sended-failed emails e_paper: "ePaper" all_articles: All Articles title: Title diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index fcb14d9..1c39424 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -1,7 +1,9 @@ zh_tw: module_name: e_paper: 電子報 - e_paper: + e_paper: + get_invalid_emails: 顯示格式錯誤的email + get_failed_emails: 顯示寄送失敗的email e_paper: "電子報" title: "文章標題" publish_date: "發行日期" diff --git a/config/routes.rb b/config/routes.rb index ef59198..17006f7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -8,6 +8,8 @@ Rails.application.routes.draw do member do get "send_test_email" get "send_newsletter" + get "get_failed_emails" + get "get_invalid_emails" end end resources :e_paper_subscribers diff --git a/lib/tasks/e_paper_tasks.rake b/lib/tasks/e_paper_tasks.rake index 8253cac..738841f 100644 --- a/lib/tasks/e_paper_tasks.rake +++ b/lib/tasks/e_paper_tasks.rake @@ -6,6 +6,7 @@ namespace :epaper do 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) @@ -13,15 +14,27 @@ namespace :epaper do 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.inspect,e,e.backtrace] - end + 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.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