added emails and sending emails also test emails

This commit is contained in:
Harry Bomrah 2019-07-01 20:06:33 +08:00
parent f2301e7506
commit 121a24dd5e
9 changed files with 89 additions and 17 deletions

View File

@ -1,3 +1,4 @@
require 'rake'
class Admin::EPaperCriteriasController < OrbitAdminController class Admin::EPaperCriteriasController < OrbitAdminController
def initialize def initialize
@ -46,10 +47,17 @@ def initialize
def send_test_email def send_test_email
paper_criteria = PaperCriteria.find(params[:id]) paper_criteria = PaperCriteria.find(params[:id])
page = Page.where(:module_app => "e_paper").first.url rescue "" page = Page.where(:module => "e_paper").first.url rescue ""
email = Email.new(:mail_subject => paper_criteria.title, :mail_lang => :zh_tw, :template => "admin/e_paper_criterias/emailer", :template_data => {"paper" => paper_criteria, "page" => "http://" + request.host_with_port + page}, :mail_to => params[:email]) email = Email.new(:mail_subject => paper_criteria.sending_title, :mail_lang => :zh_tw, :template => "admin/e_paper_criterias/emailer", :template_data => {"paper" => paper_criteria, "page" => "http://" + request.host_with_port + page}, :mail_to => params[:email])
email.deliver email.deliver
render :json => {"success" => true}.to_json
end
def send_newsletter
Orbit::Application.load_tasks
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'].invoke([params[:id], request.host_with_port])
render :json => {"success" => true}.to_json
end end
private private

View File

@ -10,13 +10,21 @@ class PaperCriteria
field :sending_time, type: DateTime field :sending_time, type: DateTime
field :sending_title, localize: true field :sending_title, localize: true
field :sending_notes field :sending_notes
field :language_to_send, type: Array, :default => [] field :language_to_send, type: Array, :default => ["en","zh_tw"]
field :receivers, type: Array, :default => [] field :receivers, type: Array, :default => ["subscriber"]
field :other_emails field :other_emails
def epaper_topics def epaper_topics
EPaperTopic.where(:period.gte => self.start_date, :period.lte => self.end_date).can_display EPaperTopic.where(:period.gte => self.start_date, :period.lte => self.end_date).can_display
end end
def other_emails_ids
if !self.other_emails.nil?
return self.other_emails.split(",")
else
return []
end
end
end end

View File

@ -27,13 +27,13 @@
<div class="tab-pane fade in active" id="basic"> <div class="tab-pane fade in active" id="basic">
<!-- Date Time Picker --> <!-- Date Time Picker -->
<div class="control-group"> <!-- <div class="control-group">
<label class="control-label muted"><%= t(:start_date) %></label> <label class="control-label muted"><%= t(:start_date) %></label>
<div class="controls"> <div class="controls">
<%= f.datetime_picker :start_date, :no_label => true, :new_record => @paper_criteria.new_record?, :data=>{"picker-type" => "range", "range" => "start"} %> <%= f.datetime_picker :start_date, :no_label => true, :new_record => @paper_criteria.new_record?, :data=>{"picker-type" => "range", "range" => "start"} %>
</div> </div>
</div> </div>
-->
<div class="control-group"> <div class="control-group">
<label class="control-label muted"><%= t(:end_date) %></label> <label class="control-label muted"><%= t(:end_date) %></label>
<div class="controls"> <div class="controls">
@ -77,7 +77,7 @@
</div> </div>
<% end %> <% end %>
</div> </div>
<div class="control-group"> <!-- <div class="control-group">
<label class="control-label muted"><%= t("e_paper.receivers") %></label> <label class="control-label muted"><%= t("e_paper.receivers") %></label>
<div class="controls"> <div class="controls">
<label> <label>
@ -89,7 +89,7 @@
<input type="checkbox" <%= @paper_criteria.receivers.include?("website_member") ? "checked=checked" : "" %> name="paper_criteria[receivers][]" value="member"> <%= t("e_paper.website_member") %> <input type="checkbox" <%= @paper_criteria.receivers.include?("website_member") ? "checked=checked" : "" %> name="paper_criteria[receivers][]" value="member"> <%= t("e_paper.website_member") %>
<label> <label>
</div> </div>
</div> </div> -->
<div class="control-group"> <div class="control-group">
<label class="control-label muted"><%= t("e_paper.other_emails") %></label> <label class="control-label muted"><%= t("e_paper.other_emails") %></label>
<div class="controls"> <div class="controls">

