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 = "  <html>\n  <head>\n      <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n      <title>REPORT</title>\n      <link type=\"text/css\" rel=\"stylesheet\" href=\"http://cdn.jsdelivr.net/foundation/5.0.3/css/foundation.min.css\" />\n      <link type=\"text/css\" rel=\"stylesheet\" href=\"http://cdn.jsdelivr.net/normalize/2.1.3/normalize.min.css\" />\n  </head>\n  <body>\n   <div class=\"row\">\n      <!-- Main Content -->\n      <div class=\"large-9 columns\" role=\"content\">\n        <article>\n          <h4>\#{subject}</h4>\n          <p>Attached as <a href=\\'\#{file_name}'>HTML file to this email</a></p>\n          <p>Generated by Hyla Tool</p>\n          </p>\n          <p>Remark : html Link works with Thunderbird, Gmail clients but not using Zimbra Web Client</p>\n        </article>\n      </div>\n  </div>\n  </body>\n  </html>\n  EOS\n\n  mail = populate_email(recipients, sender, subject)\n\n  case attachment\n    when true\n\n      attachment = File.read(file_path)\n      mail.attachments[file_name] = {\n          :mime_type => 'application/x-html',\n          :content => attachment,\n          :Content_Transfer_Encoding => 'quoted-printable'}\n\n      inline_html = inline_body_with_attachments(body, mail.attachments)\n\n    when false\n      inline_html = File.read(file_path)\n  end\n\n  html_part = Mail::Part.new do\n    content_type 'text/html; charset=UTF-8'\n    content_transfer_encoding 'quoted-printable'\n    body inline_html\n  end\n\n  mail.html_part = html_part\n\n  mail.delivery_method :smtp, parameters()\n  mail.deliver!\n  Hyla.logger2.info \"Email send to SMTP server from \#{sender} with this subject : \#{subject}\"\nend\n"