Class: Hyla::Commands::Sendmail

Inherits:
Hyla::Command show all
Defined in:
lib/hyla/commands/sendmail.rb

Class Method Summary collapse

Methods inherited from Hyla::Command

check_mandatory_option?

Class Method Details

.inline_body_with_attachments(html, attachments) ⇒ Object

Substitute filename with cid



112
113
114
115
116
117
118
119
# File 'lib/hyla/commands/sendmail.rb', line 112

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

.parametersObject

Generate the Hash of the parameters used by mail compoent nto send email



98
99
100
101
102
103
104
105
106
107
# File 'lib/hyla/commands/sendmail.rb', line 98

def self.parameters()
  parameters = {}
  parameters[:address] = @smtp_server unless @smtp_server.nil?
  parameters[:port] = @port_number unless @port_number.nil?
  parameters[:enable_starttls] = @enable_starttls unless @enable_starttls.nil?
  parameters[:user_name] = @user unless @user.nil?
  parameters[:password] = @password unless @password.nil?
  parameters[:authentication] = @authentication unless @authentication.nil?
  return parameters
end

.populate_email(recipients, sender, subject) ⇒ Object

Create Mail using Recipients, Sender and subject



84
85
86
87
88
89
90
91
92
# File 'lib/hyla/commands/sendmail.rb', line 84

def self.populate_email(recipients, sender, subject)
  mail = Mail.new do
    to recipients
    from sender
    subject subject
    content_type 'multipart/related'
  end
  return mail
end

.process(args, options) ⇒ Object



5
6
7
8
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
78
# File 'lib/hyla/commands/sendmail.rb', line 5

def self.process(args, options)

  location = options[:source] if self.check_mandatory_option?('-s / --source', options[:source])
  file_name = options[:file] if check_mandatory_option?('-f / --file', options[:file])
  email_attributes = options[:email_attributes] if check_mandatory_option?('-e / --email_attributes', options[:email_attributes])
  attachment = options[:attachment]

  sender = email_attributes[:from]
  recipients = email_attributes[:to]
  subject = email_attributes[:subject]
  file_path = [location, file_name] * '/'

  # Parameters used to connect to the SMTP Server
  @smtp_server = email_attributes[:smtp_server] if check_mandatory_option?(':smtp_server not defined in _config.yaml file', email_attributes[:smtp_server])
  @port_number = email_attributes[:port] if check_mandatory_option?(':port not defined in _config.yaml file', email_attributes[:port])
  @user = email_attributes[:user]
  @password = email_attributes[:password]
  @authentication = email_attributes[:authentication]
  @enable_starttls = email_attributes[:enable_starttls] if check_mandatory_option?(':enable_start_tls not defined in _config.yaml file', email_attributes[:enable_starttls])

  body = <<-EOS
  <html>
  <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
      <title>REPORT</title>
      <link type="text/css" rel="stylesheet" href="http://cdn.jsdelivr.net/foundation/5.0.3/css/foundation.min.css" />
      <link type="text/css" rel="stylesheet" href="http://cdn.jsdelivr.net/normalize/2.1.3/normalize.min.css" />
  </head>
  <body>
   <div class="row">
      <!-- Main Content -->
      <div class="large-9 columns" role="content">
        <article>
          <h4>#{subject}</h4>
          <p>Attached as <a href=\'#{file_name}'>HTML file to this email</a></p>
          <p>Generated by Hyla Tool</p>
          </p>
          <p>Remark : html Link works with Thunderbird, Gmail clients but not using Zimbra Web Client</p>
        </article>
      </div>
  </div>
  </body>
  </html>
  EOS

  mail = populate_email(recipients, sender, subject)

  case attachment
    when true

      attachment = File.read(file_path)
      mail.attachments[file_name] = {
          :mime_type => 'application/x-html',
          :content => attachment,
          :Content_Transfer_Encoding => 'quoted-printable'}

      inline_html = inline_body_with_attachments(body, mail.attachments)

    when false
      inline_html = File.read(file_path)
  end

  html_part = Mail::Part.new do
    content_type 'text/html; charset=UTF-8'
    content_transfer_encoding 'quoted-printable'
    body inline_html
  end

  mail.html_part = html_part

  mail.delivery_method :smtp, parameters()
  mail.deliver!
  Hyla.logger2.info "Email send to SMTP server from #{sender} with this subject : #{subject}"
end