Class: NagiosHerald::Message::Email

Inherits:
NagiosHerald::Message show all
Defined in:
lib/nagios-herald/messages/email.rb

Instance Attribute Summary collapse

Attributes inherited from NagiosHerald::Message

#content, #recipients

Instance Method Summary collapse

Methods inherited from NagiosHerald::Message

inherited, message_types

Methods included from Util

#get_nagios_var, get_script_path, load_helper, underscore_to_camel_case, #unescape_text

Methods included from Logging

#configure_logger_for, #logger, #logger_for

Constructor Details

#initialize(recipients, options = {}) ⇒ Email

Public: Initializes a new Message::Email object.

recipients - A list of recipients for this message. options - The options hash from Executor. FIXME: Is that ^^ necessary now with Config.config available?

Returns a new Message::Email object.



20
21
22
23
24
25
26
27
# File 'lib/nagios-herald/messages/email.rb', line 20

def initialize(recipients, options = {})
  @replyto     = options[:replyto]
  @subject     = ""
  @text        = ""
  @html        = ""
  @attachments = []
  super(recipients, options)
end

Instance Attribute Details

#attachmentsObject

Returns the value of attribute attachments.



8
9
10
# File 'lib/nagios-herald/messages/email.rb', line 8

def attachments
  @attachments
end

#htmlObject

Returns the value of attribute html.



9
10
11
# File 'lib/nagios-herald/messages/email.rb', line 9

def html
  @html
end

#subjectObject

Returns the value of attribute subject.



10
11
12
# File 'lib/nagios-herald/messages/email.rb', line 10

def subject
  @subject
end

#textObject

Returns the value of attribute text.



11
12
13
# File 'lib/nagios-herald/messages/email.rb', line 11

def text
  @text
end

Instance Method Details

#build_messageObject

Public: Builds the email message.

Returns the mail object.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/nagios-herald/messages/email.rb', line 96

def build_message
  curate_text
  curate_html
  @subject = self.content[:subject]
  if @no_send
    self.print
    return
  end

  mail = Mail.new({
    :from  => @replyto,
    :to    => @recipients,
    :subject => @subject,
    :content_type => 'multipart/alternative'
  })

  text_content = @text
  text_part = Mail::Part.new do
    body text_content
  end

  mail.add_part(text_part)

  html_part = Mail::Part.new do
    content_type 'multipart/related;'
  end

  # Load the attachments
  @attachments = self.content[:attachments]
  @attachments.each do |attachment|
    html_part.attachments[attachment] = File.read(attachment)
  end

  if @html != ""
    # Inline the attachment if need be
    inline_html = inline_body_with_attachments(html_part.attachments)
    html_content_part = Mail::Part.new do
      content_type 'text/html; charset=UTF-8'
      body     inline_html
    end
    html_part.add_part(html_content_part)
  end

  mail.add_part(html_part)
  mail
end

#curate_htmlObject

Public: Generates the HTML portion of the content hash.

Returns the full HTML portion of the content hash.



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/nagios-herald/messages/email.rb', line 67

def curate_html
  @html += self.content[:html][:host_info] unless self.content[:html][:host_info].empty?
  @html += self.content[:html][:state_info] unless self.content[:html][:state_info].empty?
  @html += self.content[:html][:additional_info] unless self.content[:html][:additional_info].empty?
  @html += self.content[:html][:action_url] unless self.content[:html][:action_url].empty?
  @html += self.content[:html][:notes] unless self.content[:html][:notes].empty?
  @html += self.content[:html][:additional_details] unless self.content[:html][:additional_details].empty?
  @html += self.content[:html][:recipients_email_link] unless self.content[:html][:recipients_email_link].empty?
  @html += self.content[:html][:notification_info] unless self.content[:html][:notification_info].empty?
  @html += self.content[:html][:alert_ack_url] unless self.content[:html][:alert_ack_url].empty?
  # Hack to ensure we get ack info, if it's populated.
  @html += self.content[:html][:ack_info] if self.content[:html][:ack_info] and !self.content[:html][:ack_info].empty?
end

#curate_textObject

Public: Generates the text portion of the content hash.

Returns the full text portion of the content hash.



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/nagios-herald/messages/email.rb', line 50

def curate_text
  @text += self.content[:text][:host_info] unless self.content[:text][:host_info].empty?
  @text += self.content[:text][:state_info] unless self.content[:text][:state_info].empty?
  @text += self.content[:text][:additional_info] unless self.content[:text][:additional_info].empty?
  @text += self.content[:text][:action_url] unless self.content[:text][:action_url].empty?
  @text += self.content[:text][:notes] unless self.content[:text][:notes].empty?
  @text += self.content[:text][:additional_details] unless self.content[:text][:additional_details].empty?
  @text += self.content[:text][:recipients_email_link] unless self.content[:text][:recipients_email_link].empty?
  @text += self.content[:text][:notification_info] unless self.content[:text][:notification_info].empty?
  @text += self.content[:text][:alert_ack_url] unless self.content[:text][:alert_ack_url].empty?
  # Hack to ensure we get ack info, if it's populated.
  @text += self.content[:text][:ack_info] if self.content[:text][:ack_info] and !self.content[:text][:ack_info].empty?
end

#inline_body_with_attachments(attachments) ⇒ Object

this is a list of Mail::Part

> #<Mail::Part:19564000, Multipart: false, Headers: <Content-Type: ; filename=“Rakefile”>, <Content-Transfer-Encoding: binary>, <Content-Disposition: attachment; filename=“Rakefile”>, <Content-ID: <[email protected]>>>

Public: Updates HTML content so that attachments are referenced via the ‘cid:’ URI scheme and neatly embedded in the body of a(n email) message.

attachments - The list of attachments defined in the formatter content hash.

Returns the updated HTML content.



37
38
39
40
41
42
43
44
45
# File 'lib/nagios-herald/messages/email.rb', line 37

def inline_body_with_attachments(attachments)
  inline_html = @html
  attachments.each do |attachment|
    if (inline_html =~ /#{attachment.filename}/)
      inline_html = inline_html.sub(attachment.filename, "cid:#{attachment.cid}")
    end
  end
  inline_html
end

Public: Prints the subject, text and HTML content to the terminal. Useful for debugging.

Returns nothing.



85
86
87
88
89
90
91
# File 'lib/nagios-herald/messages/email.rb', line 85

def print
  puts "------------------"
  puts "Subject : #{@subject}"
  puts "------------------"
  puts @text if !@text.empty?
  puts @html if !@html.empty?
end

#sendObject



143
144
145
146
# File 'lib/nagios-herald/messages/email.rb', line 143

def send
  mail = self.build_message
  mail.deliver! unless mail.nil?
end