Module: Gat::Email

Included in:
Base
Defined in:
lib/gat/email.rb

Instance Method Summary collapse

Instance Method Details

#create_and_deliver_email(to, email_config, deliver_config) ⇒ Object

Create and deliver a email



22
23
24
25
# File 'lib/gat/email.rb', line 22

def create_and_deliver_email(to, email_config, deliver_config)
  email = new_email(to, deliver_config.merge(email_config))
  send_email(email, deliver_config)
end

#new_email(to, options = {}) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/gat/email.rb', line 65

def new_email(to, options = {})

  options ||= Hash.new
  mail = TMail::Mail.new
  mail.date = Time.now
  mail.to = to
  mail.subject = options['subject']    || "#{ self.class.name } email report"
  mail.from = options['send_from']     || "#{ self.class.name.underscore }@#{ self.config['server_name'] }"      
  mail.cc = options['cc']              if options['cc']
  mail.bcc = options['bcc']            if options['bcc']

  mail.body = options['body']          || "empty body"
  mail
end

#send_email(email, smtp_config) ⇒ Object



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
# File 'lib/gat/email.rb', line 28

def send_email(email, smtp_config)
  
  if !smtp_config or smtp_config.empty? or !smtp_config['send_by']
    raise GatgetConfigException.new('email_config is empty or missing or send_by is not defined', 'send_email')
  end

  domain     = self.config['server_domain']  || 'localhost'
  email_from = smtp_config["send_from"] 	 || "#{ self.class.name }@#{ domain }"


  case smtp_config['send_by']
    when 'smtp'
      server    = smtp_config['smtp_config']["server"]   || 'localhost'
      port      = smtp_config['smtp_config']["port"]     || 25
      user      = smtp_config['smtp_config']["user"]     || ""
      password  = smtp_config['smtp_config']["password"] || ""

      begin
        Net::SMTP.start(server, port, server, user, password, :login) do |smtp|
          smtp.send_mail email.encoded, email_from, email.destinations
        end
      rescue => error
        raise GatgetConfigException.new(error, "send_email(smtp)", 
          { :message => "Error sending mail to '#{email.destinations}' on '#{server}:#{port}': #{error}" })
      end

    when 'sendmail'

      Net::SMTP.start('localhost', 25) do |smtp|
        smtp.send_mail email.encoded, email_from, email.destinations
      end
      
  else
    raise GatgetConfigException.new("undefined send_mail method #{ smtp_config['send_by'] } at config", 'send_email')
  end
end