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
|
2021-10-06 04:22:59 +00:00
|
|
|
desc 'Send epaper emails'
|
2020-08-28 09:09:45 +00:00
|
|
|
task :send_email, [:paper_criteria_id, :domain, :locale] => :environment do |task,args|
|
2020-12-01 16:19:58 +00:00
|
|
|
require 'net/protocol'
|
|
|
|
require 'timeout'
|
|
|
|
require 'net/smtp'
|
|
|
|
require 'openssl'
|
|
|
|
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.-]+$/
|
2021-10-07 10:49:21 +00:00
|
|
|
subscribers = EPaperSubscriber.where(:language.in=>paper.language_to_send,:email.nin=>['',nil],:subscribed.ne=>false)
|
2020-08-28 09:09:45 +00:00
|
|
|
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
|
2022-05-17 04:18:42 +00:00
|
|
|
emails.uniq!
|
2020-08-28 12:49:47 +00:00
|
|
|
emails_valid = emails.select do |email|
|
2022-12-01 05:04:09 +00:00
|
|
|
tmp = (email_format=~email.to_s)
|
2020-08-28 12:49:47 +00:00
|
|
|
tmp==0 ? true : false
|
|
|
|
end
|
|
|
|
emails_invalid = emails - emails_valid
|
|
|
|
paper.send_failed_emails = []
|
2022-12-02 03:47:34 +00:00
|
|
|
paper.invalid_emails = emails_invalid
|
|
|
|
paper.save
|
2020-08-28 12:49:47 +00:00
|
|
|
Thread.new do
|
|
|
|
tmp = []
|
2020-12-01 16:19:58 +00:00
|
|
|
site = Site.first
|
|
|
|
@address = site['site_settings']['address']
|
|
|
|
@port = (site['site_settings']['port'].to_i rescue 587)
|
2021-11-01 03:40:14 +00:00
|
|
|
@open_timeout = 10
|
2020-12-01 16:19:58 +00:00
|
|
|
@read_timeout = 60
|
|
|
|
@debug_output = nil
|
|
|
|
tls = (site['site_settings']['enable_starttls_auto'] == "1")
|
|
|
|
from_addr = site['site_settings']['service_email']
|
2020-08-28 12:49:47 +00:00
|
|
|
emails_valid.each_slice(mail_limit).each do |slice_emails|
|
2021-10-06 03:55:50 +00:00
|
|
|
invalid_mails = epaper_verify_email(from_addr,slice_emails,tls)
|
2020-12-01 16:19:58 +00:00
|
|
|
deliver_emails = slice_emails - invalid_mails
|
2022-12-02 03:47:34 +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 => deliver_emails, :module_app_key=>'e_paper')
|
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
|
2020-12-01 16:19:58 +00:00
|
|
|
puts "Sending #{deliver_emails.count} emails"
|
2020-08-28 12:49:47 +00:00
|
|
|
rescue => e
|
2020-12-01 16:19:58 +00:00
|
|
|
puts [deliver_emails.inspect,e,e.backtrace]
|
|
|
|
tmp += deliver_emails
|
2020-08-28 12:49:47 +00:00
|
|
|
end
|
2020-12-01 16:19:58 +00:00
|
|
|
error_flag = 0
|
|
|
|
begin
|
|
|
|
invalid_mails.each do |invalid_mail|
|
2022-12-02 03:47:34 +00:00
|
|
|
email = Email.create(: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 => invalid_mail, :module_app_key=>'e_paper')
|
2020-12-01 16:19:58 +00:00
|
|
|
end
|
|
|
|
rescue => e
|
|
|
|
puts [e,e.backtrace]
|
|
|
|
error_flag = 1
|
|
|
|
end
|
|
|
|
raise "There was something went wrong." if error_flag == 1
|
|
|
|
tmp += invalid_mails
|
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
|
|
|
|
end
|
|
|
|
end
|
2021-10-06 03:55:50 +00:00
|
|
|
def epaper_verify_email(from_addr,check_emails,tls) #true if check_email is ok
|
2021-10-06 03:50:38 +00:00
|
|
|
s = Timeout.timeout(@open_timeout, Net::OpenTimeout) do
|
|
|
|
TCPSocket.open(@address, @port)
|
|
|
|
end
|
2021-10-06 03:55:50 +00:00
|
|
|
@socket = epaper_new_internet_message_io(tls ? epaper_tlsconnect(s) : s)
|
|
|
|
res = epaper_get_response("MAIL FROM:<#{from_addr}>")
|
2021-10-06 03:50:38 +00:00
|
|
|
invalid_emails = []
|
|
|
|
check_list = []
|
|
|
|
if check_emails.class == String
|
|
|
|
if check_emails.include?(",")
|
|
|
|
check_list = check_emails.split(",")
|
|
|
|
else
|
|
|
|
check_list << check_emails
|
|
|
|
end
|
|
|
|
else
|
|
|
|
check_list = check_emails
|
|
|
|
end
|
|
|
|
check_list.each do |check_email|
|
2021-11-01 03:40:14 +00:00
|
|
|
begin
|
|
|
|
res1 = epaper_get_response("RCPT TO:<#{check_email}>")
|
|
|
|
res2 = epaper_get_response("RCPT TO:<#{check_email}>")
|
|
|
|
if !(res1.success? && res2.success?)
|
|
|
|
invalid_emails << check_email
|
|
|
|
puts [check_email,res1.message,res2.message]
|
|
|
|
end
|
|
|
|
rescue => e
|
|
|
|
puts e.to_s
|
2021-10-06 03:50:38 +00:00
|
|
|
invalid_emails << check_email
|
|
|
|
end
|
2020-12-01 16:19:58 +00:00
|
|
|
end
|
2021-10-06 03:50:38 +00:00
|
|
|
s.close
|
|
|
|
invalid_emails
|
|
|
|
end
|
2021-10-06 03:55:50 +00:00
|
|
|
def epaper_get_response(reqline)
|
|
|
|
res = epaper_critical {
|
2021-10-06 03:50:38 +00:00
|
|
|
@socket.writeline reqline
|
2021-10-06 03:55:50 +00:00
|
|
|
epaper_recv_response()
|
2021-10-06 03:50:38 +00:00
|
|
|
}
|
|
|
|
return res
|
|
|
|
end
|
2021-10-06 03:55:50 +00:00
|
|
|
def epaper_recv_response
|
2021-10-06 03:50:38 +00:00
|
|
|
buf = ''
|
|
|
|
while true
|
|
|
|
line = @socket.readline
|
|
|
|
buf << line << "\n"
|
|
|
|
break unless line[3,1] == '-' # "210-PIPELINING"
|
|
|
|
end
|
|
|
|
Net::SMTP::Response.parse(buf)
|
|
|
|
end
|
2021-10-06 03:55:50 +00:00
|
|
|
def epaper_critical
|
2021-10-06 03:50:38 +00:00
|
|
|
return Net::SMTP::Response.parse('200 dummy reply code') if @error_occurred
|
|
|
|
begin
|
|
|
|
return yield()
|
|
|
|
rescue Exception
|
|
|
|
@error_occurred = true
|
|
|
|
raise
|
|
|
|
end
|
|
|
|
end
|
2021-10-06 03:55:50 +00:00
|
|
|
def epaper_ssl_socket(socket, context)
|
2021-10-06 03:50:38 +00:00
|
|
|
OpenSSL::SSL::SSLSocket.new socket, context
|
|
|
|
end
|
2021-10-06 03:55:50 +00:00
|
|
|
def epaper_tlsconnect(s)
|
2021-10-06 03:50:38 +00:00
|
|
|
verified = false
|
2021-10-06 03:55:50 +00:00
|
|
|
s = epaper_ssl_socket(s, @ssl_context)
|
2021-10-06 03:50:38 +00:00
|
|
|
s.sync_close = true
|
|
|
|
s.connect
|
|
|
|
if @ssl_context.verify_mode != OpenSSL::SSL::VERIFY_NONE
|
|
|
|
s.post_connection_check(@address)
|
|
|
|
end
|
|
|
|
verified = true
|
|
|
|
s
|
|
|
|
ensure
|
|
|
|
s.close unless verified
|
|
|
|
end
|
2021-10-06 03:55:50 +00:00
|
|
|
def epaper_new_internet_message_io(s)
|
2021-10-06 03:50:38 +00:00
|
|
|
io = Net::InternetMessageIO.new(s)
|
|
|
|
io.read_timeout = @read_timeout
|
|
|
|
io.debug_output = @debug_output
|
|
|
|
io
|
2020-12-01 16:19:58 +00:00
|
|
|
end
|
|
|
|
end
|
2021-10-06 03:50:38 +00:00
|
|
|
|