require "mail" class ReadingsMailer SSL_OPTS = { :openssl_verify_mode => OpenSSL::SSL::VERIFY_NONE, } # IMAP_OPTS = { :address => "mail.van-halteren.net", # :port => 993, # :user_name => 'aart@van-halteren.net', # :password => 'XXXXX', # :openssl_verify_mode => OpenSSL::SSL::VERIFY_NONE, # :enable_ssl => true # } # # Class methods # class << self def deliver # Read SMTP options from smtp.yml smtp_opts = YAML::load(File.open('config/smtp.yml')).symbolize_keys smtp_opts.merge!(SSL_OPTS) if smtp_opts[:ssl] && smtp_opts[:ssl_verify_mode].eql?("none") # Fetch today's usage usage_today = Reading.diff_on(Date.today) c = Cost.new oxxio_cost = c.oxxio_energy_cost(Date.today.to_s,usage_today[:total_kwh_consumed_high]-usage_today[:total_kwh_produced_high], usage_today[:total_kwh_consumed_low]-usage_today[:total_kwh_produced_low]) easy_cost = c.easy_energy_cost_barplot(Date.today) # side effect: generates a PNG mail = Mail.new do delivery_method :smtp, smtp_opts to 'a.t.van.halteren@vu.nl' from 'SmartMeter ' subject "SmartMeter report for #{Date.today}" text_part do body "Summary for #{Date.today}\n -------------------------------\n\n Total kWH electricity consumed: #{usage_today[:total_kwh_consumed_high] + usage_today[:total_kwh_consumed_low]}\n Total kWH electricity produced: #{usage_today[:total_kwh_produced_high] + usage_today[:total_kwh_produced_low]}\n Total m3 gas consumed: #{usage_today[:total_m3_gas_consumed]}\n\n kWH cost (Oxxio): EUR #{ oxxio_cost }\n kWH cost (EasyEnergy): EUR #{ easy_cost }\n " end html_part do content_type 'text/html; charset=UTF-8' body "

Summary for #{Date.today}

" + "

Total kWH electricity consumed: #{usage_today[:total_kwh_consumed_high] + usage_today[:total_kwh_consumed_low]}

" + "

Total kWH electricity produced: #{usage_today[:total_kwh_produced_high] + usage_today[:total_kwh_produced_low]}

" + "

Total m3 gas consumed: #{usage_today[:total_m3_gas_consumed]}

" + "
" + "

kWH cost (Oxxio): EUR #{ oxxio_cost}

" + "

kWH cost (EasyEnergy): EUR #{ easy_cost}

" end end mail.deliver! end end end