Class: PostRemoteLog::Methods::Email

Inherits:
Object
  • Object
show all
Defined in:
lib/post_remote_log/methods/email.rb

Class Method Summary collapse

Class Method Details

.send(config, values) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
102
103
104
105
106
107
108
# File 'lib/post_remote_log/methods/email.rb', line 25

def self.send(config, values)
	message = PostRemoteLog.build_xml_message(values)

	email = StringIO.new
	marker = Digest::MD5.hexdigest(Time.now.to_s)

	from = "#{values[:user]}@#{values[:hostname]}"
	to = config[:to]

	email.puts "From: #{from}"
	email.puts "To: #{to}"
	email.puts "Subject: Remote Log: #{values[:classification]}"
	email.puts "MIME-Version: 1.0"
	email.puts "Content-Type: multipart/mixed; boundary=#{marker}"
	email.puts
	email.puts "--#{marker}"
	email.puts "Content-Type: text/plain"
	email.puts "Content-Transfer-Encoding: 8bit"
	email.puts
	email.puts "A remote log was created and sent at #{Time.now.to_s}. Here are the details:"
	
	[:classification, :user, :uptime, :system, :hostname, :address].each do |key|
		email.puts "\t#{key}: #{values[key]}"
	end
	
	email.puts
	
	email.puts values[:report]
	
	email.puts
	email.puts
	email.puts "--#{marker}"
	email.puts "Content-Type: text/xml; name=\"remote_log.xml\""
	email.puts "Content-Transfer-Encoding:base64"
	email.puts "Content-Disposition: attachment; filename=\"remote_log.xml\""
	email.puts
	
	email.puts [message.string].pack("m")
	
	email.puts
	email.puts "--#{marker}"
	email.puts "Content-Type: text/plain; name=\"report.txt\""
	email.puts "Content-Transfer-Encoding:base64"
	email.puts "Content-Disposition: attachment; filename=\"report.txt\""
	email.puts
	
	email.puts [values[:report]].pack("m")
	
	email.puts "--#{marker}--"
	
	port = 25
	
	if config[:tls]
		port = 587
	end
	
	port = config[:port] || port
	
	smtp = Net::SMTP.new(config[:host], port)
	
	begin
		if config[:tls]
			unless smtp.respond_to? :enable_starttls
				raise ArgumentError.new("STARTTLS is not supported by this version of Ruby.")
			end
			
			context = Net::SMTP.default_ssl_context
			context.verify_mode = OpenSSL::SSL::VERIFY_NONE
			
			smtp.enable_starttls(context)
		end
		
		auth = config[:auth] ? config[:auth].to_sym : nil
		auth ||= :plain if config[:password]
		
		smtp.start(values[:hostname], config[:user], config[:password], auth)
		
		smtp.sendmail(email.string, from, [to])
	ensure
		if smtp.started?
			smtp.finish
		end
	end
end