fix error

This commit is contained in:
chiu 2020-08-28 20:49:47 +08:00
parent 648d7df6bb
commit f64b643c99
7 changed files with 100 additions and 25 deletions

View File

@ -56,18 +56,24 @@ def initialize
def send_newsletter def send_newsletter
Orbit::Application.load_tasks Orbit::Application.load_tasks
locale = I18n.locale rake = Rake::Task['epaper:send_email'].execute(OpenStruct.new({:paper_criteria_id => params[:id], :domain => request.host_with_port, :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'].invoke([params[:id], request.host_with_port]) # 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 end
private private
def paper_criteria_params def paper_criteria_params

View File

@ -13,8 +13,8 @@ class PaperCriteria
field :language_to_send, type: Array, :default => ["en","zh_tw"] field :language_to_send, type: Array, :default => ["en","zh_tw"]
field :receivers, type: Array, :default => ["subscriber"] field :receivers, type: Array, :default => ["subscriber"]
field :other_emails field :other_emails
field :invalid_emails, type: Array, :default => []
field :send_failed_emails, type: Array, :default => []
def epaper_topics 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)).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? 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?

View File

@ -16,6 +16,8 @@
<li><a href="<%= edit_admin_e_paper_criteria_path(criteria.id) %>"><%= t(:edit) %></a></li> <li><a href="<%= edit_admin_e_paper_criteria_path(criteria.id) %>"><%= t(:edit) %></a></li>
<li><a class="test-email" href="<%= send_test_email_admin_e_paper_criteria_path(criteria.id) %>"><%= t('e_paper.test_mail') %></a></li> <li><a class="test-email" href="<%= send_test_email_admin_e_paper_criteria_path(criteria.id) %>"><%= t('e_paper.test_mail') %></a></li>
<li><a class="send-newsletter" href="<%= send_newsletter_admin_e_paper_criteria_path(criteria.id) %>"><%= t('e_paper.send_newsletter') %></a></li> <li><a class="send-newsletter" href="<%= send_newsletter_admin_e_paper_criteria_path(criteria.id) %>"><%= t('e_paper.send_newsletter') %></a></li>
<li><a class="get_failed_emails" href="<%= get_failed_emails_admin_e_paper_criteria_path(criteria.id) %>"><%= t('e_paper.get_failed_emails') %></a></li>
<li><a class="get_failed_emails" href="<%= get_invalid_emails_admin_e_paper_criteria_path(criteria.id) %>"><%= t('e_paper.get_invalid_emails') %></a></li>
<li><a class="delete text-error" href="<%= admin_e_paper_criteria_path(criteria.id) %>" data-method="delete" data-confirm="Are you sure?"><%= t(:delete_) %></a></li> <li><a class="delete text-error" href="<%= admin_e_paper_criteria_path(criteria.id) %>" data-method="delete" data-confirm="Are you sure?"><%= t(:delete_) %></a></li>
</ul> </ul>
</div> </div>
@ -25,6 +27,7 @@
<% end %> <% end %>
</tbody> </tbody>
</table> </table>
<div id="show_epaper_message"></div>
<div class="bottomnav clearfix" style="left: 81px;"> <div class="bottomnav clearfix" style="left: 81px;">
<%= content_tag :div, paginate(@criterias), class: "pagination pagination-centered" %> <%= content_tag :div, paginate(@criterias), class: "pagination pagination-centered" %>
<div class="action pull-right"> <div class="action pull-right">
@ -33,7 +36,16 @@
</a> </a>
</div> </div>
</div> </div>
<style type="text/css">
#show_epaper_message span {
background: #9C27B0;
color: white;
}
</style>
<script type="text/javascript"> <script type="text/javascript">
function convert_email_to_html(data){
return ('count:&nbsp;'+data.length+'<br><pre><span>'+data.join('</span></pre><br><pre><span>')+'</span></pre>')
}
$(".test-email").on("click",function(){ $(".test-email").on("click",function(){
var el = $(this), var el = $(this),
email = prompt("Please enter an email address to send test email.", "<%= current_user.member_profile.email rescue "" %>"); email = prompt("Please enter an email address to send test email.", "<%= current_user.member_profile.email rescue "" %>");
@ -55,11 +67,49 @@
$.ajax({ $.ajax({
url : el.attr("href"), url : el.attr("href"),
dataType : "json", dataType : "json",
type : "get" type : "get",
}).done(function(){ success: function(data){
alert("Newsletter sent.") if (data.success){
alert("Newsletter sent.")
}else{
var window_width = window.innerWidth
var window_height = window.innerHeight
var show_html = (typeof(data.invalid_emails)=='string' ? data.invalid_emails : convert_email_to_html(data.invalid_emails))
$("div#show_epaper_message").html(show_html)
$("div#show_epaper_message").dialog({
autoOpen: true,
show: "blind",
model: true,
hide: "explode",
width: window_width*0.7,
height: window_height*0.6
})
}
}
}) })
} }
return false; return false;
}) })
$('.get_failed_emails').on('click',function(){
$.ajax({
url : $(this).attr("href"),
dataType : "json",
type : "get",
success: function(data){
var window_width = window.innerWidth
var window_height = window.innerHeight
var show_html = (typeof(data.data)=='string' ? data.data : convert_email_to_html(data.data))
$("div#show_epaper_message").html(show_html)
$("div#show_epaper_message").dialog({
autoOpen: true,
show: "blind",
model: true,
hide: "explode",
width: window_width*0.7,
height: window_height*0.6
})
}
})
return false;
})
</script> </script>

