Fix mail verify.

This commit is contained in:
邱博亞 2024-09-15 11:00:37 +08:00
parent 59b23772c3
commit 4e7be83b86
1 changed files with 20 additions and 7 deletions

View File

@ -32,13 +32,20 @@ namespace :epaper do
Thread.new do Thread.new do
tmp = [] tmp = []
site = Site.first site = Site.first
if site[:gmail_login_flag]
@address = '127.0.0.1'
@port = 25
tls = false
from_addr = site.gmail
else
@address = site['site_settings']['address'] @address = site['site_settings']['address']
@port = (site['site_settings']['port'].to_i rescue 587) @port = (site['site_settings']['port'].to_i rescue 587)
tls = (site['site_settings']['enable_starttls_auto'] == "1")
from_addr = site['site_settings']['service_email']
end
@open_timeout = 10 @open_timeout = 10
@read_timeout = 60 @read_timeout = 60
@debug_output = nil @debug_output = nil
tls = (site['site_settings']['enable_starttls_auto'] == "1")
from_addr = site['site_settings']['service_email']
emails_valid.each_slice(mail_limit).each do |slice_emails| emails_valid.each_slice(mail_limit).each do |slice_emails|
invalid_mails = epaper_verify_email(from_addr,slice_emails,tls) invalid_mails = epaper_verify_email(from_addr,slice_emails,tls)
deliver_emails = slice_emails - invalid_mails deliver_emails = slice_emails - invalid_mails
@ -49,7 +56,7 @@ namespace :epaper do
puts "Emails will be sent at #{email_sentdate.strftime("%Y-%m-%d %H:%M")}" puts "Emails will be sent at #{email_sentdate.strftime("%Y-%m-%d %H:%M")}"
else else
email.deliver email.deliver
puts "Sending #{deliver_emails.count} emails" puts "Sending #{deliver_emails.count} emails: #{deliver_emails}"
end end
rescue => e rescue => e
puts [deliver_emails.inspect,e,e.backtrace] puts [deliver_emails.inspect,e,e.backtrace]
@ -72,9 +79,15 @@ namespace :epaper do
end end
end end
def epaper_verify_email(from_addr,check_emails,tls) #true if check_email is ok def epaper_verify_email(from_addr,check_emails,tls) #true if check_email is ok
return [] if @mail_server_broken
begin
s = Timeout.timeout(@open_timeout, Net::OpenTimeout) do s = Timeout.timeout(@open_timeout, Net::OpenTimeout) do
TCPSocket.open(@address, @port) TCPSocket.open(@address, @port)
end end
rescue => e
puts [e.to_s, e.backtrace]
@mail_server_broken = true
end
@socket = epaper_new_internet_message_io(tls ? epaper_tlsconnect(s) : s) @socket = epaper_new_internet_message_io(tls ? epaper_tlsconnect(s) : s)
res = epaper_get_response("MAIL FROM:<#{from_addr}>") res = epaper_get_response("MAIL FROM:<#{from_addr}>")
invalid_emails = [] invalid_emails = []