Added real content to email body

This commit is contained in:
Aart van Halteren
2013-08-09 14:32:43 +02:00
parent b51937f9da
commit 33c45fc0e5
2 changed files with 51 additions and 27 deletions

View File

@@ -1,38 +1,50 @@
require "mail" require "mail"
class ReadingsMailer class ReadingsMailer
OPTS = { :address => "mail.van-halteren.net",
:port => 465,
:domain => 'van-halteren.net',
:user_name => 'aart@van-halteren.net',
:password => '',
:authentication => 'plain',
:enable_ssl => true}
OPTS_NO_SSL_VERIFY = { SSL_OPTS = {
:openssl_verify_mode => OpenSSL::SSL::VERIFY_NONE, :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 methods
# #
class << self class << self
def deliver 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")
mail = Mail.new do # Fetch today's usage
delivery_method :smtp, OPTS_NO_SSL_VERIFY usage_today = Reading.diff_on(Date.today)
to 'aart@van-halteren.net'
mail = Mail.new do
delivery_method :smtp, smtp_opts
to 'a.t.van.halteren@vu.nl'
from 'SmartMeter <aart@van-halteren.net>' from 'SmartMeter <aart@van-halteren.net>'
subject 'First multipart email sent with Mail' subject 'SmartMeter report'
text_part do text_part do
body 'This is plain text' 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
"
end end
html_part do # html_part do
content_type 'text/html; charset=UTF-8' # content_type 'text/html; charset=UTF-8'
body '<h1>This is HTML</h1>' # body '<h1>This is HTML</h1>'
end # end
end end
mail.deliver! mail.deliver!

View File

@@ -34,11 +34,19 @@ class Reading < ActiveRecord::Base
# calculate difference with another reading # calculate difference with another reading
# return a hash with differences (self - reading) # return a hash with differences (self - reading)
def diff(reading) def diff(reading)
{ :total_kwh_consumed_high => (self.total_kwh_consumed_high - reading.total_kwh_consumed_high).round(1), if reading
:total_kwh_consumed_low => (self.total_kwh_consumed_low - reading.total_kwh_consumed_low).round(1), { :total_kwh_consumed_high => (self.total_kwh_consumed_high - reading.total_kwh_consumed_high).round(1),
:total_kwh_produced_high => (self.total_kwh_produced_high - reading.total_kwh_produced_high).round(1), :total_kwh_consumed_low => (self.total_kwh_consumed_low - reading.total_kwh_consumed_low).round(1),
:total_kwh_produced_low => (self.total_kwh_produced_low - reading.total_kwh_produced_low).round(1), :total_kwh_produced_high => (self.total_kwh_produced_high - reading.total_kwh_produced_high).round(1),
:total_m3_gas_consumed => (self.total_m3_gas_consumed - reading.total_m3_gas_consumed).round(3) } :total_kwh_produced_low => (self.total_kwh_produced_low - reading.total_kwh_produced_low).round(1),
:total_m3_gas_consumed => (self.total_m3_gas_consumed - reading.total_m3_gas_consumed).round(3) }
else
{ :total_kwh_consumed_high => self.total_kwh_consumed_high,
:total_kwh_consumed_low => self.total_kwh_consumed_low,
:total_kwh_produced_high => self.total_kwh_produced_high,
:total_kwh_produced_low => self.total_kwh_produced_low,
:total_m3_gas_consumed => self.total_m3_gas_consumed }
end
end end
# #
@@ -58,7 +66,11 @@ class Reading < ActiveRecord::Base
readings_on = day(date) readings_on = day(date)
first = readings_on.first first = readings_on.first
last = readings_on.last last = readings_on.last
last.diff(first) if last
last.diff(first)
else
{ :total_kwh_consumed_high => 0, :total_kwh_consumed_low => 0, :total_kwh_produced_high => 0, :total_kwh_produced_low => 0, :total_m3_gas_consumed => 0 }
end
end end
end end
end end