class MessagesController < ApplicationController before_action :set_message, only: %i[ show ] # GET /messages or /messages.json def index @search_term="" all_mail = Message.select("messages.id,messages.message_id,messages.token,messages.mail_from,messages.rcpt_to,messages.subject,messages.timestamp,messages.last_delivery_attempt,messages.size,messages.status,deliveries.details").joins("INNER JOIN deliveries ON messages.id=deliveries.message_id") from = params[:mail_from] to = params[:rcpt_to] if !params[:date_to].nil? date_to = Time.parse(params[:date_to]).to_i.to_s if !params[:date_to].empty? to_next_day = (Time.parse(params[:date_to]).to_i + 86400).to_s if !params[:date_to].empty? end if !params[:date_to].nil? date_from = Time.parse(params[:date_from]).to_i.to_s if !params[:date_from].empty? from_next_day = (Time.parse(params[:date_from]).to_i + 86400).to_s if !params[:date_from].empty? end status = params[:status] from ||= '' to ||= '' if !from.blank? @search_term << 'messages.mail_from="'+from+'"' end if !to.blank? if !@search_term.empty? @search_term << " AND " end @search_term << 'messages.rcpt_to="'+to+'"' puts "search term is #{@search_term}" end if !date_to.nil? if !@search_term.empty? @search_term << " AND " end if params[:date_from].nil? or params[:date_from].empty? @search_term << 'messages.timestamp>'+date_to+" AND messages.timestamp<"+ to_next_day else @search_term << 'messages.timestamp<='+date_to end end if !date_from.nil? if !@search_term.empty? @search_term << " AND " end if params[:date_to].nil? or params[:date_to].empty? @search_term << 'messages.timestamp>'+date_from+" AND messages.timestamp<"+ from_next_day else @search_term << 'messages.timestamp>='+date_from end end if params[:status].blank? params[:status] = 'All' end if params[:status] != "All" and @search_term.blank? @search_term << 'messages.status="'+params[:status]+'"' elsif status != "All" and !@search_term.blank? @search_term << ' AND messages.status="'+params[:status]+'"' end if @search_term.blank? mail = all_mail else mail = all_mail.where("#{@search_term}") end @@results = mail @pagy, @messages = pagy(mail.order(id: :desc)) end # GET /messages/1 or /messages/1.json def show end def export if File.file?("#{Rails.root}/mail_delivery_report.csv") File.delete("#{Rails.root}/mail_delivery_report.csv") end file = File.open("mail_delivery_report.csv", "a") file.write("ID|SENDER|RECIPIENT|SUBJECT|DATE|TIME|SIZE|STATUS|DELIVERY DETAILS\n") @@results.each do |mail| file.write("#{mail.id}|#{mail.mail_from}|#{mail.rcpt_to}|#{mail.subject}|#{Time.at(mail.timestamp).strftime('%d-%m-%Y')}|#{Time.at(mail.timestamp).strftime('%H:%M:%S')}|#{ mail.size.to_f >= 1048576 ? "#{(mail.size.to_d/1048576).round(2)} MB" : "#{(mail.size.to_d/1024).round(2)} KB" }|#{mail.status}|#{mail.details}\n") end file.close send_file("#{Rails.root}/mail_delivery_report.csv") end # POST /messages or /messages.json private # Use callbacks to share common setup or constraints between actions. def set_message @message = Message.find(params[:id]) end # Only allow a list of trusted parameters through. def message_params params.require(:message).permit(:token, :scope, :rcpt_to, :mail_from, :subject, :message_id, :timestamp, :route_id, :domain_id, :credential_id, :status, :held, :size, :last_delivery_attempt, :raw_table, :raw_body_id, :raw_headers_id, :inspected, :spam, :spam_score, :threat, :threat_details, :bounce, :bounce_for_id, :tag, :loaded, :clicked, :received_with_ssl, :hold_expiry, :tracked_links, :tracked_images, :parsed, :endpoint_id, :endpoint_type) end end