Class: WCC::MailNotificator

Inherits:
Object
  • Object
show all
Defined in:
lib/wcc/mail.rb

Constant Summary collapse

@@main =
nil
@@bodies =
nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ MailNotificator

Returns a new instance of MailNotificator.



45
46
47
# File 'lib/wcc/mail.rb', line 45

def initialize(opts)
	@to = MailAddress.new(opts)
end

Class Method Details

.get_bodiesObject



143
144
145
146
147
148
149
150
151
# File 'lib/wcc/mail.rb', line 143

def self.get_bodies
	if @@bodies.nil?
		@@bodies = {
			:plain => WCC::Prog.load_template('mail-body.plain.erb'),
			:html => WCC::Prog.load_template('mail-body.html.erb')
		}
	end
	@@bodies
end

.get_mainObject

template loading



136
137
138
139
140
141
# File 'lib/wcc/mail.rb', line 136

def self.get_main
	if @@main.nil?
		@@main = WCC::Prog.load_template('mail.alt.erb')
	end
	@@main
end

.parse_conf(conf) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/wcc/mail.rb', line 76

def self.parse_conf(conf)
	if conf.is_a?(Hash)
		if conf['smtp'].is_a?(Hash)
			from_mail = MailAddress.new(conf['smtp']['from'] || "#{Etc.getlogin}@localhost")
			return {
				:mailer => 'smtp',
				:from_mail => from_mail,
				:smtp_host => conf['smtp']['host'] || 'localhost',
				:smtp_port => conf['smtp']['port'] || 25
			}
		elsif conf['fake_file'].is_a?(Hash)
			return {
				:mailer => 'fake_file',
				:from_mail => conf['fake_file']['from'] || "#{Etc.getlogin}@localhost"
			}
		end
	end
	# default is smtp
	WCC.logger.debug "Using default SMTP configuration for MailNotificator"
	return {
		:mailer => 'smtp',
		:from_mail => MailAddress.new("#{Etc.getlogin}@localhost"),
		:smtp_host => 'localhost',
		:smtp_port => 25
	}
end

.send_fake_file(msg, from, to) ⇒ Object

This just dumps a mail’s contents into an eml file in the current working directory. This should be for TESTING ONLY as it doesn’t take care of standards and stuff like that…

Parameters:



129
130
131
132
133
# File 'lib/wcc/mail.rb', line 129

def self.send_fake_file(msg, from, to)
	# dump mail to eml-file
	filename = "#{Time.new.strftime('%Y%m%d-%H%M%S')} #{to.name}.eml"
	File.open(filename, 'w') { |f| f.write(msg) }
end

.send_smtp(msg, from, to, host, port) ⇒ Object

This is a specific implementation of an mail deliverer that does plain SMTP to host:port using [Net::SMTP].

Parameters:

  • msg (String)

    the mail

  • from (MailAddress)

    the From: address

  • to (MailAddress)

    array of To: address

  • host (String)

    the SMTP host

  • port (Integer)

    the SMTP port



113
114
115
116
117
118
119
120
# File 'lib/wcc/mail.rb', line 113

def self.send_smtp(msg, from, to, host, port)
	# send message
	Net::SMTP.start(host, port) do |smtp|
		smtp.send_message(msg, from.address, to.address)
	end
rescue => ex
	WCC.logger.fatal "Cannot send mail via SMTP to #{host}:#{port} : #{ex}"
end

.shut_downObject



103
# File 'lib/wcc/mail.rb', line 103

def self.shut_down; end

Instance Method Details

#notify!(data) ⇒ Object

Sends a mail built up from some [ERB] templates to the specified adresses.

Parameters:

  • data (Object)

    used to construct ERB binding



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/wcc/mail.rb', line 53

def notify!(data)
	# from/to addresses
	data.from = Conf[:from_mail]
	data.to = @to
	# generate a boundary that may be used for multipart
	data.boundary = "frontier-#{data.site.id}"
	# generate message
	data.bodies = {}
	# eval all body templates
	self.class.get_bodies.each do |name,template|
		data.bodies[name] = template.result(binding)
	end
	# eval main template
	msg = self.class.get_main.result(binding)
	
	case Conf[:mailer]
	when 'smtp'
		self.class.send_smtp(msg, Conf[:from_mail], @to, Conf[:smtp_host], Conf[:smtp_port])
	when 'fake_file'
		self.class.send_fake_file(msg, Conf[:from_mail], @to)
	end
end