View File

@ -15,6 +15,7 @@
<ul class="nav nav-pills"> <ul class="nav nav-pills">
<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="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>
@ -48,4 +49,17 @@
} }
return false; return false;
}) })
$(".send-newsletter").on("click",function(){
var el = $(this);
if(confirm("Are you sure?")){
$.ajax({
url : el.attr("href"),
dataType : "json",
type : "get"
}).done(function(){
alert("Newsletter sent.")
})
}
return false;
})
</script> </script>

View File

@ -150,19 +150,18 @@
<div class="epaper-show"> <div class="epaper-show">
<h3 class="epaper-show-title"><%= t('e_paper.intro') %></h3> <h3 class="epaper-show-title"><%= t('e_paper.intro') %></h3>
<div class="ep-info"> <div class="ep-info">
<div class="ep-info-title"><%= t('e_paper.title') %><span><%= paper.title %></span></div> <div class="ep-info-title"><%= t('e_paper.title') %><a class="ep-content-title" href="<%= page + "/" + paper.to_param + "?method=topics" %>" target="_blank"><span><%= paper.title %></span></a></div>
<div class="ep-info-date"><%= t('e_paper.date') %><span><%= paper.created_at.strftime("%Y-%m-%d") %></span></div> <div class="ep-info-date"><%= t('e_paper.date') %><span><%= paper.created_at.strftime("%Y-%m-%d") %></span></div>
<div class="ep-info-period"><%= t('e_paper.period') %><span><%= paper.period.strftime("%Y-%m-%d") %></span></div>
<div class="ep-info-description"><%= t('e_paper.description') %><span><%= paper.description %></span></div> <div class="ep-info-description"><%= t('e_paper.description') %><span><%= paper.description %></span></div>
</div> </div>
<div class="ep-content"> <div class="ep-content">
<% paper.epaper_topics.desc(:created_at).each do |topic| %> <% paper.epaper_topics.desc(:created_at).each do |topic| %>
<div class="ep-content-item"> <div class="ep-content-item">
<a class="ep-content-title" href="<%= page + "/" + paper.to_param + "?method=showtopic" %>" target="_blank"><%= topic.episode %></a> <a class="ep-content-title" href="<%= page + "/" + topic.to_param %>" target="_blank"><%= topic.title %></a>
<% t = strip_tags(topic.content) %> <% t = strip_tags(topic.content) %>
<p class="ep-content-text"><%= t[0..100] %></p> <p class="ep-content-text"><%= t[0..100] %></p>
<a class="ep-content-more" href="<%= page + "/" + paper.to_param + "?method=showtopic" %>" target="_blank">more</a> <a class="ep-content-more" href="<%= page + "/" + topic.to_param %>" target="_blank">more</a>
</div> </div>
<% end %> <% end %>
</div> </div>

View File

@ -34,3 +34,4 @@ en:
website_member: Website Members website_member: Website Members
other_email: Other Email other_email: Other Email
receivers: Receivers receivers: Receivers
send_newsletter: Send Newsletter

View File

@ -34,4 +34,5 @@ zh_tw:
subscriber: 訂閱者 subscriber: 訂閱者
website_member: 網站會員 website_member: 網站會員
other_email: 其他Email other_email: 其他Email
receivers: 收件人 receivers: 收件人
send_newsletter: 發送時事通訊

View File

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

View File

@ -1,4 +1,44 @@
# desc "Explaining what the task does" # desc "Explaining what the task does"
# task :e_paper do namespace :epaper do
# # Task goes here task :send_email, [:paper_criteria_id, :domain] => :environment do |task,args|
# end paper = PaperCriteria.find(args[:paper_criteria_id]) rescue nil
page = Page.where(:module => "e_paper").first.url rescue ""
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.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}, :mail_to => emails)
email.deliver
emails = []
sleep 0.5
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}, :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}, :mail_to => emails)
email.deliver
end
paper.save
end
end
end