View File

@ -2,6 +2,8 @@ en:
module_name: module_name:
e_paper: "ePaper" e_paper: "ePaper"
e_paper: e_paper:
get_invalid_emails: get invalid email
get_failed_emails: get sended-failed emails
e_paper: "ePaper" e_paper: "ePaper"
all_articles: All Articles all_articles: All Articles
title: Title title: Title

View File

@ -2,6 +2,8 @@ zh_tw:
module_name: module_name:
e_paper: 電子報 e_paper: 電子報
e_paper: e_paper:
get_invalid_emails: 顯示格式錯誤的email
get_failed_emails: 顯示寄送失敗的email
e_paper: "電子報" e_paper: "電子報"
title: "文章標題" title: "文章標題"
publish_date: "發行日期" publish_date: "發行日期"

View File

@ -8,6 +8,8 @@ Rails.application.routes.draw do
member do member do
get "send_test_email" get "send_test_email"
get "send_newsletter" get "send_newsletter"
get "get_failed_emails"
get "get_invalid_emails"
end end
end end
resources :e_paper_subscribers resources :e_paper_subscribers

View File

@ -6,6 +6,7 @@ namespace :epaper do
papers_data = Admin::EpaperHelper.get_paper_data(paper) papers_data = Admin::EpaperHelper.get_paper_data(paper)
domain = args[:domain] domain = args[:domain]
if !paper.nil? 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 = EPaperSubscriber.where(:language.in=>paper.language_to_send,:email.nin=>['',nil])
subscribers.update_all(:last_paper_sent => paper.id.to_s) subscribers.update_all(:last_paper_sent => paper.id.to_s)
emails = subscribers.collect(&:email) emails = subscribers.collect(&:email)
@ -13,15 +14,27 @@ namespace :epaper do
email_limit_reciever = site[:site_settings][:email_limit_reciever].to_i rescue 0 email_limit_reciever = site[:site_settings][:email_limit_reciever].to_i rescue 0
mail_limit = email_limit_reciever==0 ? 25 : email_limit_reciever mail_limit = email_limit_reciever==0 ? 25 : email_limit_reciever
emails += paper.other_emails_ids emails += paper.other_emails_ids
emails.each_slice(mail_limit).each do |slice_emails| emails_valid = emails.select do |email|
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) tmp = (email_format=~email)
begin tmp==0 ? true : false
email.deliver
puts "Sending #{slice_emails.count} emails"
rescue => e
puts [slice_emails.inspect,e,e.backtrace]
end
end 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 paper.save
end end
end end