Class: Origen::Utility::Mailer

Inherits:
Object
  • Object
show all
Includes:
Origen::Users
Defined in:
lib/origen/utility/mailer.rb

Instance Method Summary collapse

Methods included from Origen::Users

#admins, #app_users, #current_user

Instance Method Details

#send_email(options = {}) ⇒ Object

Generic method to send an email, alternatively use one of the pre-defined mail types using the other methods.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
# File 'lib/origen/utility/mailer.rb', line 9

def send_email(options = {})
  options = { server:         Origen.site_config.email_server,
              port:           Origen.site_config.email_port,
              from:           current_user.email,
              from_alias:     current_user.name,
              subject:        'Hello',
              body:           'Hello from Origen!',
              to:             current_user.email,
              authentication: (Origen.site_config.email_authentication || :none).to_sym,
              domain:         (Origen.site_config.email_domain || ''),

              auth_user:      (Origen.site_config.email_auth_user || current_user.email),
              auth_password:  (Origen.site_config.email_auth_password || current_user.password) }.merge(options)

  # Force to an array
  to = options[:to].respond_to?('each') ? options[:to] : [options[:to]]

  # Convert any user objects to an email
  to = to.map { |obj| obj.respond_to?('email') ? obj.email : obj }

  to.uniq.each do |addr|
    msg = "From: \#{options[:from_alias]} <\#{options[:from]}>\nTo: \#{addr}\nSubject: \#{options[:subject]}\n\n\#{options[:body]}\n"

    begin
      Origen.log.debug('Origen::Utility::Mailer Setup:')
      # Must not save a user's password to the Origen log file! So build a shadow copy of the
      # options hash and display that content in the log file with the auth_password removed.
      options_reduced = options
      options_reduced.delete(:auth_password)
      options_reduced.each { |k, v| Origen.log.debug("  #{k}: #{v}") }

      # Net::SMTP.start(options[:server], options[:port]) do |smtp|
      #  smtp.send_message msg, options[:from], addr
      # end

      # Exceptions raised here will be caught by rescue clause
      smtp = Net::SMTP.new(options[:server], options[:port])
      smtp.enable_starttls if options[:authentication] != :none

      opts = if options[:authentication] == :none
               # Trying to add username and password if there's no authentication will actually be rejected by
               # the server.
               [options[:domain]]
             else
               [options[:domain], options[:auth_user], options[:auth_password], options[:authentication]]
             end

      smtp.start(*opts) do |m|
        m.send_message(msg, options[:from], addr)
      end

      # Exceptions raised here will be caught by rescue clause
      # smtp = Net::SMTP.new(options[:server], options[:port])
      # smtp.enable_starttls

      # smtp.start (options[:domain], options[:auth_user], options[:auth_password], options[:authentication]) do |smtp|
      #  smtp.send_message(msg, options[:from], addr)
      # end
    rescue
      warn "Email not able to be sent to address '#{addr}'"
    end
  end
end

#send_regression_complete_notice(stats = Origen.app.runner.stats, options = {}) ⇒ Object

Send a regression complete notice,



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/origen/utility/mailer.rb', line 142

def send_regression_complete_notice(stats = Origen.app.runner.stats, options = {})
  stats, options = Origen.app.runner.stats, stats if stats.is_a?(Hash)
  options = {
    to: current_user
  }.merge(options)

  msg = "Hi,\n\nThe regression results are:\n\n\#{stats.summary_text}\n\n  END1\n\n  subject = \"[\#{Origen.app.namespace}] Regression - \"\n  if stats.clean_run?\n    subject += 'PASSED'\n  else\n    subject += 'FAILED'\n  end\n  send_email({ to: options[:to], subject: subject, body: msg }.merge(options))\nend\n"

#send_release_notice(version, release_note, type, selectors, options = {}) ⇒ Object

Call to send a notice



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
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
# File 'lib/origen/utility/mailer.rb', line 80

def send_release_notice(version, release_note, type, selectors, options = {})
  if external?(type)
    header = "A new version of #{config.name} is available:"
  else
    header = "A new development version of #{config.name} is available:"
  end

  msg = "Hi,\n\n\#{header}\n\n    \#{version}    \#{selectors}\n\nRelease note:\n\n--------------------------------------------------------------------------------------\n\n\#{release_note}\n\n--------------------------------------------------------------------------------------\n    END1\n\n  if config.release_instructions\n    msg += <<-END2\n\n\#{config.release_instructions}\n\n--------------------------------------------------------------------------------------\n      END2\n  end\n\n  msg += <<-END3\n\nYou are receiving this because you are a member of the \#{config.name} Mailing List,\nor a member of the development team.\nEND3\n\n  if external?(type)\n    to = Origen.app.maillist_prod + Origen.app.maillist_dev\n    if config.release_email_subject\n      subject = \"[\#{Origen.app.namespace}] New Official Release: \#{config.release_email_subject}\"\n    else\n      subject = \"[\#{Origen.app.namespace}] New Official Release\"\n    end\n  else\n    to = Origen.app.maillist_dev\n    if config.release_email_subject\n      subject = \"[\#{Origen.app.namespace}] New Development Tag: \#{config.release_email_subject}\"\n    else\n      subject = \"[\#{Origen.app.namespace}] New Development Tag\"\n    end\n  end\n\n  begin\n    send_email({ to: to, subject: subject, body: msg }.merge(options))\n  rescue\n    warn \"Email could not be sent to \#{to}\"\n  end\nend\n"