epaper/lib/tasks/e_paper_tasks.rake

143 lines
4.8 KiB
Ruby
Raw Normal View History

2019-05-28 15:54:32 +00:00
# desc "Explaining what the task does"
namespace :epaper do
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)
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 = []
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)
@open_timeout = 30
@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|
2020-12-01 16:19:58 +00:00
invalid_mails = verify_email(from_addr,slice_emails,tls)
deliver_emails = slice_emails - invalid_mails
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)
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|
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)
end
rescue => e
puts [e,e.backtrace]
error_flag = 1
end
raise "There was something went wrong." if error_flag == 1
tmp += invalid_mails
end
2020-08-28 12:49:47 +00:00
paper.update_attributes(:send_failed_emails => tmp)
end
2020-08-28 12:49:47 +00:00
paper.invalid_emails = emails_invalid
paper.save
end
end
def verify_email(from_addr,check_emails,tls) #true if check_email is ok
s = Timeout.timeout(@open_timeout, Net::OpenTimeout) do
TCPSocket.open(@address, @port)
end
@socket = new_internet_message_io(tls ? tlsconnect(s) : s)
res = get_response("MAIL FROM:<#{from_addr}>")
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|
res1 = get_response("RCPT TO:<#{check_email}>")
res2 = get_response("RCPT TO:<#{check_email}>")
if !(res1.success? && res2.success?)
invalid_emails << check_email
puts [check_email,res1.message,res2.message]
end
2020-12-01 16:19:58 +00:00
end
s.close
invalid_emails
end
def get_response(reqline)
res = critical {
@socket.writeline reqline
recv_response()
}
return res
end
def recv_response
buf = ''
while true
line = @socket.readline
buf << line << "\n"
break unless line[3,1] == '-' # "210-PIPELINING"
end
Net::SMTP::Response.parse(buf)
end
def critical
return Net::SMTP::Response.parse('200 dummy reply code') if @error_occurred
begin
return yield()
rescue Exception
@error_occurred = true
raise
end
end
def ssl_socket(socket, context)
OpenSSL::SSL::SSLSocket.new socket, context
end
def tlsconnect(s)
verified = false
s = ssl_socket(s, @ssl_context)
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
def new_internet_message_io(s)
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